build.js 2.1 KB

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