update generate-config to read the short sha from docker

This commit is contained in:
Felipe Knorr Kuhn 2021-06-25 23:47:40 -07:00
parent 446bdfebea
commit 33d37a9b5b

View File

@ -1,5 +1,5 @@
var fs = require('fs');
const { execSync } = require('child_process');
const { spawnSync } = require('child_process');
const CONFIG_FILE_NAME = 'mempool-frontend-config.json';
const GENERATED_CONFIG_FILE_NAME = 'generated-config.js';
@ -12,15 +12,19 @@ let packetJsonVersion = '';
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 {
console.log(`${CONFIG_FILE_NAME} file not found, using default config`);
}
}
try {
const packageJson = fs.readFileSync('package.json');
packetJsonVersion = JSON.parse(packageJson).version;
console.log(`mempool version ${packetJsonVersion}`);
} catch (e) {
throw new Error(e);
}
@ -32,11 +36,23 @@ for (setting in configContent) {
});
}
if (process.env.DOCKER_COMMIT_HASH) {
gitCommitHash = process.env.DOCKER_COMMIT_HASH
} else {
try {
const command = 'git rev-parse --short HEAD';
gitCommitHash = execSync(command).toString('utf8').replace(/[\n\r\s]+$/, '');
const gitRevParse = spawnSync('git', ['rev-parse', '--short', 'HEAD']);
if (!gitRevParse.error) {
gitCommitHash = gitRevParse.stdout.toString('utf-8').replace(/[\n\r\s]+$/, '');
console.log(`mempool revision ${gitCommitHash}`);
} else if (gitRevParse.error.code === 'ENOENT') {
console.log('git not found, cannot parse git hash');
gitCommitHash = '?';
}
} catch (e) {
console.log('Could not load git commit info: ' + e.message || e);
console.log('Could not load git commit info: ' + e.message);
gitCommitHash = '?';
}
}
const newConfig = `(function (window) {
@ -46,18 +62,38 @@ const newConfig = `(function (window) {
window.__env.PACKAGE_JSON_VERSION = '${packetJsonVersion}';
}(global || this));`;
function readConfig(path) {
try {
const currentConfig = fs.readFileSync(GENERATED_CONFIG_FILE_NAME).toString().trim();
if (currentConfig === newConfig) {
console.log("Configuration not changed, skipping generation");
const currentConfig = fs.readFileSync(path).toString().trim();
return currentConfig;
} catch (e) {
return false;
}
}
function writeConfig(path, config) {
try {
fs.writeFileSync(path, config, 'utf8');
} catch (e) {
throw new Error(e);
}
}
const currentConfig = readConfig(GENERATED_CONFIG_FILE_NAME);
if (currentConfig && currentConfig === newConfig) {
console.log(`No configuration updates, skipping ${GENERATED_CONFIG_FILE_NAME} file update`);
return;
} else if (!currentConfig) {
console.log(`${GENERATED_CONFIG_FILE_NAME} file not found, creating new config file`);
console.log('CONFIG: ', newConfig);
writeConfig(GENERATED_CONFIG_FILE_NAME, newConfig);
console.log(`${GENERATED_CONFIG_FILE_NAME} file saved`);
return;
} else {
try {
fs.writeFileSync(GENERATED_CONFIG_FILE_NAME, newConfig, 'utf8');
console.log('Config file generated');
} catch (e) {
throw new Error(e);
}
}
} catch (e) {
throw new Error(e);
}
console.log(`Configuration changes detected, updating ${GENERATED_CONFIG_FILE_NAME} file`);
console.log('OLD CONFIG: ', currentConfig);
console.log('NEW CONFIG: ', newConfig);
writeConfig(GENERATED_CONFIG_FILE_NAME, newConfig);
console.log(`${GENERATED_CONFIG_FILE_NAME} file updated`);
};