-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathrelease.ts
More file actions
151 lines (122 loc) · 3.94 KB
/
Copy pathrelease.ts
File metadata and controls
151 lines (122 loc) · 3.94 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env bun
import semver from 'semver';
interface PackageJson {
version?: string;
[key: string]: unknown;
}
interface CommandResult {
stdout: string;
stderr: string;
}
function fail(message: string): never {
console.error(message);
process.exit(1);
}
async function run(args: Array<string>): Promise<CommandResult> {
const process = Bun.spawn(args, {
stdout: 'pipe',
stderr: 'pipe',
});
const [exitCode, stdout, stderr] = await Promise.all([
process.exited,
new Response(process.stdout).text(),
new Response(process.stderr).text(),
]);
if (exitCode !== 0) {
const command = args.join(' ');
fail(stderr.trim() || `Command failed: ${command}`);
}
return { stdout, stderr };
}
function validateVersion(version: string): void {
if (!semver.valid(version)) {
fail(`Invalid version '${version}'. Expected semver like 0.7.0.`);
}
}
const INCREMENT_TYPES = ['major', 'minor', 'patch'] as const;
type IncrementType = (typeof INCREMENT_TYPES)[number];
function isIncrementType(value: string): value is IncrementType {
return (INCREMENT_TYPES as ReadonlyArray<string>).includes(value);
}
const versionArg = process.argv[2];
if (!versionArg) {
fail('Usage: ./bun release <version | major | minor | patch>');
}
const releaseStatus = await run(['git', 'status', '--porcelain']);
if (releaseStatus.stdout.trim().length > 0) {
fail(
'Working directory is not clean. Commit, stash, or discard changes before releasing.',
);
}
const packageFile = Bun.file('package.json');
if (!(await packageFile.exists())) {
fail('package.json not found in current directory.');
}
const packageJson = (await packageFile.json()) as PackageJson;
const currentVersion = packageJson.version;
if (!currentVersion || !semver.valid(currentVersion)) {
fail(
`Current version '${currentVersion ?? '(missing)'}' in package.json is not valid semver.`,
);
}
const nextVersion = isIncrementType(versionArg)
? semver.inc(currentVersion, versionArg)
: versionArg;
if (!nextVersion) {
fail(`Failed to compute next version from '${versionArg}'.`);
}
validateVersion(nextVersion);
if (!semver.gt(nextVersion, currentVersion)) {
fail(
`Version ${nextVersion} is not greater than current version ${currentVersion}.`,
);
}
const tagName = `v${nextVersion}`;
const tagCheck = await run(['git', 'tag', '-l', tagName]);
if (tagCheck.stdout.trim() === tagName) {
fail(`Tag ${tagName} already exists.`);
}
const branchResult = await run(['git', 'rev-parse', '--abbrev-ref', 'HEAD']);
const currentBranch = branchResult.stdout.trim();
if (currentBranch !== 'main') {
fail(
`Must be on the 'main' branch to release. Currently on '${currentBranch}'.`,
);
}
await run(['git', 'fetch', 'origin', 'main']);
const behindResult = await run([
'git',
'rev-list',
'--count',
'main..origin/main',
]);
const behindCount = Number.parseInt(behindResult.stdout.trim(), 10);
if (behindCount > 0) {
fail(
`Local 'main' is ${behindCount} commit(s) behind 'origin/main'. Pull before releasing.`,
);
}
packageJson.version = nextVersion;
await Bun.write(packageFile, `${JSON.stringify(packageJson, null, 2)}\n`);
const claudeMarketplaceFile = Bun.file('.claude-plugin/marketplace.json');
const claudeMarketplace = await claudeMarketplaceFile.json();
claudeMarketplace.plugins[0].version = nextVersion;
await Bun.write(
claudeMarketplaceFile,
`${JSON.stringify(claudeMarketplace, null, 2)}\n`,
);
const cursorPluginFile = Bun.file('.cursor-plugin/plugin.json');
const cursorPlugin = await cursorPluginFile.json();
cursorPlugin.version = nextVersion;
await Bun.write(cursorPluginFile, `${JSON.stringify(cursorPlugin, null, 2)}\n`);
await run([
'git',
'add',
'package.json',
'.claude-plugin/marketplace.json',
'.cursor-plugin/plugin.json',
]);
await run(['git', 'commit', '-m', `release: ${tagName}`]);
await run(['git', 'tag', '-a', tagName, '-m', tagName]);
await run(['git', 'push', '--follow-tags']);
console.log(`Released ${tagName}.`);