Repository navigation
chore: enforce version sync in release workflows #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { assertVersionsMatch } from "./lib/version-sync.mjs"; | ||
|
|
||
| try { | ||
| const version = assertVersionsMatch(); | ||
| process.stdout.write( | ||
| `Version sync OK: package.json and .codex-plugin/plugin.json are both ${version}.\n` | ||
| ); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| process.stderr.write(`${message}\n`); | ||
| process.exitCode = 1; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const ROOT_DIR = path.resolve(fileURLToPath(new URL("../..", import.meta.url))); | ||
|
|
||
| export function resolveVersionSyncPaths(rootDir = ROOT_DIR) { | ||
| return { | ||
| rootDir, | ||
| packageJsonPath: path.join(rootDir, "package.json"), | ||
| pluginJsonPath: path.join(rootDir, ".codex-plugin", "plugin.json"), | ||
| }; | ||
| } | ||
|
|
||
| function readJson(filePath) { | ||
| return JSON.parse(fs.readFileSync(filePath, "utf8")); | ||
| } | ||
|
|
||
| function writeJson(filePath, value) { | ||
| fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8"); | ||
| } | ||
|
|
||
| export function readVersionPair(rootDir = ROOT_DIR) { | ||
| const { packageJsonPath, pluginJsonPath } = resolveVersionSyncPaths(rootDir); | ||
| const packageJson = readJson(packageJsonPath); | ||
| const pluginJson = readJson(pluginJsonPath); | ||
| return { | ||
| packageJson, | ||
| pluginJson, | ||
| packageVersion: String(packageJson.version ?? ""), | ||
| pluginVersion: String(pluginJson.version ?? ""), | ||
| }; | ||
| } | ||
|
|
||
| export function assertVersionsMatch(rootDir = ROOT_DIR) { | ||
| const { packageVersion, pluginVersion } = readVersionPair(rootDir); | ||
| if (!packageVersion || !pluginVersion) { | ||
| throw new Error( | ||
| "Both package.json and .codex-plugin/plugin.json must declare a version." | ||
| ); | ||
| } | ||
| if (packageVersion !== pluginVersion) { | ||
| throw new Error( | ||
| `Version mismatch: package.json is ${packageVersion} but .codex-plugin/plugin.json is ${pluginVersion}.` | ||
| ); | ||
| } | ||
| return packageVersion; | ||
| } | ||
|
|
||
| export function syncPluginVersionFromPackage(rootDir = ROOT_DIR) { | ||
| const { pluginJsonPath } = resolveVersionSyncPaths(rootDir); | ||
| const { packageJson, pluginJson, packageVersion, pluginVersion } = | ||
| readVersionPair(rootDir); | ||
|
|
||
| if (!packageVersion) { | ||
| throw new Error("package.json is missing a version."); | ||
| } | ||
|
|
||
| if (packageVersion === pluginVersion) { | ||
| return { changed: false, version: packageVersion }; | ||
| } | ||
|
|
||
| const nextPluginJson = { | ||
| ...pluginJson, | ||
| version: packageJson.version, | ||
| }; | ||
| writeJson(pluginJsonPath, nextPluginJson); | ||
| return { changed: true, version: packageVersion }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { syncPluginVersionFromPackage } from "./lib/version-sync.mjs"; | ||
|
|
||
| try { | ||
| const result = syncPluginVersionFromPackage(); | ||
| process.stdout.write( | ||
| result.changed | ||
| ? `Updated .codex-plugin/plugin.json to version ${result.version}.\n` | ||
| : `.codex-plugin/plugin.json already matches package.json at ${result.version}.\n` | ||
| ); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| process.stderr.write(`${message}\n`); | ||
| process.exitCode = 1; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { describe, it } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
|
|
||
| import { | ||
| assertVersionsMatch, | ||
| readVersionPair, | ||
| syncPluginVersionFromPackage, | ||
| } from "../scripts/lib/version-sync.mjs"; | ||
|
|
||
| function writeJson(filePath, value) { | ||
| fs.mkdirSync(path.dirname(filePath), { recursive: true }); | ||
| fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8"); | ||
| } | ||
|
|
||
| function createTempRepo({ packageVersion, pluginVersion }) { | ||
| const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cc-version-sync-")); | ||
| writeJson(path.join(dir, "package.json"), { | ||
| name: "cc-plugin-codex", | ||
| version: packageVersion, | ||
| }); | ||
| writeJson(path.join(dir, ".codex-plugin", "plugin.json"), { | ||
| name: "cc", | ||
| version: pluginVersion, | ||
| }); | ||
| return dir; | ||
| } | ||
|
|
||
| describe("version sync", () => { | ||
| it("asserts the live repo versions match", () => { | ||
| assert.equal(assertVersionsMatch(), "1.0.0"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This test locks the expected live version to Useful? React with 👍 / 👎. |
||
| }); | ||
|
|
||
| it("detects mismatched versions", () => { | ||
| const dir = createTempRepo({ | ||
| packageVersion: "1.2.3", | ||
| pluginVersion: "1.2.2", | ||
| }); | ||
| try { | ||
| assert.throws( | ||
| () => assertVersionsMatch(dir), | ||
| /Version mismatch: package\.json is 1\.2\.3 but \.codex-plugin\/plugin\.json is 1\.2\.2\./ | ||
| ); | ||
| } finally { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it("syncs plugin.json from package.json", () => { | ||
| const dir = createTempRepo({ | ||
| packageVersion: "2.0.0", | ||
| pluginVersion: "1.0.0", | ||
| }); | ||
| try { | ||
| const result = syncPluginVersionFromPackage(dir); | ||
| assert.deepEqual(result, { changed: true, version: "2.0.0" }); | ||
| const { packageVersion, pluginVersion } = readVersionPair(dir); | ||
| assert.equal(packageVersion, "2.0.0"); | ||
| assert.equal(pluginVersion, "2.0.0"); | ||
| } finally { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it("does nothing when versions already match", () => { | ||
| const dir = createTempRepo({ | ||
| packageVersion: "3.1.4", | ||
| pluginVersion: "3.1.4", | ||
| }); | ||
| try { | ||
| const result = syncPluginVersionFromPackage(dir); | ||
| assert.deepEqual(result, { changed: false, version: "3.1.4" }); | ||
| } finally { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
versionlifecycle script updates.codex-plugin/plugin.jsonbut never stages it, sonpm versionwill create a commit/tag that bumpspackage.jsonwhile leaving the plugin manifest change unstaged. npm's own command docs (.../npm/docs/content/commands/npm-version.md, lifecycle steps 2/4) state files changed by lifecycle scripts must be explicitly added withgit add; without that, a normalnpm version patchrelease flow can produce a tagged commit that immediately failscheck:version-syncin CI/publish.Useful? React with 👍 / 👎.