version.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /** 自动记录版本信息**/
  2. const fs = require('fs')
  3. const buildPath = process.env.npm_package_remote_local //'wandaBmGuide' //放置 version.txt的路径
  4. const execSync = require('child_process').execSync //同步子进程
  5. const date = new Date() // Date对象
  6. // 获取时间函数
  7. const getDate = (df, tf) => {
  8. const dArr = [
  9. date.getFullYear(),
  10. date.getMonth() > 9 ? String(date.getMonth() + 1) : String('0' + (date.getMonth() + 1)),
  11. date.getDate() > 9 ? String(date.getDate()) : String('0' + date.getDate()),
  12. ]
  13. const tArr = [date.getHours(), date.getMinutes() > 9 ? String(date.getMinutes() + 1) : String('0' + date.getMinutes())]
  14. return `${dArr.join(df)}${tf ? ' ' : ''}${tArr.join(tf)}`
  15. }
  16. //写入最新的版本信息
  17. const branch = execSync('git symbolic-ref --short -q HEAD')
  18. .toString()
  19. .trim() // 分支
  20. const commit = execSync('git show -s --format=%h')
  21. .toString()
  22. .trim() //版本号
  23. const message = execSync('git show -s --format=%s')
  24. .toString()
  25. .trim() //说明
  26. const name = execSync('git show -s --format=%cn')
  27. .toString()
  28. .trim() //姓名
  29. const email = execSync('git show -s --format=%ce')
  30. .toString()
  31. .trim() //邮箱
  32. //const date = execSync('git show -s --format=%cd').toString().trim(); //日期
  33. const versionStr = `git:${commit}<${branch}>\n作者:${name}<${email}>\n日期:${getDate('-', ':')}\n说明:${message}\n`
  34. fs.writeFile(`${buildPath}/version.txt`, versionStr, (err) => {
  35. if (err) console.err('写入失败', err)
  36. })
  37. // 打包文件命名
  38. const distName = `打包命名:${buildPath}_${getDate('', '')}_git_${commit}`
  39. // 程序运行结束
  40. console.info(
  41. '\x1B[32m%s\x1b[0m',
  42. [
  43. distName,
  44. versionStr,
  45. '██████╗ ███████╗██████╗ ███████╗ █████╗ ██████╗██╗ ██╗',
  46. '██╔══██╗██╔════╝██╔══██╗██╔════╝██╔══██╗██╔════╝╚██╗ ██╔╝',
  47. '██████╔╝█████╗ ██████╔╝███████╗███████║██║ ███╗╚████╔╝ ',
  48. '██╔═══╝ ██╔══╝ ██╔══██╗╚════██║██╔══██║██║ ██║ ╚██╔╝ ',
  49. '██║ ███████╗██║ ██║███████║██║ ██║╚██████╔╝ ██║ ',
  50. '╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ',
  51. ].join('\n')
  52. )