prebuild.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { writeFileSync, existsSync } 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 (!existsSync("./src/devLoginParams.ts")) {
  45. writeFileSync(
  46. "./src/devLoginParams.ts",
  47. `
  48. export const isAdmin = false;
  49. export let forceChange = false;
  50. forceChange = true;
  51. // 很多任务,选分组2
  52. export const loginName = "";
  53. export const password = "";
  54. export const examId = "";
  55. export const markerId = "";
  56. `
  57. );
  58. }
  59. console.log();