forked from terraforming-mars/terraforming-mars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_static_json.js
123 lines (103 loc) · 3.64 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* eslint-disable no-undef */
// 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 filename = path.join(translationDir, file);
try {
const dataJson = JSON.parse(fs.readFileSync(filename, 'utf8'));
for (const phrase in dataJson) {
if (dataJson.hasOwnProperty(phrase)) {
if (translations[phrase] === undefined) {
translations[phrase] = {};
}
translations[phrase][lang] = dataJson[phrase];
}
}
} catch (e) {
throw new Error(`While parsing ${filename}:` + e);
}
});
}
});
return translations;
}
function getBuildMetadata() /* {head: string, date: string} */ {
// assumes SOURCE_VERSION is git hash
if (process.env.SOURCE_VERSION) {
return {
head: process.env.SOURCE_VERSION.substring(0, 7),
date: new Date().toUTCString().replace(/ \(.+\)/, ''),
};
}
try {
const output = child_process.execSync(`git log -1 --pretty=format:"%h %cD"`).toString();
const [head, ...rest] = output.split(' ');
return {head, date: rest.join(' ')};
} catch (error) {
console.error('unable to generate app version', error);
return {head: 'n/a', date: 'n/a'};
}
}
function getWaitingForTimeout() {
if (process.env.WAITING_FOR_TIMEOUT) {
return Number(process.env.WAITING_FOR_TIMEOUT);
}
return 1000;
}
function getLogLength() {
if (process.env.LOG_LENGTH) {
return Number(process.env.LOG_LENGTH);
}
return 50;
}
if (!fs.existsSync('src/genfiles')) {
fs.mkdirSync('src/genfiles');
}
const buildmetadata = getBuildMetadata();
fs.writeFileSync('src/genfiles/settings.json', JSON.stringify({
head: buildmetadata.head,
builtAt: buildmetadata.date,
waitingForTimeout: getWaitingForTimeout(),
logLength: getLogLength(),
}));
fs.writeFileSync('src/genfiles/translations.json', JSON.stringify(
getAllTranslations(),
));
/**
* Generate translation files in `/assets/locales/*.json` to load them async by the client
*/
function generateTranslations() {
const localesDir = path.join(__dirname, 'src/locales');
const localesCodes = fs.readdirSync(localesDir);
const destinationPath = path.join(__dirname, 'assets/locales');
if (!fs.existsSync(destinationPath)) {
fs.mkdirSync(destinationPath);
}
const isJSONExt = (fileName) => fileName.endsWith('.json');
localesCodes.forEach((localeCode) => {
const localeDir = path.join(localesDir, localeCode);
const localeFiles = fs.readdirSync(localeDir).filter(isJSONExt);
const localeObject = localeFiles.reduce((localeObject, localeFile) => {
const filePath = path.join(localeDir, localeFile);
Object.assign(localeObject, require(filePath));
return localeObject;
}, {});
const destinationFile = path.join(destinationPath, `${localeCode}.json`);
fs.writeFileSync(destinationFile, JSON.stringify(localeObject));
});
}
generateTranslations();