From 4c7d0cd2e5586952afaf708167a4d16364c5f2ca Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn <100320+knorrium@users.noreply.github.com> Date: Wed, 16 Jun 2021 11:47:05 -0700 Subject: [PATCH] Generate config on serve and updated git revision method (#587) * run generate-config on serve * write the config file only if settings have changed * read the git commit hash from the current branch, not master * git sha is now short by default, no need to trim on the about component --- frontend/generate-config.js | 20 ++++++++++++++----- frontend/package.json | 6 +++--- .../app/components/about/about.component.ts | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/frontend/generate-config.js b/frontend/generate-config.js index 6540ccf56..3746c8ab3 100644 --- a/frontend/generate-config.js +++ b/frontend/generate-config.js @@ -1,4 +1,5 @@ var fs = require('fs'); +const { execSync } = require('child_process'); const CONFIG_FILE_NAME = 'mempool-frontend-config.json'; const GENERATED_CONFIG_FILE_NAME = 'generated-config.js'; @@ -32,12 +33,13 @@ for (setting in configContent) { } try { - gitCommitHash = fs.readFileSync('../.git/refs/heads/master').toString().trim(); + const command = 'git rev-parse --short HEAD'; + gitCommitHash = execSync(command).toString('utf8').replace(/[\n\r\s]+$/, ''); } catch (e) { console.log('Could not load git commit info: ' + e.message || e); } -const code = `(function (window) { +const newConfig = `(function (window) { window.__env = window.__env || {};${settings.reduce((str, obj) => `${str} window.__env.${obj.key} = ${ typeof obj.value === 'string' ? `'${obj.value}'` : obj.value };`, '')} window.__env.GIT_COMMIT_HASH = '${gitCommitHash}'; @@ -45,9 +47,17 @@ const code = `(function (window) { }(global || this));`; try { - fs.writeFileSync(GENERATED_CONFIG_FILE_NAME, code, 'utf8'); + 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); + } + } } catch (e) { throw new Error(e); } - -console.log('Config file generated'); \ No newline at end of file diff --git a/frontend/package.json b/frontend/package.json index bfc473dc6..902417956 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -24,9 +24,9 @@ "tsc": "./node_modules/typescript/bin/tsc", "i18n-extract-from-source": "./node_modules/@angular/cli/bin/ng xi18n --ivy --out-file ./src/locale/messages.xlf", "i18n-pull-from-transifex": "tx pull -a --parallel --minimum-perc 1 --force", - "serve": "ng serve -c local", - "serve:stg": "ng serve -c staging", - "serve:local-prod": "ng serve -c local-prod", + "serve": "npm run generate-config && ng serve -c local", + "serve:stg": "npm run generate-config && ng serve -c staging", + "serve:local-prod": "npm run generate-config && ng serve -c local-prod", "start": "npm run generate-config && npm run sync-assets-dev && ng serve -c local", "start:stg": "npm run generate-config && npm run sync-assets-dev && ng serve -c staging", "start:local-prod": "npm run generate-config && npm run sync-assets-dev && ng serve -c local-prod", diff --git a/frontend/src/app/components/about/about.component.ts b/frontend/src/app/components/about/about.component.ts index ccec8ab5f..2ea120601 100644 --- a/frontend/src/app/components/about/about.component.ts +++ b/frontend/src/app/components/about/about.component.ts @@ -17,7 +17,7 @@ export class AboutComponent implements OnInit { backendInfo$: Observable; sponsors$: Observable; contributors$: Observable; - frontendGitCommitHash = this.stateService.env.GIT_COMMIT_HASH.substr(0, 8); + frontendGitCommitHash = this.stateService.env.GIT_COMMIT_HASH; packetJsonVersion = this.stateService.env.PACKAGE_JSON_VERSION; officialMempoolSpace = this.stateService.env.OFFICIAL_MEMPOOL_SPACE; showNavigateToSponsor = false;