config.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const path = require('path');
  2. const fs = require('fs');
  3. const buildName = process.argv[2] //打包后的名字
  4. // 获取当前命令行上下文路径
  5. const currentDirectory = process.cwd();
  6. console.log(`----------------------------`)
  7. console.log(`正在处理编译打包前的准备...`)
  8. //修改版本更新信息
  9. console.log(`更新版本更新信息...`)
  10. const versionPath = path.join(currentDirectory, '/public/version.json');
  11. const versionContent = fs.readFileSync(versionPath, 'utf8');
  12. //修改配置文件
  13. const versionJson = JSON.parse(versionContent);
  14. versionJson.value = dateFormat(new Date()); //版本号
  15. //更新版本更新信息
  16. fs.writeFileSync(versionPath, JSON.stringify(versionJson, null, 2));
  17. console.log(`----------------------------`)
  18. //删除上次打包相关的文件
  19. console.log(`正在删除上次打包相关的文件...`)
  20. const distZipPath = path.join(currentDirectory, `/zip/${buildName}`);
  21. if(fs.existsSync(distZipPath)) {
  22. fs.unlinkSync(distZipPath);
  23. }
  24. // 获取配置文件
  25. console.log(`获取当前的配置文件...`)
  26. const indexJsonPath = path.join(currentDirectory, 'src/config/index.json');
  27. const indexJsonContent = fs.readFileSync(indexJsonPath, 'utf8');
  28. // 检测上次打包异常中断的缓存文件是否存在
  29. console.log(`检测上次打包异常中断的缓存文件是否存在...`)
  30. const cacheFilePath = path.join(currentDirectory, 'scripts/cache.json');
  31. if(!fs.existsSync(cacheFilePath)) {
  32. //创建缓存文件
  33. console.log(`创建配置缓存文件...`)
  34. const cacheJsonPath = path.join(currentDirectory, 'scripts/cache.json');
  35. fs.writeFileSync(cacheJsonPath, indexJsonContent, 'utf8');
  36. }
  37. //修改配置文件
  38. const indexJson = JSON.parse(indexJsonContent);
  39. indexJson.version = dateFormat(new Date()); //版本号
  40. indexJson.target = "http://127.0.0.1:8090"; //接口地址
  41. indexJson.smsPhone = ""; //短信接口手机号
  42. indexJson.vite = {}; //vite配置
  43. //更新配置文件
  44. console.log(`修改配置文件为生产环境的配置...`)
  45. fs.writeFileSync(indexJsonPath, JSON.stringify(indexJson, null, 2));
  46. //时间格式化
  47. function dateFormat(date) {
  48. let format = 'yyyyMMddhhmmss';
  49. let o = {
  50. "M+": date.getMonth() + 1, //month
  51. "d+": date.getDate(), //day
  52. "h+": date.getHours(), //hour
  53. "m+": date.getMinutes(), //minute
  54. "s+": date.getSeconds(), //second
  55. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  56. "S": date.getMilliseconds() //millisecond
  57. }
  58. if (/(y+)/.test(format)) {
  59. format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  60. }
  61. for (let k in o) {
  62. if (new RegExp("(" + k + ")").test(format)) {
  63. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  64. }
  65. }
  66. return format;
  67. }