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