forked from terraforming-mars/terraforming-mars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_static_json.js
79 lines (66 loc) · 2.23 KB
/
make_static_json.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
// Generates the files settings.json and translations.json, stored in src/genfiles
require('dotenv').config();
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
function getAllTranslations() {
const pathToTranslationsDir = path.resolve('src/locales');
const translations = {};
let translationDir = '';
const dirs = fs.readdirSync(pathToTranslationsDir);
dirs.forEach((lang) => {
const localeDir = path.join(pathToTranslationsDir, lang);
if (lang.length === 2 && fs.statSync(localeDir).isDirectory()) {
translationDir = path.resolve(path.join(pathToTranslationsDir, lang));
const files = fs.readdirSync(translationDir);
files.forEach((file) => {
if ( file === undefined || ! file.endsWith('.json')) return;
const dataJson = JSON.parse(fs.readFileSync(path.join(translationDir, file), 'utf8'));
for (const phrase in dataJson) {
if (dataJson.hasOwnProperty(phrase)) {
if (translations[phrase] === undefined) {
translations[phrase] = {};
}
translations[phrase][lang] = dataJson[phrase];
}
}
});
}
});
return translations;
}
function generateAppVersion() {
// assumes SOURCE_VERSION is git hash
if (process.env.SOURCE_VERSION) {
return process.env.SOURCE_VERSION.substring(0, 7) + ' ' + new Date().toUTCString().replace(/ \(.+\)/, '');
}
try {
return child_process.execSync(`git log -1 --pretty=format:"%h %cD"`).toString();
} catch (error) {
console.warn('unable to generate app version', error);
return 'unknown version';
}
}
function getWaitingForTimeout() {
if (process.env.WAITING_FOR_TIMEOUT) {
return Number(process.env.WAITING_FOR_TIMEOUT);
}
return 5000;
}
function getLogLength() {
if (process.env.LOG_LENGTH) {
return Number(process.env.LOG_LENGTH);
}
return 50;
}
if (!fs.existsSync('src/genfiles')) {
fs.mkdirSync('src/genfiles');
}
fs.writeFileSync('src/genfiles/settings.json', JSON.stringify({
version: generateAppVersion(),
waitingForTimeout: getWaitingForTimeout(),
logLength: getLogLength(),
}));
fs.writeFileSync('src/genfiles/translations.json', JSON.stringify(
getAllTranslations(),
));