build.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const path = require('path');
  2. const fs = require('fs');
  3. // 获取当前命令行上下文路径
  4. const currentDirectory = process.cwd();
  5. console.log(`----------------------------`)
  6. console.log(`正在处理编译打包前的准备...`)
  7. //修改版本更新信息
  8. console.log(`更新版本更新信息...`)
  9. const versionPath = path.join(currentDirectory, '/public/version.json');
  10. const versionContent = fs.readFileSync(versionPath, 'utf8');
  11. //修改配置文件
  12. const versionJson = JSON.parse(versionContent);
  13. versionJson.value = dateFormat(new Date()); //版本号
  14. //更新版本更新信息
  15. fs.writeFileSync(versionPath, JSON.stringify(versionJson, null, 2));
  16. console.log(`----------------------------`)
  17. //删除上次打包相关的文件
  18. console.log(`正在删除上次打包相关的文件...`)
  19. const distZipPath = path.join(currentDirectory, '/zip/saber.zip');
  20. if(fs.existsSync(distZipPath)) {
  21. fs.unlinkSync(distZipPath);
  22. }
  23. // 获取配置文件
  24. console.log(`获取当前的配置文件...`)
  25. const indexJsonPath = path.join(currentDirectory, 'src/config/index.json');
  26. const indexJsonContent = fs.readFileSync(indexJsonPath, 'utf8');
  27. // 检测上次打包异常中断的缓存文件是否存在
  28. console.log(`检测上次打包异常中断的缓存文件是否存在...`)
  29. const cacheFilePath = path.join(currentDirectory, 'scripts/cache.json');
  30. if(!fs.existsSync(cacheFilePath)) {
  31. //创建缓存文件
  32. console.log(`创建配置缓存文件...`)
  33. const cacheJsonPath = path.join(currentDirectory, 'scripts/cache.json');
  34. fs.writeFileSync(cacheJsonPath, indexJsonContent, 'utf8');
  35. }
  36. //修改配置文件
  37. const indexJson = JSON.parse(indexJsonContent);
  38. indexJson.target = "http://127.0.0.1:8090"; //接口地址
  39. indexJson.dev = {}; //清空端口号配置
  40. //更新配置文件
  41. console.log(`修改配置文件为生产环境的配置...`)
  42. fs.writeFileSync(indexJsonPath, JSON.stringify(indexJson, null, 2));
  43. //时间格式化
  44. function dateFormat(date) {
  45. let format = 'yyyyMMddhhmmss';
  46. let o = {
  47. "M+": date.getMonth() + 1, //month
  48. "d+": date.getDate(), //day
  49. "h+": date.getHours(), //hour
  50. "m+": date.getMinutes(), //minute
  51. "s+": date.getSeconds(), //second
  52. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  53. "S": date.getMilliseconds() //millisecond
  54. }
  55. if (/(y+)/.test(format)) {
  56. format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  57. }
  58. for (let k in o) {
  59. if (new RegExp("(" + k + ")").test(format)) {
  60. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  61. }
  62. }
  63. return format;
  64. }