version.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**定义模块和变量**/
  2. const exec = require('child_process').exec; //异步子进程
  3. const execSync = require('child_process').execSync; //同步子进程
  4. const fs = require('fs'); //文件读取模块
  5. const versionPath = 'version.txt'; //version路径
  6. const buildPath = 'wandaBmGuideH5'; //打包的路径
  7. const autoPush = true; //写入版本信息之后是否自动提交git上
  8. const autoText = "node自动提交的版本信息"; //自动提交时的说明
  9. const commit = execSync('git show -s --format=%H').toString().trim(); //当前提交的版本号
  10. /**程序开始**/
  11. var versionStr = ""; //版本信息字符串
  12. // 如果versionPath存在,将先读取里边的版本信息
  13. if (fs.existsSync(versionPath)) {
  14. versionStr = fs.readFileSync(versionPath).toString() + '\n';
  15. }
  16. // 根据版本信息是已存在commit,进行不同处理
  17. if (!autoPush && versionStr.indexOf(commit) != -1) {
  18. console.warn('\x1B[33m%s\x1b[0m', 'warming: 当前的git版本数据已经存在了!\n')
  19. } else if (execSync('git show -s --format=%s').toString().trim() == autoText) {
  20. console.warn('\x1B[33m%s\x1b[0m', `warming: 读取最新的git说明为(${autoText})!\n`);
  21. } else {
  22. let name = execSync('git show -s --format=%cn').toString().trim(); //姓名
  23. let email = execSync('git show -s --format=%ce').toString().trim(); //邮箱
  24. let date = new Date(execSync('git show -s --format=%cd').toString()); //日期
  25. let message = execSync('git show -s --format=%s').toString().trim(); //说明
  26. versionStr = `git:${commit}\n作者:${name}<${email}>\n日期:${date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes()}\n说明:${message}\n${new Array(80).join('*')}\n${versionStr}`;
  27. fs.writeFileSync(versionPath, versionStr);
  28. // 写入版本信息之后,自动将版本信息提交到当前分支的git上
  29. if (autoPush) {
  30. execSync(`git commit ${versionPath} -m "${autoText}"`);
  31. execSync(`git push origin ${execSync('git rev-parse --abbrev-ref HEAD').toString().trim()}`);
  32. }
  33. }
  34. // 将version文件移植到打包文件中
  35. if (fs.existsSync(buildPath)) {
  36. fs.writeFileSync(`${buildPath}/${versionPath}`, fs.readFileSync(versionPath));
  37. }
  38. // 程序执行结束
  39. console.info('\x1B[32m%s\x1b[0m', [
  40. "██████╗ ███████╗██████╗ ███████╗ █████╗ ██████╗██╗ ██╗",
  41. "██╔══██╗██╔════╝██╔══██╗██╔════╝██╔══██╗██╔════╝╚██╗ ██╔╝",
  42. "██████╔╝█████╗ ██████╔╝███████╗███████║██║ ███╗╚████╔╝ ",
  43. "██╔═══╝ ██╔══╝ ██╔══██╗╚════██║██╔══██║██║ ██║ ╚██╔╝ ",
  44. "██║ ███████╗██║ ██║███████║██║ ██║╚██████╔╝ ██║ ",
  45. "╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ",
  46. ].join('\n'));