UNPKG

rollup-plugin-generate-git-version

Version:

Generate the front-end current release git version number and the last commit information file

48 lines (44 loc) 1.84 kB
'use strict'; var child_process = require('child_process'); /** *@Description: Generate git version numbers and commit information *@Author: Jackson *@CreateTime: 2021年12月27日 11:09:38 *@UpdateTime: **/ function generateGitVersion(options = { fileName: "gitVersion.json" }) { const { fileName } = options; return { name: "generateGitVersion", // 生成文件输出 generateBundle() { try { // 执行命令失败进行异常捕获 const command = "git log -1 --pretty=format:"; const Branch = child_process.execSync(`${command}%d`).toString().trim(); const Hash = child_process.execSync(`${command}%H`).toString().trim(); const author = child_process.execSync(`${command}%cn`).toString().trim(); const email = child_process.execSync(`${command}%ce`).toString().trim(); const content = child_process.execSync(`${command}%s`).toString().trim(); const date = child_process.execSync(`${command}%cd`).toString().trim(); const jsonStr = { Branch: `${Branch}`, Hash: `${Hash}`, CommitUser: `${author}(${email})`, CommitContent: `${content}`, Date: `${date}`, }; this.emitFile({ type: "asset", fileName: fileName, source: JSON.stringify(jsonStr, null, "\t"), }); } catch (error) { console.log(error); console.log(`------Use it in the Git version management directory------`); } }, }; } module.exports = generateGitVersion;