verify-generated-runtime.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env node
  2. import { execFile } from "node:child_process";
  3. import { promisify } from "node:util";
  4. const execFileAsync = promisify(execFile);
  5. const cwd = process.cwd();
  6. const runtimePaths = ["dist/cli.js", "dist/config.js"];
  7. async function compileRuntime() {
  8. try {
  9. await execFileAsync("npm", ["run", "compile", "--silent"], {
  10. cwd,
  11. maxBuffer: 10 * 1024 * 1024,
  12. });
  13. } catch (error) {
  14. const code = typeof error === "object" && error !== null && "code" in error
  15. ? String(error.code)
  16. : "unknown";
  17. throw new Error(`runtime compilation failed with exit code ${code}`);
  18. }
  19. }
  20. async function generatedRuntimeDrift() {
  21. const [{ stdout: changed }, { stdout: untracked }] = await Promise.all([
  22. execFileAsync("git", ["diff", "--name-status", "--", ...runtimePaths], { cwd }),
  23. execFileAsync(
  24. "git",
  25. ["ls-files", "--others", "--exclude-standard", "--", ...runtimePaths],
  26. { cwd },
  27. ),
  28. ]);
  29. return [changed.trim(), untracked.trim()].filter(Boolean).join("\n");
  30. }
  31. async function verify() {
  32. await compileRuntime();
  33. const drift = await generatedRuntimeDrift();
  34. if (drift !== "") {
  35. throw new Error(`generated runtime drift detected:\n${drift}`);
  36. }
  37. process.stdout.write("Generated runtime is fresh.\n");
  38. }
  39. verify().catch((error) => {
  40. const message = error instanceof Error ? error.message : String(error);
  41. process.stderr.write(`Generated-runtime verification failed: ${message}\n`);
  42. process.exitCode = 1;
  43. });