Resolve CLI version from package metadata with tests
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
c263ff67ed
commit
e6540e7f2d
2 files changed
+52
-3
No files matched your search
+25
-1
@@ -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");
|
||||
});
|
||||
});
|
||||
+27
-2
@@ -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<void> {
|
||||
|
||||
Reference in new issue
Block a user