runCli
uses node:fs
Process entry point: run a command, report errors, set the exit code.
import { serveChecks, TCM_COMMAND } from "#codebase/tcmts/cli/checkers"; import { runCommand } from "#codebase/tcmts/cli/main"; import { serveWorker } from "#codebase/tcmts/cli/mcp"; import fs from "node:fs"; import path from "node:path"; export async function runCli(...argv: string[]): Promise<void> { // measured (a suite runs tcm to see what code it runs), a tcm stopped by a signal (a test ending the language // server it started) still says what it ran: V8 writes coverage on exit, not when a signal ends the process if (process.env.NODE_V8_COVERAGE) { for (const [signal, n] of [ ["SIGTERM", 15], ["SIGINT", 2], ] as const) process.on(signal, () => process.exit(128 + n)); // and says it ran, so the suite waits for it to end (and write what it ran) before reading what ran, even when // the test that started it didn't wait try { // (V8 makes the directory only as it writes, on exit) fs.mkdirSync(process.env.NODE_V8_COVERAGE, { recursive: true }); fs.writeFileSync(path.join(process.env.NODE_V8_COVERAGE, `tcm-pid-${process.pid}`), ""); } catch { // (one it can't write in: nothing it can say) } } // the MCP server's worker, which runs its tools' commands in this one process (not a command anyone types) if (argv[0] === "internal-worker") return serveWorker(runCommand); // a checker process, among which a sync shares checking what it receives (not a command anyone types either) if (argv[0] === "internal-check") return serveChecks(); // the command that runs this tcm, for those checker processes: where this is the process's own command (not code // calling it with arguments of its own), without what profiles or debugs it if (argv.join("\0") === process.argv.slice(2).join("\0") && process.argv[1]) { const flags: string[] = []; const given = process.execArgv; for (let i = 0; i < given.length; i++) { if (/^--(inspect|cpu-prof|heap-prof)/.test(given[i]!)) { if (!given[i]!.includes("=") && given[i + 1] && !given[i + 1]!.startsWith("-")) i++; continue; } flags.push(given[i]!); } (globalThis as unknown as Record<symbol, unknown>)[TCM_COMMAND] = [ process.execPath, ...flags, process.argv[1], ]; } const code = await runCommand(argv); if (code >= 0) process.exitCode = code; }