-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (77 loc) · 2.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const { extractSheets } = require('spreadsheet-to-json');
const config = require('./config');
if (process.env.GCP_API_KEY === undefined) {
console.error('GCP_API_KEY require, create an API key here : https://console.developers.google.com/apis/credentials');
process.exit(1);
}
extractSheets({
spreadsheetKey: config.spreadsheetKey,
credentials: process.env.GCP_API_KEY,
}, (err, data) => {
if (err) console.log(err);
else {
try {
const boothI18nColumns = ['displayText', 'description'];
const { Booth: boothList, Config } = data;
const booths = boothList.map(({
slug, significant, image_url: imageUrl, point: rawPoint, isBonus: rawIsBonus, ...others
}) => {
const point = Number.parseInt(rawPoint, 10);
const isBonus = ((isBonusValue) => {
switch (isBonusValue && isBonusValue.toLowerCase()) {
case 'false':
return false;
case 'true':
return true;
default:
return null;
}
})(rawIsBonus);
if (isBonus === null) {
console.error({
slug, significant, imageUrl, point: rawPoint, isBonus: rawIsBonus, ...others,
});
throw new Error('isBonus is not given.');
}
return {
slug,
significant,
imageUrl,
point,
isBonus,
...boothI18nColumns.reduce((dict, key) => Object.assign(dict, {
[key]: Object.keys(others)
.filter((k) => (new RegExp(key).test(k)))
.reduce((i18nDict, keyLang) => {
const newKey = keyLang.match(new RegExp(`${key}_([a-zA-Z-]+)`));
return Object.assign(i18nDict, { [newKey[1]]: others[keyLang] });
}, {}),
}), {}),
};
});
const configI18nColumns = ['title', 'description'];
const { conf_name: confName, bingo_pattern: bingoPattern, ...others } = Config.reduce(
(dict, item) => Object.assign(dict, { [item.ColumnName]: item.Value }), {},
);
const profile = {
confName,
bingoPattern,
...configI18nColumns.reduce((dict, key) => Object.assign(dict, {
[key]: Object.keys(others)
.filter((k) => (new RegExp(key).test(k)))
.reduce((i18nDict, keyLang) => {
const newKey = keyLang.match(new RegExp(`${key}_([a-zA-Z-]+)`));
return Object.assign(i18nDict, { [newKey[1]]: others[keyLang] });
}, {}),
}), {}),
};
const output = JSON.stringify({
booths,
...profile,
});
console.log(output);
} catch (e) {
console.error(e);
}
}
});