Skip to content

Commit 81b6a16

Browse files
chore: add version bump helper (anomalyco#89)
1 parent 1f5e06f commit 81b6a16

3 files changed

Lines changed: 128 additions & 1 deletion

File tree

‎AGENTS.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ OpenWork releases are built by GitHub Actions (`Release App`). A release is trig
107107
- `vendor/openwork/src-tauri/tauri.conf.json` (`version`)
108108
- `vendor/openwork/src-tauri/Cargo.toml` (`version`)
109109

110+
You can bump all three non-interactively with:
111+
- `pnpm bump:patch`
112+
- `pnpm bump:minor`
113+
- `pnpm bump:major`
114+
- `pnpm bump:set -- 0.1.21`
115+
110116
3. Merge the version bump to `main`.
111117
4. Create and push a tag:
112118
- `git tag vX.Y.Z`

‎package.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
"test:permissions": "node scripts/permissions.mjs",
1818
"test:session-switch": "node scripts/session-switch.mjs",
1919
"test:fs-engine": "node scripts/fs-engine.mjs",
20-
"test:e2e": "node scripts/e2e.mjs && node scripts/session-switch.mjs && node scripts/fs-engine.mjs"
20+
"test:e2e": "node scripts/e2e.mjs && node scripts/session-switch.mjs && node scripts/fs-engine.mjs",
21+
"bump:patch": "node scripts/bump-version.mjs patch",
22+
"bump:minor": "node scripts/bump-version.mjs minor",
23+
"bump:major": "node scripts/bump-version.mjs major",
24+
"bump:set": "node scripts/bump-version.mjs --set"
2125
},
2226
"dependencies": {
2327
"@opencode-ai/sdk": "^1.1.19",

‎scripts/bump-version.mjs‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env node
2+
import { readFile, writeFile } from "node:fs/promises";
3+
import path from "node:path";
4+
5+
const ROOT = process.cwd();
6+
const args = process.argv.slice(2);
7+
8+
const usage = () => {
9+
console.log(`Usage:
10+
node scripts/bump-version.mjs patch|minor|major
11+
node scripts/bump-version.mjs --set x.y.z
12+
node scripts/bump-version.mjs --dry-run [patch|minor|major|--set x.y.z]`);
13+
};
14+
15+
const isDryRun = args.includes("--dry-run");
16+
const filtered = args.filter((arg) => arg !== "--dry-run");
17+
18+
if (!filtered.length) {
19+
usage();
20+
process.exit(1);
21+
}
22+
23+
let mode = filtered[0];
24+
let explicit = null;
25+
26+
if (mode === "--set") {
27+
explicit = filtered[1] ?? null;
28+
if (!explicit) {
29+
console.error("--set requires a version like 0.1.21");
30+
process.exit(1);
31+
}
32+
}
33+
34+
const semverPattern = /^\d+\.\d+\.\d+$/;
35+
36+
const readJson = async (filePath) => JSON.parse(await readFile(filePath, "utf8"));
37+
38+
const bump = (value, bumpMode) => {
39+
if (!semverPattern.test(value)) {
40+
throw new Error(`Invalid version: ${value}`);
41+
}
42+
const [major, minor, patch] = value.split(".").map(Number);
43+
if (bumpMode === "major") return `${major + 1}.0.0`;
44+
if (bumpMode === "minor") return `${major}.${minor + 1}.0`;
45+
if (bumpMode === "patch") return `${major}.${minor}.${patch + 1}`;
46+
throw new Error(`Unknown bump mode: ${bumpMode}`);
47+
};
48+
49+
const targetVersion = async () => {
50+
if (explicit) return explicit;
51+
const pkg = await readJson(path.join(ROOT, "package.json"));
52+
return bump(pkg.version, mode);
53+
};
54+
55+
const updatePackageJson = async (nextVersion) => {
56+
const filePath = path.join(ROOT, "package.json");
57+
const data = await readJson(filePath);
58+
data.version = nextVersion;
59+
if (!isDryRun) {
60+
await writeFile(filePath, JSON.stringify(data, null, 2) + "\n");
61+
}
62+
};
63+
64+
const updateCargoToml = async (nextVersion) => {
65+
const filePath = path.join(ROOT, "src-tauri", "Cargo.toml");
66+
const raw = await readFile(filePath, "utf8");
67+
const updated = raw.replace(/\bversion\s*=\s*"[^"]+"/m, `version = "${nextVersion}"`);
68+
if (!isDryRun) {
69+
await writeFile(filePath, updated);
70+
}
71+
};
72+
73+
const updateTauriConfig = async (nextVersion) => {
74+
const filePath = path.join(ROOT, "src-tauri", "tauri.conf.json");
75+
const data = JSON.parse(await readFile(filePath, "utf8"));
76+
data.version = nextVersion;
77+
if (!isDryRun) {
78+
await writeFile(filePath, JSON.stringify(data, null, 2) + "\n");
79+
}
80+
};
81+
82+
const main = async () => {
83+
if (explicit && !semverPattern.test(explicit)) {
84+
throw new Error(`Invalid explicit version: ${explicit}`);
85+
}
86+
if (explicit === null && !["patch", "minor", "major"].includes(mode)) {
87+
throw new Error(`Unknown mode: ${mode}`);
88+
}
89+
90+
const nextVersion = await targetVersion();
91+
await updatePackageJson(nextVersion);
92+
await updateCargoToml(nextVersion);
93+
await updateTauriConfig(nextVersion);
94+
95+
console.log(
96+
JSON.stringify(
97+
{
98+
ok: true,
99+
version: nextVersion,
100+
dryRun: isDryRun,
101+
files: [
102+
"package.json",
103+
"src-tauri/Cargo.toml",
104+
"src-tauri/tauri.conf.json",
105+
],
106+
},
107+
null,
108+
2,
109+
),
110+
);
111+
};
112+
113+
main().catch((error) => {
114+
const message = error instanceof Error ? error.message : String(error);
115+
console.error(JSON.stringify({ ok: false, error: message }));
116+
process.exit(1);
117+
});

0 commit comments

Comments
 (0)