Skip to content

Commit e3076f1

Browse files
zeevdrclaude
andauthored
fix(version): generate VERSION constant from package.json at build time (#71)
Replaces the hardcoded VERSION literal in src/index.ts (which had drifted to "0.2.0-alpha.1" while package.json declared "0.3.0-alpha.1") with a generated src/version.ts produced by scripts/gen-version.mjs. - Add scripts/gen-version.mjs with a --check mode that exits non-zero when src/version.ts is out of sync with package.json - Wire gen-version into the build script so the file is always fresh - Add npm run check:version and a CI "version" job that gates the alls-green check - Update the version test to compare against package.json rather than a hardcoded string Closes #42 Co-authored-by: Claude <noreply@anthropic.com>
1 parent c8bb606 commit e3076f1

6 files changed

Lines changed: 51 additions & 5 deletions

File tree

.github/workflows/ci.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ jobs:
7474
files: coverage/coverage-final.json
7575
token: ${{ secrets.CODECOV_TOKEN }}
7676

77+
version:
78+
name: Version sync
79+
runs-on: ubuntu-latest
80+
steps:
81+
- *checkout
82+
- *setup-node-22
83+
- *install
84+
- name: Check src/version.ts matches package.json
85+
run: npm run check:version
86+
7787
examples:
7888
name: Examples
7989
runs-on: ubuntu-latest
@@ -90,7 +100,7 @@ jobs:
90100
check:
91101
name: CI check
92102
if: always()
93-
needs: [lint, typecheck, test, examples]
103+
needs: [lint, typecheck, test, examples, version]
94104
runs-on: ubuntu-latest
95105
steps:
96106
- uses: re-actors/alls-green@release/v1

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
],
1919
"scripts": {
2020
"generate": "rm -rf src/generated && buf generate",
21-
"build": "tsc",
21+
"gen-version": "node scripts/gen-version.mjs",
22+
"check:version": "node scripts/gen-version.mjs --check",
23+
"build": "node scripts/gen-version.mjs && tsc",
2224
"lint": "biome check src/ test/",
2325
"format": "biome format --write src/ test/",
2426
"typecheck": "tsc --noEmit",

scripts/gen-version.mjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env node
2+
// Generates src/version.ts from package.json.
3+
// With --check: exits non-zero if the file is out of sync.
4+
import { readFileSync, writeFileSync } from "node:fs";
5+
6+
const pkg = JSON.parse(readFileSync("package.json", "utf8"));
7+
const version = pkg.version;
8+
9+
const content =
10+
`// Generated by scripts/gen-version.mjs — do not edit manually.\n` +
11+
`export const VERSION = "${version}";\n`;
12+
13+
if (process.argv.includes("--check")) {
14+
const existing = readFileSync("src/version.ts", "utf8");
15+
if (existing !== content) {
16+
const match = existing.match(/VERSION = "([^"]+)"/);
17+
console.error(
18+
`src/version.ts is out of sync with package.json\n` +
19+
` package.json : ${version}\n` +
20+
` src/version.ts: ${match?.[1] ?? "(unreadable)"}`,
21+
);
22+
process.exit(1);
23+
}
24+
console.log(`src/version.ts is in sync with package.json (${version})`);
25+
} else {
26+
writeFileSync("src/version.ts", content);
27+
console.log(`Generated src/version.ts with VERSION = "${version}"`);
28+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @packageDocumentation
55
*/
66

7-
export const VERSION = "0.2.0-alpha.1";
7+
export { VERSION } from "./version.js";
88
export const SUPPORTED_SERVER_VERSION = ">=0.8.0,<1.0.0";
99
export const PROTO_VERSION = "v1";
1010

src/version.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Generated by scripts/gen-version.mjs — do not edit manually.
2+
export const VERSION = "0.3.0-alpha.1";

test/version.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { createRequire } from "node:module";
12
import { describe, expect, it } from "vitest";
23
import { PROTO_VERSION, SUPPORTED_SERVER_VERSION, VERSION } from "../src/index.js";
34

5+
const require = createRequire(import.meta.url);
6+
const pkg = require("../package.json") as { version: string };
7+
48
describe("version constants", () => {
5-
it("exports SDK version", () => {
6-
expect(VERSION).toBe("0.2.0-alpha.1");
9+
it("exports SDK version matching package.json", () => {
10+
expect(VERSION).toBe(pkg.version);
711
});
812

913
it("exports supported server version range", () => {

0 commit comments

Comments
 (0)