build.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 cacheJsonPath = path.join(currentDirectory, 'scripts/cache.json');
  20. fs.writeFileSync(cacheJsonPath, indexJsonContent, 'utf8');
  21. //修改配置文件
  22. console.log(`修改配置文件为生产环境的配置...`)
  23. const indexJson = JSON.parse(indexJsonContent);
  24. indexJson.version = dateFormat(new Date()); //版本号
  25. indexJson.target = "http://127.0.0.1:8090"; //接口地址
  26. indexJson.smsPhone = ""; //短信接口手机号
  27. indexJson.vite = {}; //vite配置
  28. //更新配置文件
  29. console.log(`更新配置文件...`)
  30. fs.writeFileSync(indexJsonPath, JSON.stringify(indexJson, null, 2));
  31. console.log(`----------------------------`)
  32. console.log(`开始编译打包项目...`)
  33. //时间格式化
  34. function dateFormat(date) {
  35. let format = 'yyyyMMddhhmmss';
  36. let o = {
  37. "M+": date.getMonth() + 1, //month
  38. "d+": date.getDate(), //day
  39. "h+": date.getHours(), //hour
  40. "m+": date.getMinutes(), //minute
  41. "s+": date.getSeconds(), //second
  42. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  43. "S": date.getMilliseconds() //millisecond
  44. }
  45. if (/(y+)/.test(format)) {
  46. format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  47. }
  48. for (let k in o) {
  49. if (new RegExp("(" + k + ")").test(format)) {
  50. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  51. }
  52. }
  53. return format;
  54. }