build.js 2.8 KB

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