forked from terraforming-mars/terraforming-mars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_static_json.js
67 lines (55 loc) · 2.05 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
require('dotenv').config()
const child_process = require("child_process");
const fs = require("fs");
const path = require("path");
function getAllTranslations() {
let pathToTranslationsDir = path.resolve("src/locales")
let translations = {};
let translationDir = "";
let dirs = fs.readdirSync(pathToTranslationsDir);
for (let idx in dirs) {
let lang = dirs[idx];
let 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);
for (let idx in files) {
let file = files[idx];
if ( file === undefined || ! file.endsWith(".json")) continue;
let dataJson = JSON.parse(fs.readFileSync(path.join(translationDir, file),"utf8"));
for (const phrase in dataJson) {
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) + " deployed " + new Date().toISOString();
}
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;
}
fs.writeFileSync("assets/settings.json", JSON.stringify({
version: generateAppVersion(),
waitingForTimeout: getWaitingForTimeout()
}));
fs.writeFileSync("assets/translations.json", JSON.stringify(
getAllTranslations()
));