Last active
November 10, 2022 09:47
-
-
Save bilelz/04707e300811d2b7fe3815ede073422c to your computer and use it in GitHub Desktop.
Generate app version file from package.json and git | for angular or other frameworks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
From https://gist.github.com/bilelz/04707e300811d2b7fe3815ede073422c | |
inspired by https://medium.com/@amcdnl/version-stamping-your-app-with-the-angular-cli-d563284bb94d | |
*/ | |
const fs = require('fs'); | |
const exec = require('child_process').execSync; | |
/* Get package.json version. */ | |
const packageVersion = JSON.parse( | |
fs.readFileSync('package.json', 'UTF-8') | |
).version.toString(); | |
/* Get git full revision. */ | |
const commitSha = exec('git rev-parse HEAD').toString().trim(); | |
/* Get git short revision. */ | |
const commitShortSha = exec('git rev-parse --short HEAD').toString().trim(); // --short=8 if you want to be like gitlab | |
/* Get git branch. */ | |
const branch = exec('git rev-parse --abbrev-ref HEAD').toString().trim(); | |
/* Get build date. */ | |
const buildDateUTC = new Date().toUTCString(); | |
const appInfo = { | |
buildDateUTC, | |
packageVersion, | |
commitSha, | |
commitShortSha | |
branch, | |
}; | |
console.log('App Informations', appInfo); | |
const exportedInfos = `// IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT OR CHECKIN! | |
/* tslint:disable */ | |
export const APP_VERSION = ${JSON.stringify(appInfo, null, 2)}; | |
/* tslint:enable */`; | |
fs.writeFileSync('src/environments/app.version.ts', exportedInfos, { | |
encoding: 'utf8', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To execute it, add a
postinstall
script to yourpackage.json
:postinstall
is automatically executed after eachnpm install
Example of a generated
app.version.ts
file :To display/use it :
Add it to .gitignore