From c2cc0013f2144aa92e1e103b9e5863de9096aad7 Mon Sep 17 00:00:00 2001 From: ferologics Date: Tue, 31 Mar 2026 20:25:20 +0200 Subject: [PATCH] fix(cli): report packaged version in installed builds --- scripts/smoke-prebuilt-install.ts | 23 ++++++++++++++-------- src/core/updateNotice.ts | 3 +-- src/core/version.ts | 32 ++++++++----------------------- test/cli.test.ts | 5 +++++ tsconfig.json | 1 + 5 files changed, 30 insertions(+), 34 deletions(-) diff --git a/scripts/smoke-prebuilt-install.ts b/scripts/smoke-prebuilt-install.ts index 139c0f7..e0db0e8 100644 --- a/scripts/smoke-prebuilt-install.ts +++ b/scripts/smoke-prebuilt-install.ts @@ -62,17 +62,27 @@ try { const sanitizedPath = `${path.join(installDir, "bin")}:${nodeDir}`; const installedHunk = path.join(installDir, "bin", "hunk"); + const commandEnv = { + ...process.env, + PATH: sanitizedPath, + }; const help = run([installedHunk, "--help"], { - env: { - ...process.env, - PATH: sanitizedPath, - }, + env: commandEnv, }); if (help.stdout.includes("Usage: hunk") === false) { throw new Error(`Expected help output to include 'Usage: hunk'.\n${help.stdout}`); } + const version = run([installedHunk, "--version"], { + env: commandEnv, + }); + if (version.stdout !== `${packageVersion}\n`) { + throw new Error( + `Expected installed hunk --version to print ${packageVersion}.\n${version.stdout}`, + ); + } + const bunCheck = Bun.spawnSync( [ resolvedNode, @@ -80,10 +90,7 @@ try { "const {spawnSync}=require('node:child_process'); process.exit(spawnSync('bun',['--version'],{stdio:'ignore'}).status===0?1:0);", ], { - env: { - ...process.env, - PATH: sanitizedPath, - }, + env: commandEnv, }, ); diff --git a/src/core/updateNotice.ts b/src/core/updateNotice.ts index d9e5d47..8372341 100644 --- a/src/core/updateNotice.ts +++ b/src/core/updateNotice.ts @@ -1,9 +1,8 @@ -import { resolveCliVersion } from "./version"; +import { resolveCliVersion, UNKNOWN_CLI_VERSION } from "./version"; const DIST_TAGS_URL = "https://registry.npmjs.org/-/package/hunkdiff/dist-tags"; const STABLE_SEMVER_PATTERN = /^\d+\.\d+\.\d+$/; const PRERELEASE_SEMVER_PATTERN = /^\d+\.\d+\.\d+-[0-9A-Za-z.-]+$/; -const UNKNOWN_CLI_VERSION = "0.0.0-unknown"; const DEFAULT_UPDATE_NOTICE_FETCH_TIMEOUT_MS = 5_000; const DISABLE_STARTUP_UPDATE_NOTICE_ENV = "HUNK_DISABLE_UPDATE_NOTICE"; diff --git a/src/core/version.ts b/src/core/version.ts index 3728201..795778a 100644 --- a/src/core/version.ts +++ b/src/core/version.ts @@ -1,30 +1,14 @@ -import { existsSync, readFileSync } from "node:fs"; -import { dirname, resolve } from "node:path"; +import packageJson from "../../package.json" with { type: "json" }; -const UNKNOWN_CLI_VERSION = "0.0.0-unknown"; +export const UNKNOWN_CLI_VERSION = "0.0.0-unknown"; -/** Resolve the CLI version from the nearest shipped package manifest. */ -export function resolveCliVersion() { - const candidatePaths = [ - resolve(import.meta.dir, "..", "..", "package.json"), - resolve(dirname(process.execPath), "..", "package.json"), - resolve(dirname(process.execPath), "..", "..", "package.json"), - ]; +const PACKAGE_CLI_VERSION = packageJson.version; - for (const candidatePath of candidatePaths) { - if (!existsSync(candidatePath)) { - continue; - } - - try { - const parsed = JSON.parse(readFileSync(candidatePath, "utf8")) as { version?: unknown }; - if (typeof parsed.version === "string" && parsed.version.length > 0) { - return parsed.version; - } - } catch { - continue; - } +/** Resolve the CLI version reported by `hunk --version`. */ +export function resolveCliVersion(): string { + if (typeof PACKAGE_CLI_VERSION !== "string" || PACKAGE_CLI_VERSION.length === 0) { + return UNKNOWN_CLI_VERSION; } - return UNKNOWN_CLI_VERSION; + return PACKAGE_CLI_VERSION; } diff --git a/test/cli.test.ts b/test/cli.test.ts index d11746a..71bced1 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -3,6 +3,7 @@ import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { parseCli } from "../src/core/cli"; +import { resolveCliVersion } from "../src/core/version"; const tempDirs: string[] = []; @@ -52,6 +53,10 @@ describe("parseCli", () => { expect(explicit).toEqual(bare); }); + test("resolves the package version metadata", () => { + expect(resolveCliVersion()).toBe(require("../package.json").version); + }); + test("prints the package version for --version and version", async () => { const expectedVersion = require("../package.json").version; const flag = await parseCli(["bun", "hunk", "--version"]); diff --git a/tsconfig.json b/tsconfig.json index aa0ea70..3517232 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "jsx": "react-jsx", "jsxImportSource": "@opentui/react", "moduleResolution": "bundler", + "resolveJsonModule": true, "allowImportingTsExtensions": true, "verbatimModuleSyntax": true, "noEmit": true,