deploy.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * 打包命令
  3. * node deploy 环境名
  4. * 运行仅支持单环境
  5. */
  6. // 压缩插件
  7. const compressing = require("compressing");
  8. const dayjs = require("dayjs");
  9. const fs = require("fs");
  10. const colors = require("colors");
  11. const path = require("path"); //解析需要遍历的文件夹
  12. const { spawnSync } = require("child_process");
  13. const { envList } = require("./env.js");
  14. // 运行文件夹
  15. const cmdPath = `${path.resolve("./")}/node_modules/.bin`;
  16. // 打包文件路径
  17. let vueCliService = "";
  18. if (process.platform.includes("win")) {
  19. vueCliService = `${cmdPath}/vue-cli-service.cmd`;
  20. } else if (process.platform.includes("linux")) {
  21. vueCliService = `${cmdPath}/vue-cli-service`;
  22. } else if (process.platform.includes("mac")) {
  23. vueCliService = `${cmdPath}/vue-cli-service.ps1`;
  24. }
  25. // 获取参数列表
  26. let envKeyList = process.argv.splice(2);
  27. // 当前平台
  28. let nowPlatform = "";
  29. // 当前环境
  30. let nowEnvObj = {};
  31. // 运行类型
  32. let type = "build";
  33. // 打包列表
  34. let nextEnvKeyList = [];
  35. console.log(colors.yellow("------------开始" + JSON.stringify(envKeyList)));
  36. nextEnvKeyList = envKeyList.map((elem) => elem);
  37. nextEnv();
  38. /**
  39. * 执行下一条
  40. */
  41. async function nextEnv() {
  42. if (nextEnvKeyList.length) {
  43. await runSerivce(nextEnvKeyList[0]);
  44. }
  45. }
  46. /**
  47. * 执行 npm run build/serve 命令
  48. */
  49. async function runSerivce(envKey) {
  50. nowPlatform = envKey;
  51. nowEnvObj = envList[envKey] || {};
  52. if (!nowEnvObj?.VUE_APP_ENV) {
  53. console.log(colors.red("不存在该环境"));
  54. return;
  55. }
  56. console.log(colors.yellow("执行参数", envKey, JSON.stringify(nowEnvObj)));
  57. Object.assign(process.env, nowEnvObj);
  58. console.log(colors.blue("开始运行...", `${vueCliService} ${type}`));
  59. await build();
  60. }
  61. /**
  62. * 打包1
  63. */
  64. async function build() {
  65. // 同步执行
  66. try {
  67. await spawnSync(vueCliService, [type]);
  68. // 成功
  69. console.log(colors.yellow("清理多余文件_mini"));
  70. nextEnvKeyList.shift();
  71. await miniDist();
  72. // await addZip()
  73. } catch (err) {
  74. console.log(colors.red("运行失败", err));
  75. }
  76. }
  77. /**
  78. * 删除不需要的文件
  79. */
  80. async function filterFileList(src, dist) {
  81. let docs = fs.readdirSync(src);
  82. let minDirList = ["resource", "platform"];
  83. let envKeys = Object.keys(envList).filter(
  84. (key) => key != nowEnvObj.VUE_APP_ENV
  85. );
  86. for (let index = 0; index < docs.length; index++) {
  87. const doc = docs[index];
  88. let _src = src + "/" + doc,
  89. _dist = dist + "/" + doc;
  90. let st = fs.statSync(_src);
  91. // 判断是否为文件
  92. if (st.isFile() && doc !== ".DS_Store") {
  93. // 常规打包
  94. if (
  95. minDirList.some((elem) => _src.indexOf(`/${elem}/`) > -1) &&
  96. envKeys.some((elem) => _dist.indexOf(`/${elem}/`) > -1)
  97. ) {
  98. // 删除其他平台相关文件
  99. fs.unlinkSync(_src);
  100. }
  101. } else if (st.isDirectory()) {
  102. // 如果是目录则递归调用自身
  103. if (
  104. minDirList.some((elem) => _dist.indexOf(`/${elem}/`) > -1) &&
  105. envKeys.some((elem) => _dist == `/platform/${elem}`)
  106. ) {
  107. // 删除其他平台相关文件夹
  108. fs.rmSync(_src, { recursive: true });
  109. } else {
  110. await filterFileList(_src, _dist);
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * 压缩软件
  117. */
  118. async function addZip() {
  119. await wait(2000);
  120. await filterFileList(path.resolve("dist"), "");
  121. let zipName = `./Echo_${nowPlatform}_${dayjs().format("YYYYMMDDHHmmss")}.zip`;
  122. await compressing.zip.compressDir("dist/", zipName);
  123. console.log(colors.green("打包成功"));
  124. }
  125. /**
  126. * 过滤文件
  127. */
  128. async function miniDist() {
  129. await wait(2000);
  130. await filterFileList(path.resolve("dist"), "");
  131. console.log(
  132. colors.green(`打包成功_${nowPlatform}_${dayjs().format("YYYYMMDDHHmmss")}`)
  133. );
  134. }
  135. // 函数实现,参数单位 毫秒 ;
  136. function wait(ms) {
  137. return new Promise((resolve) => setTimeout(() => resolve(), ms));
  138. }