Single-source the version from package.json (closes #5)
check / check (push) Successful in 33s
check / check (push) Successful in 33s
src/index.ts imports package.json for VERSION and bin/quak.ts passes VERSION to commander, so package.json is the only place the version is written. tsc copies package.json to dist/package.json, so the import resolves from the built output; script/build runs the built CLI with --version to prove it. A test checks VERSION and quak --version against package.json. Model: opus-5-5
This commit is contained in:
+39
-5
@@ -1,9 +1,43 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { VERSION } from "../src/index.js";
|
||||
|
||||
describe("quak", () => {
|
||||
it("exports a version string", () => {
|
||||
expect(typeof VERSION).toBe("string");
|
||||
expect(VERSION.length).toBeGreaterThan(0);
|
||||
const packageVersion = (
|
||||
JSON.parse(
|
||||
readFileSync(new URL("../package.json", import.meta.url), "utf-8"),
|
||||
) as { version: string }
|
||||
).version;
|
||||
|
||||
class ExitCalled extends Error {}
|
||||
|
||||
describe("version", () => {
|
||||
const argv = process.argv;
|
||||
|
||||
afterEach(() => {
|
||||
process.argv = argv;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("exports the version from package.json", () => {
|
||||
expect(VERSION).toBe(packageVersion);
|
||||
});
|
||||
|
||||
// Runs bin/quak.ts with --version. commander prints the version and then
|
||||
// calls process.exit, which is stubbed to throw so the test survives.
|
||||
it("reports the version from package.json in quak --version", async () => {
|
||||
const printed: string[] = [];
|
||||
vi.spyOn(process.stdout, "write").mockImplementation((chunk) => {
|
||||
printed.push(String(chunk));
|
||||
return true;
|
||||
});
|
||||
vi.spyOn(process, "exit").mockImplementation(() => {
|
||||
throw new ExitCalled();
|
||||
});
|
||||
process.argv = ["node", "quak", "--version"];
|
||||
|
||||
await expect(import("../bin/quak.js")).rejects.toBeInstanceOf(
|
||||
ExitCalled,
|
||||
);
|
||||
expect(printed.join("").trim()).toBe(packageVersion);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user