build.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 distZipPath = path.join(currentDirectory, '/zip/archives.zip');
  10. if(fs.existsSync(distZipPath)) {
  11. fs.unlinkSync(distZipPath);
  12. }
  13. // 获取配置文件
  14. console.log(`获取当前的配置文件...`)
  15. const indexJsonPath = path.join(currentDirectory, 'src/config/index.json');
  16. const indexJsonContent = fs.readFileSync(indexJsonPath, 'utf8');
  17. // 检测上次打包异常中断的缓存文件是否存在
  18. console.log(`检测上次打包异常中断的缓存文件是否存在...`)
  19. const cacheFilePath = path.join(currentDirectory, 'scripts/cache.json');
  20. if(!fs.existsSync(cacheFilePath)) {
  21. //创建缓存文件
  22. console.log(`创建配置缓存文件...`)
  23. const cacheJsonPath = path.join(currentDirectory, 'scripts/cache.json');
  24. fs.writeFileSync(cacheJsonPath, indexJsonContent, 'utf8');
  25. }
  26. //修改配置文件
  27. console.log(`修改配置文件为生产环境的配置...`)
  28. const indexJson = JSON.parse(indexJsonContent);
  29. indexJson.version = dateFormat(new Date()); //版本号
  30. indexJson.target = "http://127.0.0.1:8090"; //接口地址
  31. indexJson.smsPhone = ""; //短信接口手机号
  32. indexJson.vite = {}; //vite配置
  33. //更新配置文件
  34. console.log(`更新配置文件...`)
  35. fs.writeFileSync(indexJsonPath, JSON.stringify(indexJson, null, 2));
  36. console.log(`----------------------------`)
  37. console.log(`开始编译打包项目...`)
  38. //时间格式化
  39. function dateFormat(date) {
  40. let format = 'yyyyMMddhhmmss';
  41. let o = {
  42. "M+": date.getMonth() + 1, //month
  43. "d+": date.getDate(), //day
  44. "h+": date.getHours(), //hour
  45. "m+": date.getMinutes(), //minute
  46. "s+": date.getSeconds(), //second
  47. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  48. "S": date.getMilliseconds() //millisecond
  49. }
  50. if (/(y+)/.test(format)) {
  51. format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  52. }
  53. for (let k in o) {
  54. if (new RegExp("(" + k + ")").test(format)) {
  55. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  56. }
  57. }
  58. return format;
  59. }