build.js 1.6 KB

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