build.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/analysis.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.version = dateFormat(new Date()); //版本号
  39. indexJson.target = "http://127.0.0.1:8014"; //接口地址
  40. indexJson.vite = {}; //vite配置
  41. //更新配置文件
  42. console.log(`修改配置文件为生产环境的配置...`)
  43. fs.writeFileSync(indexJsonPath, JSON.stringify(indexJson, null, 2));
  44. //时间格式化
  45. function dateFormat(date) {
  46. let format = 'yyyyMMddhhmmss';
  47. let o = {
  48. "M+": date.getMonth() + 1, //month
  49. "d+": date.getDate(), //day
  50. "h+": date.getHours(), //hour
  51. "m+": date.getMinutes(), //minute
  52. "s+": date.getSeconds(), //second
  53. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  54. "S": date.getMilliseconds() //millisecond
  55. }
  56. if (/(y+)/.test(format)) {
  57. format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  58. }
  59. for (let k in o) {
  60. if (new RegExp("(" + k + ")").test(format)) {
  61. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  62. }
  63. }
  64. return format;
  65. }