2020-06-22 17:10:49 +02:00
|
|
|
var fs = require('fs');
|
2021-06-16 20:47:05 +02:00
|
|
|
const { execSync } = require('child_process');
|
2020-06-22 17:10:49 +02:00
|
|
|
|
|
|
|
const CONFIG_FILE_NAME = 'mempool-frontend-config.json';
|
|
|
|
const GENERATED_CONFIG_FILE_NAME = 'generated-config.js';
|
|
|
|
|
|
|
|
let settings = [];
|
|
|
|
let configContent = {};
|
2021-04-12 20:17:13 +02:00
|
|
|
let gitCommitHash = '';
|
|
|
|
let packetJsonVersion = '';
|
2020-06-22 17:10:49 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const rawConfig = fs.readFileSync(CONFIG_FILE_NAME);
|
|
|
|
configContent = JSON.parse(rawConfig);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== 'ENOENT') {
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 20:17:13 +02:00
|
|
|
try {
|
|
|
|
const packageJson = fs.readFileSync('package.json');
|
|
|
|
packetJsonVersion = JSON.parse(packageJson).version;
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:10:49 +02:00
|
|
|
for (setting in configContent) {
|
|
|
|
settings.push({
|
|
|
|
key: setting,
|
|
|
|
value: configContent[setting]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-12 20:17:13 +02:00
|
|
|
try {
|
2021-06-16 20:47:05 +02:00
|
|
|
const command = 'git rev-parse --short HEAD';
|
|
|
|
gitCommitHash = execSync(command).toString('utf8').replace(/[\n\r\s]+$/, '');
|
2021-04-12 20:17:13 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log('Could not load git commit info: ' + e.message || e);
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:47:05 +02:00
|
|
|
const newConfig = `(function (window) {
|
2020-06-22 17:10:49 +02:00
|
|
|
window.__env = window.__env || {};${settings.reduce((str, obj) => `${str}
|
|
|
|
window.__env.${obj.key} = ${ typeof obj.value === 'string' ? `'${obj.value}'` : obj.value };`, '')}
|
2021-04-12 20:17:13 +02:00
|
|
|
window.__env.GIT_COMMIT_HASH = '${gitCommitHash}';
|
|
|
|
window.__env.PACKAGE_JSON_VERSION = '${packetJsonVersion}';
|
2020-11-22 20:30:46 +01:00
|
|
|
}(global || this));`;
|
2020-06-22 17:10:49 +02:00
|
|
|
|
|
|
|
try {
|
2021-06-16 20:47:05 +02:00
|
|
|
const currentConfig = fs.readFileSync(GENERATED_CONFIG_FILE_NAME).toString().trim();
|
|
|
|
if (currentConfig === newConfig) {
|
|
|
|
console.log("Configuration not changed, skipping generation");
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(GENERATED_CONFIG_FILE_NAME, newConfig, 'utf8');
|
|
|
|
console.log('Config file generated');
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 17:10:49 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error(e);
|
|
|
|
}
|