run-lint-staged-vitest.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env node
  2. import { spawnSync } from "node:child_process";
  3. import { existsSync, readFileSync, rmSync } from "node:fs";
  4. import { delimiter, join } from "node:path";
  5. const ROOT_UNIT_E2E_GLOB = "packages/desktop/test/e2e/**/*.test.ts";
  6. function resolvePnpmCliPath(env) {
  7. const npmExecPath = env.npm_execpath;
  8. if (
  9. npmExecPath &&
  10. (npmExecPath.toLowerCase().includes("\\pnpm\\") ||
  11. npmExecPath.toLowerCase().includes("/pnpm/"))
  12. ) {
  13. return npmExecPath;
  14. }
  15. const pathEntries = (env.PATH ?? env.Path ?? "")
  16. .split(delimiter)
  17. .map((entry) => entry.trim())
  18. .filter(Boolean);
  19. for (const pathEntry of pathEntries) {
  20. const candidate = join(pathEntry, "node_modules", "pnpm", "bin", "pnpm.cjs");
  21. if (existsSync(candidate)) {
  22. return candidate;
  23. }
  24. }
  25. return undefined;
  26. }
  27. function spawnPnpm(args, options) {
  28. // Bugfix: Windows 下不能把文件参数拼成 cmd.exe 字符串;%PATH% 这类合法文件名
  29. // 会被 shell 展开。pre-push 专用 runner 在 Windows 下直接走 pnpm JS 入口,保持 argv 原样传递。
  30. if (process.platform !== "win32") {
  31. return spawnSync("pnpm", args, options);
  32. }
  33. const cliPath = resolvePnpmCliPath(options.env ?? process.env);
  34. if (cliPath) {
  35. return spawnSync(process.execPath, [cliPath, ...args], options);
  36. }
  37. return spawnSync("pnpm", args, { ...options, shell: true });
  38. }
  39. function collectGitLocalEnvNames() {
  40. const result = spawnSync("git", ["rev-parse", "--local-env-vars"], {
  41. encoding: "utf8",
  42. stdio: ["ignore", "pipe", "ignore"],
  43. });
  44. if (result.status !== 0) {
  45. return ["GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE"];
  46. }
  47. return result.stdout
  48. .split("\n")
  49. .map((line) => line.trim())
  50. .filter(Boolean);
  51. }
  52. function createCleanGitEnv() {
  53. const env = { ...process.env };
  54. // lint-staged 在 pre-push 路径下使用临时 GIT_INDEX_FILE 保护真实 index。
  55. // Vitest 及单测中的临时 Git 仓库不应继承这些 local env,否则会误命中当前仓库。
  56. for (const name of collectGitLocalEnvNames()) {
  57. delete env[name];
  58. }
  59. return env;
  60. }
  61. function readFilesFromManifest(mode, manifestPath) {
  62. try {
  63. const parsed = JSON.parse(readFileSync(manifestPath, "utf8"));
  64. if (
  65. !parsed ||
  66. parsed.mode !== mode ||
  67. !Array.isArray(parsed.files) ||
  68. !parsed.files.every((file) => typeof file === "string")
  69. ) {
  70. throw new Error("invalid manifest shape");
  71. }
  72. return parsed.files;
  73. } finally {
  74. rmSync(manifestPath, { force: true });
  75. }
  76. }
  77. function parseArgs(argv) {
  78. const [mode, ...rest] = argv;
  79. if (rest[0] === "--files-manifest") {
  80. const manifestPath = rest[1];
  81. if (!manifestPath) {
  82. throw new Error("--files-manifest requires a path");
  83. }
  84. return {
  85. mode,
  86. files: readFilesFromManifest(mode, manifestPath),
  87. };
  88. }
  89. return { mode, files: rest };
  90. }
  91. const { mode, files } = parseArgs(process.argv.slice(2));
  92. if (mode !== "run" && mode !== "related") {
  93. console.error(
  94. "Usage: node scripts/run-lint-staged-vitest.mjs <run|related> [--files-manifest <path>|<files...>]",
  95. );
  96. process.exit(1);
  97. }
  98. if (files.length === 0) {
  99. process.exit(0);
  100. }
  101. const vitestArgs =
  102. mode === "run"
  103. ? ["exec", "vitest", "run", "--exclude", ROOT_UNIT_E2E_GLOB, ...files]
  104. : [
  105. "exec",
  106. "vitest",
  107. "related",
  108. "--run",
  109. "--passWithNoTests",
  110. "--exclude",
  111. ROOT_UNIT_E2E_GLOB,
  112. ...files,
  113. ];
  114. const result = spawnPnpm(vitestArgs, {
  115. env: createCleanGitEnv(),
  116. stdio: "inherit",
  117. });
  118. if (result.error) {
  119. console.error(
  120. `[lint-staged vitest] failed to spawn pnpm: ${result.error.message}`,
  121. );
  122. }
  123. process.exitCode = result.status ?? 1;