From e6540e7f2d4e1b63a1b26ef54c0678379fb1da90 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 9 Feb 2026 22:45:13 +0000 Subject: [PATCH] Resolve CLI version from package metadata with tests Co-authored-by: Theo Browne --- apps/t3/src/cli.test.ts | 26 +++++++++++++++++++++++++- apps/t3/src/cli.ts | 29 +++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/apps/t3/src/cli.test.ts b/apps/t3/src/cli.test.ts index dd261e079c..513436d4c8 100644 --- a/apps/t3/src/cli.test.ts +++ b/apps/t3/src/cli.test.ts @@ -1,7 +1,9 @@ +import { mkdtempSync, writeFileSync } from "node:fs"; +import os from "node:os"; import path from "node:path"; import { describe, expect, it } from "vitest"; -import { parseCliOptions } from "./cli"; +import { parseCliOptions, readCliVersion } from "./cli"; describe("parseCliOptions", () => { it("reads defaults from environment variables", () => { @@ -63,3 +65,25 @@ describe("parseCliOptions", () => { ); }); }); + +describe("readCliVersion", () => { + it("prefers npm_package_version from environment", () => { + const value = readCliVersion("/tmp/does-not-matter.json", { + npm_package_version: "9.9.9", + }); + expect(value).toBe("9.9.9"); + }); + + it("falls back to package json version when env is missing", () => { + const tempDir = mkdtempSync(path.join(os.tmpdir(), "t3-version-test-")); + const packageJsonPath = path.join(tempDir, "package.json"); + writeFileSync(packageJsonPath, JSON.stringify({ version: "1.2.3" }), "utf8"); + const value = readCliVersion(packageJsonPath, {}); + expect(value).toBe("1.2.3"); + }); + + it("returns default when env and package file are unavailable", () => { + const value = readCliVersion("/tmp/no-such-package.json", {}); + expect(value).toBe("0.1.0"); + }); +}); diff --git a/apps/t3/src/cli.ts b/apps/t3/src/cli.ts index 88629df982..6888f72228 100644 --- a/apps/t3/src/cli.ts +++ b/apps/t3/src/cli.ts @@ -9,6 +9,7 @@ import { startRuntimeApiServer } from "./runtimeApiServer"; const DEFAULT_BACKEND_PORT = 4317; const DEFAULT_WEB_PORT = 4318; +const DEFAULT_CLI_VERSION = "0.1.0"; function parsePort(value: string | undefined, fallback: number): number { if (!value) { @@ -171,8 +172,32 @@ function openBrowser(url: string, noOpen: boolean): void { child.unref(); } -function readCliVersion(): string { - return process.env.npm_package_version ?? "0.1.0"; +export function readCliVersion( + packageJsonPath = path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + "..", + "..", + "..", + "package.json", + ), + env = process.env, +): string { + const envVersion = env.npm_package_version; + if (typeof envVersion === "string" && envVersion.length > 0) { + return envVersion; + } + + try { + const raw = fs.readFileSync(packageJsonPath, "utf8"); + const parsed = JSON.parse(raw) as { version?: unknown }; + if (typeof parsed.version === "string" && parsed.version.length > 0) { + return parsed.version; + } + } catch { + // Ignore read/parse failures and return fallback. + } + + return DEFAULT_CLI_VERSION; } async function runCli(options: CliOptions): Promise {