diff --git a/src/scripts/verifyEnv.ts b/src/scripts/verifyEnv.ts new file mode 100644 index 0000000..59ad8fc --- /dev/null +++ b/src/scripts/verifyEnv.ts @@ -0,0 +1,37 @@ +import { $, sleep, spawn } from "bun"; +import { readFile } from "fs/promises"; + +import { logStep } from "~/libs/logStep"; + +await $`cp src/types/env.d.ts /tmp/env.d.ts`.quiet(); + +await logStep( + 'Generating "env.d.ts"', + () => import("./generateEnv"), + "Generated env.d.ts", +); + +await logStep("Comparing env.d.ts", async () => { + function filterComments(content: Buffer) { + return content + .toString() + .split("\n") + .filter((line) => !line.trim().startsWith("//")) + .join("\n"); + } + + const currentFileContent = filterComments(await readFile("/tmp/env.d.ts")); + const generatedFileContent = filterComments( + await readFile("src/types/env.d.ts"), + ); + + if (currentFileContent === generatedFileContent) { + console.log("env.d.ts is up to date"); + return; + } + + spawn({ cmd: ["sl", "diff", "src/types/env.d.ts"], stdout: "inherit" }); + // add 1 second to make sure spawn completes + await sleep(1000); + throw new Error("env.d.ts is out of date"); +});