diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 57546e3..d6c5db1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -74,6 +74,16 @@ jobs: files: coverage/coverage-final.json token: ${{ secrets.CODECOV_TOKEN }} + version: + name: Version sync + runs-on: ubuntu-latest + steps: + - *checkout + - *setup-node-22 + - *install + - name: Check src/version.ts matches package.json + run: npm run check:version + examples: name: Examples runs-on: ubuntu-latest @@ -90,7 +100,7 @@ jobs: check: name: CI check if: always() - needs: [lint, typecheck, test, examples] + needs: [lint, typecheck, test, examples, version] runs-on: ubuntu-latest steps: - uses: re-actors/alls-green@release/v1 diff --git a/package.json b/package.json index fd09bc4..7cedb3b 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,9 @@ ], "scripts": { "generate": "rm -rf src/generated && buf generate", - "build": "tsc", + "gen-version": "node scripts/gen-version.mjs", + "check:version": "node scripts/gen-version.mjs --check", + "build": "node scripts/gen-version.mjs && tsc", "lint": "biome check src/ test/", "format": "biome format --write src/ test/", "typecheck": "tsc --noEmit", diff --git a/scripts/gen-version.mjs b/scripts/gen-version.mjs new file mode 100644 index 0000000..f270574 --- /dev/null +++ b/scripts/gen-version.mjs @@ -0,0 +1,28 @@ +#!/usr/bin/env node +// Generates src/version.ts from package.json. +// With --check: exits non-zero if the file is out of sync. +import { readFileSync, writeFileSync } from "node:fs"; + +const pkg = JSON.parse(readFileSync("package.json", "utf8")); +const version = pkg.version; + +const content = + `// Generated by scripts/gen-version.mjs — do not edit manually.\n` + + `export const VERSION = "${version}";\n`; + +if (process.argv.includes("--check")) { + const existing = readFileSync("src/version.ts", "utf8"); + if (existing !== content) { + const match = existing.match(/VERSION = "([^"]+)"/); + console.error( + `src/version.ts is out of sync with package.json\n` + + ` package.json : ${version}\n` + + ` src/version.ts: ${match?.[1] ?? "(unreadable)"}`, + ); + process.exit(1); + } + console.log(`src/version.ts is in sync with package.json (${version})`); +} else { + writeFileSync("src/version.ts", content); + console.log(`Generated src/version.ts with VERSION = "${version}"`); +} diff --git a/src/index.ts b/src/index.ts index aa26770..80c7158 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ * @packageDocumentation */ -export const VERSION = "0.2.0-alpha.1"; +export { VERSION } from "./version.js"; export const SUPPORTED_SERVER_VERSION = ">=0.8.0,<1.0.0"; export const PROTO_VERSION = "v1"; diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 0000000..83f1e5e --- /dev/null +++ b/src/version.ts @@ -0,0 +1,2 @@ +// Generated by scripts/gen-version.mjs — do not edit manually. +export const VERSION = "0.3.0-alpha.1"; diff --git a/test/version.test.ts b/test/version.test.ts index e601acc..1dad747 100644 --- a/test/version.test.ts +++ b/test/version.test.ts @@ -1,9 +1,13 @@ +import { createRequire } from "node:module"; import { describe, expect, it } from "vitest"; import { PROTO_VERSION, SUPPORTED_SERVER_VERSION, VERSION } from "../src/index.js"; +const require = createRequire(import.meta.url); +const pkg = require("../package.json") as { version: string }; + describe("version constants", () => { - it("exports SDK version", () => { - expect(VERSION).toBe("0.2.0-alpha.1"); + it("exports SDK version matching package.json", () => { + expect(VERSION).toBe(pkg.version); }); it("exports supported server version range", () => {