/** 自动记录版本信息**/ const fs = require('fs') const buildPath = process.env.npm_package_remote_local //'wandaBmGuide' //放置 version.txt的路径 const execSync = require('child_process').execSync //同步子进程 const date = new Date() // Date对象 // 获取时间函数 const getDate = (df, tf) => { const dArr = [ date.getFullYear(), date.getMonth() > 9 ? String(date.getMonth() + 1) : String('0' + (date.getMonth() + 1)), date.getDate() > 9 ? String(date.getDate()) : String('0' + date.getDate()), ] const tArr = [date.getHours(), date.getMinutes() > 9 ? String(date.getMinutes() + 1) : String('0' + date.getMinutes())] return `${dArr.join(df)}${tf ? ' ' : ''}${tArr.join(tf)}` } //写入最新的版本信息 const branch = execSync('git symbolic-ref --short -q HEAD') .toString() .trim() // 分支 const commit = execSync('git show -s --format=%h') .toString() .trim() //版本号 const message = execSync('git show -s --format=%s') .toString() .trim() //说明 const name = execSync('git show -s --format=%cn') .toString() .trim() //姓名 const email = execSync('git show -s --format=%ce') .toString() .trim() //邮箱 //const date = execSync('git show -s --format=%cd').toString().trim(); //日期 const versionStr = `git:${commit}<${branch}>\n作者:${name}<${email}>\n日期:${getDate('-', ':')}\n说明:${message}\n` fs.writeFile(`${buildPath}/version.txt`, versionStr, (err) => { if (err) console.err('写入失败', err) }) // 打包文件命名 const distName = `打包命名:${buildPath}_${getDate('', '')}_git_${commit}` // 程序运行结束 console.info( '\x1B[32m%s\x1b[0m', [ distName, versionStr, '██████╗ ███████╗██████╗ ███████╗ █████╗ ██████╗██╗ ██╗', '██╔══██╗██╔════╝██╔══██╗██╔════╝██╔══██╗██╔════╝╚██╗ ██╔╝', '██████╔╝█████╗ ██████╔╝███████╗███████║██║ ███╗╚████╔╝ ', '██╔═══╝ ██╔══╝ ██╔══██╗╚════██║██╔══██║██║ ██║ ╚██╔╝ ', '██║ ███████╗██║ ██║███████║██║ ██║╚██████╔╝ ██║ ', '╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ', ].join('\n') )