How to include git revision into angular-cli application?

前端 未结 9 2195
甜味超标
甜味超标 2021-01-31 16:40

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

9条回答
  •  借酒劲吻你
    2021-01-31 16:52

    I've done it by generating prebuild script run on postinstall and run before any angular related script

    const fs =  require('fs');
    const git = require('git-rev-sync');
    var mkdirp = require('mkdirp');
    
    const releaseTag = git.tag();
    const template = `export const gitTag = '${releaseTag}';\n`;
    
    mkdirp('./generated', function(err) {
        fs.writeFileSync('./generated/git-tag.ts', template, { encoding: 'UTF-8' });
    });
    
    

    which generated git-tag.ts file:

    export const gitTag = 'xxxxxxx';
    

    and now u just use in component

    import { gitTag } from '[pathToRoot]/generated/git-tag';
    

    also add .gitignore

    generated

提交回复
热议问题