一,根路径下创建version.js文件,内容为以下
const fs = require('fs');
const execSync = require('child_process').execSync;
const execGitCmd = cmd => execSync(cmd).toString().replace(/\n$/, '');
// git相关信息
const date = new Date().toLocaleString(); // 编译时间
const user = execGitCmd('git config user.name'); // 编译执行人名称
const email = execGitCmd('git config user.email'); // 编译执行人邮箱
const gitBranch = execGitCmd(`git symbolic-ref --short -q HEAD`); // git 分支
const commit = execGitCmd(`git log -n 1 --pretty=format:"%H"`); // 最近commit
const info = execGitCmd('git log --abbrev-commit -1'); // 最近1次 git 提交信息
try {
const versionStr = `
环境: ${process.env.NODE_ENV || '--'}
编译时间:${date}
执行人: ${user} <${email}>
分支: ${gitBranch}
commitId:${commit}
最后提交信息:
${info}
`;
const group = `console.group('版本信息');`;
const allInfo = `console.log(${JSON.stringify(versionStr)});`;
const output = `
${group}
${allInfo}
`
console.log(versionStr)
fs.writeFileSync(`version.txt`, versionStr);
fs.writeFileSync('versionLog.js', output, err => {
if (err) console.log(err)
})
} catch (e) {
throw new Error(e);
}
二,根路径下创建 versionLog.js 文件
入口文件main.ts 引入
import '../versionLog';
三,build和start命令前加上node version.js &&
"start": "node version.js && node --max_old_space_size=5120 ./node_modules/@angular/cli/bin/ng serve --port 4206 --host 0.0.0.0 --optimization=false",
"build": "node version.js && node --max_old_space_size=5120 ./node_modules/@angular/cli/bin/ng build --prod --output-hashing=all --optimization=false",