8
0

config.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. indexJson.smsPhone = ""; //短信接口手机号
  44. indexJson.vite = {}; //vite配置
  45. //更新配置文件
  46. console.log(`修改配置文件为生产环境的配置...`)
  47. fs.writeFileSync(indexJsonPath, JSON.stringify(indexJson, null, 2));
  48. //时间格式化
  49. function dateFormat(date) {
  50. let format = 'yyyyMMddhhmmss';
  51. let o = {
  52. "M+": date.getMonth() + 1, //month
  53. "d+": date.getDate(), //day
  54. "h+": date.getHours(), //hour
  55. "m+": date.getMinutes(), //minute
  56. "s+": date.getSeconds(), //second
  57. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  58. "S": date.getMilliseconds() //millisecond
  59. }
  60. if (/(y+)/.test(format)) {
  61. format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  62. }
  63. for (let k in o) {
  64. if (new RegExp("(" + k + ")").test(format)) {
  65. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  66. }
  67. }
  68. return format;
  69. }