I need to display git revision on my angular2 application\'s about page. The project is based on angular-cli.
How can build be extended so git revision is put for exampl
For angular 6
1 Install git-describe as a dev dependency
npm i git-describe -s
2 On your root project create a grab-git-info.js
const { gitDescribeSync } = require('git-describe');
const { writeFileSync } = require('fs');
const path = require('path');
const info = gitDescribeSync();
const infoJson = JSON.stringify(info, null, 2);
writeFileSync(path.join(__dirname, '/src/git-version.json'), infoJson);
The output of the grab-git-info.js script will be the ‘git-version.json’ file under /src/ which will contain all the git info needed by our app.
In order to be able to import the json file (or any other json file) we need to add a definition file declaring of the added module so that the Typescript compiler will recognize it.
/src/typings.d.ts:
declare module '*.json' {
const value: any;
export default value;
}
From this point on you can import any json file located under /src as a module!
In your component you can import this json
import * as data from '../../../git-version.json';
...
public git = data;
In the html
Rev: {{git.hash}}
Finally Add and most important, run the script before build
In package.json add:
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "node grab-git-info && ng build",
And run the app with
npm run build
The other answers were helpful, but I preferred a more simple, direct approach. Here's mine.
Run npm install git-describe --save-dev
. Then add git-version.js
to the root:
// This script runs operations *synchronously* which is normally not the best
// approach, but it keeps things simple, readable, and for now is good enough.
const { gitDescribeSync } = require('git-describe');
const { writeFileSync } = require('fs');
const gitInfo = gitDescribeSync();
const versionInfoJson = JSON.stringify(gitInfo, null, 2);
writeFileSync('git-version.json', versionInfoJson);
Optionally you can add /git-version.json
to your .gitignore
file.
Update your package.json
to do something like this:
"scripts": {
"build": "node git-version.js && ng build"
}
Then add version-info.ts
to the root of your project:
export const versionInfo = (() => {
try {
// tslint:disable-next-line:no-var-requires
return require('../../git-version.json');
} catch {
// In dev the file might not exist:
return { tag: 'v0.0.0', hash: 'dev' };
}
})();
And import
the versionInfo
in your app.component.ts
or anywhere else you'd want to use it.
I like to keep things simple. Can add to your index.html:
<script>window.version = '{git-hash}';</script>
Then add a postbuild
script to your package.json
:
"postbuild": "sed -i '' \"s/{git\\-hash}/$(git rev-parse --short HEAD)/g\" dist/*/index.html"
Not elegant by any means. Personally I create an object on window
with various information about the build (time, version, and a release summary link).
To stay more 'pure', stick the {git-hash}
string in environment.prod.ts
and run the sed
against all main-*.js
files produced.
"postbuild": "sed -i '' \"s/{git\\-hash}/$(git rev-parse --short HEAD)/g\" dist/*/main-*.js"
Note you'll always see '{git-hash}' running locally as it's only replaced post-build. If you replace it before the build, obviously can't replace it on future builds locally and would most definitely accidentally check that in.
---- UPDATE ----
Ended up creating a library to pull various information. Only ended up using version, build time, and commitTime personally.
https://www.npmjs.com/package/@rippell/ngx-build-info