-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.ts
55 lines (44 loc) · 1.33 KB
/
version.ts
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
/**
* @see {@link https://github.com/oven-sh/bun/issues/5291#issuecomment-2506719866}
*/
import { fail } from "node:assert";
import { $ } from "bun";
import { type ReleaseType, inc, valid as isValidVersion } from "semver";
const path = "./package.json";
const variants: ReleaseType[] = [
"major",
"premajor",
"minor",
"preminor",
"patch",
"prepatch",
"prerelease",
];
const isValidTarget = (subject: string): subject is ReleaseType =>
(variants as string[]).includes(subject);
const isDirty = async () => (await $`git status --porcelain`.quiet()).text();
const target = Bun.argv.pop();
const json = await Bun.file(path).json();
const { version: current } = json;
if (!isValidVersion(current))
throw new Error(`Invalid current version ${current}`);
if (await isDirty())
throw new Error(
"There are uncommitted changes. Commit them before releasing.",
);
const desired = isValidVersion(target)
? target
: target && isValidTarget(target)
? inc(current, target, "beta", "1")
: fail("invalid target version");
if (!desired) throw new Error("Failed to bump");
console.debug(current, "—>", desired);
await Bun.write(
path,
JSON.stringify(Object.assign(json, { version: desired }), null, 2),
);
await $`git add ${path}`;
await $`git commit -m v${desired}`;
await $`git tag v${desired}`;
await $`git push`;
await $`git push --tags`;