mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-04 03:35:36 +08:00
44 lines
1.7 KiB
TypeScript
44 lines
1.7 KiB
TypeScript
/**
|
|
* 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)
|
|
|
|
// Node's type stripper blanks types with spaces at the same offsets. Drop the
|
|
// lines that only held types, and the blank line after them, leaving inline
|
|
// separators, comments, and string literals untouched.
|
|
const stripTypes = (source: string): string => {
|
|
const original = source.split("\n")
|
|
const stripped = Module.stripTypeScriptTypes(source).split("\n")
|
|
const erased = (i: number) => stripped[i].trim() === "" && original[i].trim() !== ""
|
|
return stripped.filter((line, i) => !erased(i) && !(line === "" && i > 0 && erased(i - 1))).join("\n")
|
|
}
|
|
|
|
// The source text of the program, as an ES module that calls `run`.
|
|
const program = stripTypes(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)}\`
|
|
`
|
|
)
|
|
}
|