build.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const path = require('path');
  2. const fs = require('fs');
  3. // 获取当前命令行上下文路径
  4. const currentDirectory = process.cwd();
  5. //删除上次打包相关的文件
  6. const distZipPath = path.join(currentDirectory, '/zip/client.zip');
  7. if(fs.existsSync(distZipPath)) {
  8. fs.unlinkSync(distZipPath);
  9. }
  10. // 获取配置文件
  11. const indexJsonPath = path.join(currentDirectory, 'src/config/index.json');
  12. const indexJsonContent = fs.readFileSync(indexJsonPath, 'utf8');
  13. //创建缓存文件
  14. const cacheJsonPath = path.join(currentDirectory, 'scripts/cache.json');
  15. fs.writeFileSync(cacheJsonPath, indexJsonContent, 'utf8');
  16. //修改配置文件
  17. const indexJson = JSON.parse(indexJsonContent);
  18. indexJson.version = dateFormat(new Date()); //版本号
  19. indexJson.target = "http://127.0.0.1:8090"; //接口地址
  20. indexJson.smsPhone = ""; //短信接口手机号
  21. indexJson.vite = {}; //vite配置
  22. //更新配置文件
  23. fs.writeFileSync(indexJsonPath, JSON.stringify(indexJson, null, 2));
  24. //时间格式化
  25. function dateFormat(date) {
  26. let format = 'yyyyMMddhhmmss';
  27. let o = {
  28. "M+": date.getMonth() + 1, //month
  29. "d+": date.getDate(), //day
  30. "h+": date.getHours(), //hour
  31. "m+": date.getMinutes(), //minute
  32. "s+": date.getSeconds(), //second
  33. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  34. "S": date.getMilliseconds() //millisecond
  35. }
  36. if (/(y+)/.test(format)) {
  37. format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  38. }
  39. for (let k in o) {
  40. if (new RegExp("(" + k + ")").test(format)) {
  41. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  42. }
  43. }
  44. return format;
  45. }