config.js 3.3 KB

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