prebuild.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { writeFileSync, existsSync, readFileSync } from "fs";
  2. import { execSync } from "child_process";
  3. import moment from "moment";
  4. import packageJson from "./package.json" assert { type: "json" };
  5. import * as readline from "node:readline/promises";
  6. import { stdin as input, stdout as output } from "node:process";
  7. console.log("> prebuild");
  8. console.log("> prebuild create .env.*.local");
  9. // const packageJson = require("./package.json");
  10. const buildDate = moment().format("YYYY-MM-DD HH:mm:ss");
  11. console.log(" 构建日期为 " + buildDate);
  12. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  13. const revision = execSync("git rev-parse HEAD").toString().trim().slice(0, 7);
  14. console.log(" 当前的git版本为 " + revision);
  15. const branchName = execSync("git branch --show-current").toString().trim();
  16. const branchNum = branchName.match(/.*(\d+\.\d+.\d+)$/)[1];
  17. if (branchNum !== packageJson.version) {
  18. const rl = readline.createInterface({ input, output });
  19. /** @type string */
  20. const answer = await rl.question(
  21. "package.json的版本号与git分支不一致,是否停止构建?(Y/N)"
  22. );
  23. console.log(`${answer}`);
  24. rl.close();
  25. const upAnswer = answer.toUpperCase();
  26. if (upAnswer === "Y") {
  27. process.exit(1);
  28. } else if (upAnswer === "N") {
  29. console.log("继续构建");
  30. } else {
  31. console.error("无效输入");
  32. process.exit(1);
  33. }
  34. }
  35. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  36. writeFileSync(
  37. ".env.production.local",
  38. `VITE_FE_VERSION=${packageJson.version}
  39. VITE_BUILD_DATE=${buildDate}
  40. VITE_GIT_REPO_VERSION=${revision}
  41. `
  42. );
  43. // 如果不存在则创建,对真正的构建无影响
  44. // if (
  45. // !existsSync("./src/devLoginParams.ts") &&
  46. // !readFileSync("./src/devLoginParams.ts").toString().includes("LOGIN_CONFIG")
  47. // ) {
  48. // writeFileSync(
  49. // "./src/devLoginParams.ts",
  50. // `
  51. // export const LOGIN_CONFIG = {
  52. // isAdmin: false,
  53. // forceChange: true,
  54. // loginName: "1-431-2-1",
  55. // password: "123456",
  56. // examId: "1",
  57. // markerId: "451",
  58. // };
  59. // `
  60. // );
  61. // }
  62. console.log();