Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions scripts/smoke-prebuilt-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,35 @@ 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,
"-e",
"const {spawnSync}=require('node:child_process'); process.exit(spawnSync('bun',['--version'],{stdio:'ignore'}).status===0?1:0);",
],
{
env: {
...process.env,
PATH: sanitizedPath,
},
env: commandEnv,
},
);

Expand Down
3 changes: 1 addition & 2 deletions src/core/updateNotice.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
32 changes: 8 additions & 24 deletions src/core/version.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 5 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];

Expand Down Expand Up @@ -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"]);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"jsx": "react-jsx",
"jsxImportSource": "@opentui/react",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
Expand Down
Loading