/** * Generates `src/internal/proxyProcessSource.ts`, the source text of the * proxy process program that the default launcher runs with `node -e`, from * `src/internal/proxyProcessWorker.ts`. Run by `pnpm codegen`. With * `--check`, exits with code 1 if the generated file is out of date. */ import * as Fs from "node:fs" import * as Module from "node:module" const worker = new URL("../src/internal/proxyProcessWorker.ts", import.meta.url) const output = new URL("../src/internal/proxyProcessSource.ts", import.meta.url) // The source text of the program, as an ES module that calls `run`. The type // stripper replaces types with spaces, which are kept, so that generating the // program changes nothing else in it, such as a string literal. const program = Module.stripTypeScriptTypes(Fs.readFileSync(worker, "utf8")) + "\nrun()\n" const escape = (text: string) => text.replace(/[\\`]|\$\{/g, (match) => "\\" + match) if (process.argv.includes("--check")) { const { source } = await import(output.href) if (source !== program) { process.stderr.write("src/internal/proxyProcessSource.ts is out of date; run pnpm codegen\n") process.exitCode = 1 } } else { Fs.writeFileSync( output, `// Generated by scripts/generateProxyProcessSource.ts from proxyProcessWorker.ts. Do not edit. /** @internal */ export const source = \`${escape(program)}\` ` ) }