Files
scriptc/packages/cli/src/main.ts
T
Malte UblandClaude Opus 5.5 7675b7d653 Add scriptc types patch and scriptc types list
`scriptc types patch <package> <selector> <type> [--range] [--dry-run]`
finds the installed package from the working directory, shows every
declaration the selector matches (declaration files, JSDoc, @types
package) with its type before and after, and adds the patch to the
nearest package.json: into an entry whose range covers the installed
version, or under ^<version>. Nothing is written when the selector
matches nothing. `scriptc types list` shows each patch and what it
matches. Both CLI hosts run the same implementation with their own
parser. The TypeError note for a wrong declaration now names the command.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 16:00:29 -07:00

64 lines
2.0 KiB
TypeScript

import { spawn } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import {
analyzeCached,
collectGarbageUnderHeapPressure,
compile,
compileLibrary,
mergeProfiles,
resolveProvenanceSources,
sourceTargetPlatform,
typePatches,
warmNativeCaches,
} from "@scriptc/compiler";
import { runCli } from "./command.js";
/** The version of the installed package. Read from the manifest rather than
* baked in by the build, so a stamped release and a source checkout answer
* the same way. */
function version(): string {
const here = dirname(fileURLToPath(import.meta.url));
// dist/main.js at install time, src/main.ts under tsx: the manifest is
// one level up from either.
const manifest = JSON.parse(readFileSync(join(here, "..", "package.json"), "utf8")) as {
version?: string;
};
return manifest.version ?? "unknown";
}
// The compiler process collects its heap as lowering and LLVM emission grow it.
collectGarbageUnderHeapPressure();
process.exitCode = await runCli(process.argv.slice(2), {
version,
analyze: analyzeCached,
compile,
compileLibrary,
resolveProvenanceSources,
sourceTargetPlatform,
warmNativeCaches,
mergeProfiles,
typePatches,
run: (binary) =>
new Promise<number>((resolveExit) => {
let child;
if (sourceTargetPlatform() === "wasi") {
const builtRunner = fileURLToPath(new URL("./wasi-runner.js", import.meta.url));
const runner = existsSync(builtRunner)
? builtRunner
: fileURLToPath(new URL("./wasi-runner.ts", import.meta.url));
child = spawn(process.execPath, [...process.execArgv, "--no-warnings", runner, binary], {
stdio: "inherit",
});
} else child = spawn(binary, [], { stdio: "inherit" });
child.on("exit", (code, signal) => {
if (signal) {
process.stderr.write(`scriptc: program killed by ${signal}\n`);
resolveExit(1);
} else resolveExit(code ?? 0);
});
}),
});