verify-generated-runtime.mjs 1.4 KB

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