如何将 git 修订包含到 angular-cli 应用程序中? [英] How to include git revision into angular-cli application?
问题描述
我需要在我的 angular2 应用程序的关于页面上显示 git 修订版.该项目基于angular-cli.
I need to display git revision on my angular2 application's about page. The project is based on angular-cli.
如何扩展构建以便将 git 修订版放入例如 environment.ts
或其他应用程序可访问的位置?
How can build be extended so git revision is put for example into environment.ts
or other place accessible to application?
推荐答案
根据@Yuri 的建议,我能够通过使用 npm
脚本来解决这个问题.
As suggested by @Yuri, I was able to solve this by using npm
scripting.
- 在angular-cli项目的根目录定义
git.version.ts
:
import { Observable, combineLatest } from 'rxjs'
declare var require: any;
declare var process: any;
const fs = require('fs');
const exec = require('child_process').exec;
const revision = new Observable<string>(s => {
exec('git rev-parse --short HEAD',
(error: Error, stdout, stderr) => {
if (error !== null) {
console.log('git error: ' + error + stderr);
}
s.next(stdout.toString().trim());
s.complete();
});
});
const branch = new Observable<string>(s => {
exec('git rev-parse --abbrev-ref HEAD',
(error: Error, stdout, stderr) => {
if (error !== null) {
console.log('git error: ' + error + stderr);
}
s.next(stdout.toString().trim());
s.complete();
});
});
combineLatest(revision, branch)
.subscribe(([revision, branch]) => {
console.log(`version: '${process.env.npm_package_version}', revision: '${revision}', branch: '${branch}'`);
const content = '// this file is automatically generated by git.version.ts script\n' +
`export const versions = {version: '${process.env.npm_package_version}', revision: '${revision}', branch: '${branch}'};`;
fs.writeFileSync(
'src/environments/versions.ts',
content,
{encoding: 'utf8'}
);
});
- 在
package.json
中添加了预构建钩子:
- Added pre-build hook in
package.json
:
"scripts": {
"ng": "ng",
...
"start": "ng serve --proxy proxy-config.json",
"prebuild.prod": "ts-node -O \"{\\\"module\\\":\\\"commonjs\\\"}\" git.version.ts",
"build.prod": "ng build -prod",
...
}
在应用程序中使用生成的
src/environments/versions.ts
.
更新 10/2018: 这是脚本的可读性更强的版本,rxjs-version-agnostic:
UPDATE 10/2018: Here is the more-readable version of the script, rxjs-version-agnostic:
import { writeFileSync } from 'fs';
import { dedent } from 'tslint/lib/utils';
import { promisify } from 'util';
import * as child from 'child_process';
const exec = promisify(child.exec);
async function createVersionsFile(filename: string) {
const revision = (await exec('git rev-parse --short HEAD')).stdout.toString().trim();
const branch = (await exec('git rev-parse --abbrev-ref HEAD')).stdout.toString().trim();
console.log(`version: '${process.env.npm_package_version}', revision: '${revision}', branch: '${branch}'`);
const content = dedent`
// this file is automatically generated by git.version.ts script
export const versions = {
version: '${process.env.npm_package_version}',
revision: '${revision}',
branch: '${branch}'
};`;
writeFileSync(filename, content, {encoding: 'utf8'});
}
createVersionsFile('src/environments/versions.ts');
注意,在使用 angular-cli v7.0.6 时,我还必须更改 package.json
中的脚本调用:
Note, when using angular-cli v7.0.6, I also had to change script invocation in the package.json
:
"scripts": {
...
"prebuild.prod": "ts-node -O '{\"module\": \"commonjs\"}' git.version.ts",
...
}
这篇关于如何将 git 修订包含到 angular-cli 应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!