forked from mempool/mempool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-config.js
97 lines (84 loc) · 3.02 KB
/
update-config.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
const fs = require('fs');
const CONFIG_FILE_NAME = 'mempool-frontend-config.json';
const GENERATED_CONFIG_FILE_NAME = 'generated-config.js';
let settings = [];
let configContent = {};
const packageSettings = ['GIT_COMMIT_HASH', 'PACKAGE_JSON_VERSION']; //These will be handled by generate-config
var args = process.argv.slice(2);
function addSetting(key, value) {
settings.push({
key: key,
value: value
});
}
function normalizedValue(value) {
if (Number(value)) {
value = Number(value);
} else if ((value === 'true') || (value === 'false')) {
value = !!JSON.parse(String(value).toLowerCase());
} else {
value = String(value).toLowerCase();
}
return value;
}
function parseGeneratedFile() {
const generatedConfig = fs.readFileSync(GENERATED_CONFIG_FILE_NAME);
if (generatedConfig) {
const configContents = generatedConfig.toString();
const regexp = new RegExp(/window.__env.(\w+) = '(.*)'/,'g');
while ((match = regexp.exec(configContents)) !== null) {
// Do not add setting if it's the git hash or package json version
if (!packageSettings.includes(match[1])) {
const key = match[1];
const value = match[2];
console.log(typeof(value));
addSetting(key, value);
}
}
}
}
function saveSettingsJson() {
settings.forEach(setting => {
if (configContent.hasOwnProperty(setting['key']) && normalizedValue(configContent[setting['key']]) !== normalizedValue(setting['value'])) {
console.log(setting['key'] + " updated from " + configContent[setting['key']] + " to " + setting['value']);
} else if (configContent.hasOwnProperty(setting['key']) && normalizedValue(configContent[setting['key']]) === normalizedValue(setting['value'])) {
console.log(setting['key'] + " unchanged, skipping");
} else {
console.log(setting['key'] + " set to " + setting['value']);
}
configContent[setting['key']] = setting['value'];
});
fs.writeFileSync(CONFIG_FILE_NAME, JSON.stringify(configContent));
}
function configToJson() {
for (setting in configContent) {
settings.push({
key: setting,
value: configContent[setting]
});
}
}
try {
const rawConfig = fs.readFileSync(CONFIG_FILE_NAME);
configContent = JSON.parse(rawConfig);
console.log(`${CONFIG_FILE_NAME} file found, using provided config`);
} catch (e) {
if (e.code !== 'ENOENT') {
throw new Error(e);
} else {
if (fs.existsSync(GENERATED_CONFIG_FILE_NAME)) {
console.log(`${CONFIG_FILE_NAME} file not found, reading current config from generated-config.js`);
parseGeneratedFile();
}
}
}
if (args.length > 0) {
args.forEach(setting => {
setting = setting.split('=');
const key = setting[0];
let value = setting[1];
addSetting(key, normalizedValue(value));
});
}
saveSettingsJson();
console.log('new json', configContent);