-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.ts
More file actions
92 lines (82 loc) · 2.93 KB
/
Copy pathversion.ts
File metadata and controls
92 lines (82 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bun
import { execFileSync } from "node:child_process";
import { readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
const root = process.cwd();
const packageJsonPath = resolve(root, "package.json");
const packageLockPath = resolve(root, "package-lock.json");
const skillPath = resolve(root, "skills/trace-server/SKILL.md");
function run(command: string, args: string[]) {
return execFileSync(command, args, {
cwd: root,
encoding: "utf8",
stdio: ["inherit", "pipe", "pipe"],
}).trim();
}
function fail(message: string): never {
console.error(`error: ${message}`);
process.exit(1);
}
function ensureCleanRepo() {
const status = run("git", ["status", "--porcelain=v1"]);
if (status) fail("repo is dirty; commit or stash changes before bumping version");
}
function parseNextVersion() {
const next = process.argv[2]?.trim();
if (!next) fail("usage: bunx version <x.y.z>");
if (!/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(next)) {
fail(`invalid semver: ${next}`);
}
return next;
}
function updatePackageJson(nextVersion: string) {
const pkg = JSON.parse(readFileSync(packageJsonPath, "utf8")) as {
version: string;
bin?: Record<string, string>;
};
const currentVersion = pkg.version;
if (currentVersion === nextVersion) fail(`package.json is already at ${nextVersion}`);
pkg.version = nextVersion;
pkg.bin = {
...(pkg.bin ?? {}),
version: "./version.ts",
};
writeFileSync(packageJsonPath, `${JSON.stringify(pkg, null, 2)}\n`);
return currentVersion;
}
function updatePackageLock(nextVersion: string) {
const lock = JSON.parse(readFileSync(packageLockPath, "utf8")) as {
version?: string;
packages?: Record<string, { version?: string; bin?: Record<string, string> }>;
};
lock.version = nextVersion;
lock.packages ??= {};
lock.packages[""] ??= {};
lock.packages[""].version = nextVersion;
lock.packages[""].bin = {
...(lock.packages[""].bin ?? {}),
version: "version.ts",
};
writeFileSync(packageLockPath, `${JSON.stringify(lock, null, 2)}\n`);
}
function updateSkill(currentVersion: string, nextVersion: string) {
const skill = readFileSync(skillPath, "utf8");
const updated = skill.replace(
` version: \"${currentVersion}\"`,
` version: \"${nextVersion}\"`,
);
if (updated === skill) fail(`failed to update ${skillPath}`);
writeFileSync(skillPath, updated);
}
function commit(nextVersion: string) {
run("git", ["add", "package.json", "package-lock.json", "skills/trace-server/SKILL.md", "version.ts"]);
run("git", ["commit", "-m", `chore: bump version to ${nextVersion}`]);
run("git", ["tag", `v${nextVersion}`]);
}
const nextVersion = parseNextVersion();
ensureCleanRepo();
const currentVersion = updatePackageJson(nextVersion);
updatePackageLock(nextVersion);
updateSkill(currentVersion, nextVersion);
commit(nextVersion);
console.log(`bumped version: ${currentVersion} -> ${nextVersion}`);