From e2a8f16e9efd51a50a5ecacc41e5a1c53e249461 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 9 Feb 2026 22:32:39 +0000 Subject: [PATCH] Harden CLI startup cleanup and error propagation Co-authored-by: Theo Browne --- apps/t3/src/cli.ts | 81 ++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 27 deletions(-) diff --git a/apps/t3/src/cli.ts b/apps/t3/src/cli.ts index 428434ec2f..88629df982 100644 --- a/apps/t3/src/cli.ts +++ b/apps/t3/src/cli.ts @@ -175,6 +175,52 @@ function readCliVersion(): string { return process.env.npm_package_version ?? "0.1.0"; } +async function runCli(options: CliOptions): Promise { + const runtimeServer = await startRuntimeApiServer({ + port: options.backendPort, + launchCwd: options.launchCwd, + }); + + const __dirname = path.dirname(fileURLToPath(import.meta.url)); + const rendererRoot = path.resolve(__dirname, "../../renderer"); + ensureRendererBuild(rendererRoot); + + let staticServer: + | { + close: () => Promise; + } + | undefined; + try { + staticServer = await startStaticWebServer(path.join(rendererRoot, "dist"), options.webPort); + } catch (error) { + await runtimeServer.close(); + throw error; + } + + const wsParam = encodeURIComponent(runtimeServer.wsUrl); + const appUrl = `http://127.0.0.1:${options.webPort}?ws=${wsParam}`; + openBrowser(appUrl, options.noOpen); + + process.stdout.write(`CodeThing is running at ${appUrl}\n`); + + let shutdownStarted = false; + const shutdown = async () => { + if (shutdownStarted) { + return; + } + shutdownStarted = true; + await Promise.all([staticServer.close(), runtimeServer.close()]); + process.exit(0); + }; + + process.on("SIGINT", () => { + void shutdown(); + }); + process.on("SIGTERM", () => { + void shutdown(); + }); +} + function ensureRendererBuild(rendererRoot: string): void { const distPath = path.join(rendererRoot, "dist", "index.html"); if (fs.existsSync(distPath)) { @@ -275,33 +321,14 @@ async function main() { return; } - const runtimeServer = await startRuntimeApiServer({ - port: options.backendPort, - launchCwd: options.launchCwd, - }); - - const __dirname = path.dirname(fileURLToPath(import.meta.url)); - const rendererRoot = path.resolve(__dirname, "../../renderer"); - ensureRendererBuild(rendererRoot); - const staticServer = await startStaticWebServer(path.join(rendererRoot, "dist"), options.webPort); - - const wsParam = encodeURIComponent(runtimeServer.wsUrl); - const appUrl = `http://127.0.0.1:${options.webPort}?ws=${wsParam}`; - openBrowser(appUrl, options.noOpen); - - process.stdout.write(`CodeThing is running at ${appUrl}\n`); - - const shutdown = async () => { - await Promise.all([staticServer.close(), runtimeServer.close()]); - process.exit(0); - }; - - process.on("SIGINT", () => { - void shutdown(); - }); - process.on("SIGTERM", () => { - void shutdown(); - }); + try { + await runCli(options); + } catch (error) { + process.stderr.write( + `${error instanceof Error ? error.message : "Failed to start t3 runtime."}\n`, + ); + process.exit(1); + } } const entrypoint = process.argv[1];