diff --git a/README.md b/README.md index 5f89ad56a4..f8d0c82c07 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ CLI flags: - `--backend-port ` — set WebSocket runtime port. - `--web-port ` — set web UI port. - `--cwd ` — choose launch project directory (defaults to current directory). +- `--version` — print CLI version. - `--help` — print CLI usage. Optional environment variables: diff --git a/apps/t3/src/cli.test.ts b/apps/t3/src/cli.test.ts index 6895b04ea6..dd261e079c 100644 --- a/apps/t3/src/cli.test.ts +++ b/apps/t3/src/cli.test.ts @@ -46,6 +46,17 @@ describe("parseCliOptions", () => { expect(options.showHelp).toBe(true); }); + it("supports version flag", () => { + const options = parseCliOptions(["--version"], {}, "/workspace"); + expect(options.showVersion).toBe(true); + }); + + it("throws for invalid explicit port values", () => { + expect(() => parseCliOptions(["--web-port", "nope"], {}, "/workspace")).toThrow( + "Invalid value for --web-port", + ); + }); + it("throws for unknown arguments", () => { expect(() => parseCliOptions(["--wat"], {}, "/workspace")).toThrow( "Unknown argument: --wat", diff --git a/apps/t3/src/cli.ts b/apps/t3/src/cli.ts index 2cf3c42710..428434ec2f 100644 --- a/apps/t3/src/cli.ts +++ b/apps/t3/src/cli.ts @@ -23,12 +23,22 @@ function parsePort(value: string | undefined, fallback: number): number { return parsed; } +function parseExplicitPort(value: string, key: string): number { + const parsed = Number(value); + if (!Number.isInteger(parsed) || parsed <= 0) { + throw new Error(`Invalid value for ${key}: '${value}'. Expected a positive integer.`); + } + + return parsed; +} + interface CliOptions { backendPort: number; webPort: number; launchCwd: string; noOpen: boolean; showHelp: boolean; + showVersion: boolean; } function readArgValue(args: string[], index: number, key: string): string { @@ -50,6 +60,7 @@ export function parseCliOptions( let launchCwd = cwd; let noOpen = env.T3_NO_OPEN === "1"; let showHelp = false; + let showVersion = false; for (let index = 0; index < argv.length; index += 1) { const arg = argv[index]; @@ -60,32 +71,37 @@ export function parseCliOptions( continue; } + if (arg === "--version" || arg === "-v") { + showVersion = true; + continue; + } + if (arg === "--no-open") { noOpen = true; continue; } if (arg.startsWith("--backend-port=")) { - backendPort = parsePort(arg.split("=")[1], DEFAULT_BACKEND_PORT); + backendPort = parseExplicitPort(arg.split("=")[1] ?? "", "--backend-port"); continue; } if (arg === "--backend-port") { - backendPort = parsePort( + backendPort = parseExplicitPort( readArgValue(argv, index, "--backend-port"), - DEFAULT_BACKEND_PORT, + "--backend-port", ); index += 1; continue; } if (arg.startsWith("--web-port=")) { - webPort = parsePort(arg.split("=")[1], DEFAULT_WEB_PORT); + webPort = parseExplicitPort(arg.split("=")[1] ?? "", "--web-port"); continue; } if (arg === "--web-port") { - webPort = parsePort(readArgValue(argv, index, "--web-port"), DEFAULT_WEB_PORT); + webPort = parseExplicitPort(readArgValue(argv, index, "--web-port"), "--web-port"); index += 1; continue; } @@ -110,6 +126,7 @@ export function parseCliOptions( launchCwd, noOpen, showHelp, + showVersion, }; } @@ -123,6 +140,7 @@ function printHelp(): void { " --backend-port Override WebSocket API port (default: 4317)", " --web-port Override web UI port (default: 4318)", " --cwd Launch project directory (default: current directory)", + " -v, --version Print CLI version", " -h, --help Show this help message", "", "Environment variables:", @@ -153,6 +171,10 @@ function openBrowser(url: string, noOpen: boolean): void { child.unref(); } +function readCliVersion(): string { + return process.env.npm_package_version ?? "0.1.0"; +} + function ensureRendererBuild(rendererRoot: string): void { const distPath = path.join(rendererRoot, "dist", "index.html"); if (fs.existsSync(distPath)) { @@ -247,6 +269,12 @@ async function main() { return; } + if (options.showVersion) { + process.stdout.write(`${readCliVersion()}\n`); + process.exit(0); + return; + } + const runtimeServer = await startRuntimeApiServer({ port: options.backendPort, launchCwd: options.launchCwd,