-
根目录下 新建build_log.js
const fs = require('fs'); // 引入文件系统
const exec = require('child_process').exec; // 开启一个子进程
// 封装命令
function execute(command) {
return new Promise((resolve, reject) => {
exec(command, function(err, stdout, stderr) {
if (err != null) {
resolve(err)
} else {
resolve(stdout)
}
})
})
}
async function start() {
// 执行npm打包命令
console.log("开始打包...")
const _build = await execute("npm run build")
// 获取当前git分支名称
const branchName = await execute("git rev-parse --abbrev-ref HEAD")
// 执行git log命令 查看当前分支提交历史
const _history = await execute("git log -1 --pretty=\"short\"")
// 日志拼接
let newStr = '打包日期:' + new Date().toLocaleString()+ '\n分支名称:' + branchName + '\n' + _history
// 将日志写入log.txt
fs.writeFile('dist/log.txt', newStr, 'utf-8', function(err) {
if (err != null) {
console.log("ERROR:", err)
} else {
console.log("打包完成")
}
})
}
// 执行任务
start()
-
在package.json中scripts加入以下命令
"build_log":"node build_log.js",
-
打包时候运行命令
npm run build_log
-
在生成的dist中可以看到,生成了一个log.txt,内容如下:
![](https://img-blog.csdnimg.cn/img_convert/e350be41949a67d7414b590e6ec8fdff.png)