run-cli-lint.mjs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env node
  2. import { execFile } from "node:child_process";
  3. import { promisify } from "node:util";
  4. import { readdir, readFile } from "node:fs/promises";
  5. import { dirname, resolve, relative } from "node:path";
  6. import { fileURLToPath } from "node:url";
  7. import { loadPolicy } from "./architecture/policy.mjs";
  8. import { pristineUpstreamFiles } from "./architecture/upstream-admission.mjs";
  9. import { classifyLintDiagnostics, parseLintCommand } from "./cli-lint-admission.mjs";
  10. const exec = promisify(execFile);
  11. const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  12. const cli = resolve(root, "apps/zcode-cli");
  13. async function main() {
  14. const policy = await loadPolicy(root);
  15. const pristine = await pristineUpstreamFiles(
  16. root,
  17. policy.global.acceptedUpstreamCommits,
  18. policy.global.acceptedUpstreamSnapshot,
  19. );
  20. let errors = 0,
  21. warnings = 0,
  22. accepted = 0;
  23. for (const parent of ["packages", "tools"]) {
  24. const entries = await readdir(resolve(cli, parent), { withFileTypes: true });
  25. for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
  26. if (!entry.isDirectory()) continue;
  27. const cwd = resolve(cli, parent, entry.name);
  28. let manifest;
  29. try {
  30. manifest = JSON.parse(await readFile(resolve(cwd, "package.json"), "utf8"));
  31. } catch (error) {
  32. if (error.code === "ENOENT") continue;
  33. throw error;
  34. }
  35. if (!manifest.scripts?.lint) continue;
  36. const args = [...parseLintCommand(manifest.scripts.lint), "--format", "json"];
  37. // 沿用各包解析出的 oxlint 版本、cwd 和原参数;包级配置仍由 oxlint 自己读取。
  38. const pnpm = process.env.npm_execpath;
  39. if (!pnpm) throw new Error("请通过 pnpm lint 运行 CLI lint");
  40. let result;
  41. try {
  42. result = await exec(process.execPath, [pnpm, "exec", "oxlint", ...args], {
  43. cwd,
  44. maxBuffer: 16 * 1024 * 1024,
  45. });
  46. } catch (error) {
  47. if (error.code !== 1 || !error.stdout) throw error;
  48. result = error;
  49. }
  50. const report = JSON.parse(result.stdout);
  51. if (!Array.isArray(report.diagnostics))
  52. throw new Error(`${manifest.name}: 无法解析 oxlint 结果`);
  53. const groups = classifyLintDiagnostics(report.diagnostics, {
  54. pristine,
  55. relativeDirectory: relative(root, cwd).replaceAll("\\", "/"),
  56. });
  57. // 非诊断性失败不能被“零错误”误判为成功。
  58. if (result.code === 1 && !report.diagnostics.some((item) => item.severity === "error"))
  59. throw new Error(`${manifest.name}: oxlint 异常退出:${result.stderr}`);
  60. for (const item of [...groups.errors, ...groups.warnings])
  61. console.log(
  62. `${manifest.name}: ${item.severity} ${item.filename} ${item.code}: ${item.message}`,
  63. );
  64. if (groups.accepted.length)
  65. console.log(`${manifest.name}: 接纳 ${groups.accepted.length} 项原样上游 max-lines 存量`);
  66. errors += groups.errors.length;
  67. warnings += groups.warnings.length;
  68. accepted += groups.accepted.length;
  69. }
  70. }
  71. console.log(
  72. `CLI lint: ${errors} errors, ${warnings} warnings, ${accepted} pristine upstream line-count diagnostics`,
  73. );
  74. return errors ? 1 : 0;
  75. }
  76. main()
  77. .then((code) => {
  78. process.exitCode = code;
  79. })
  80. .catch((error) => {
  81. console.error(error);
  82. process.exitCode = 1;
  83. });