Skip to content

Commit

Permalink
Minor: readable version tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Nov 28, 2024
1 parent 2344aaa commit e02b0ca
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions tools/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { $ } from "bun";
import { type ReleaseType, inc, valid as isValidVersion } from "semver";

const path = "./packages/merge-sx/package.json";

const variants: ReleaseType[] = [
"major",
"premajor",
Expand All @@ -13,43 +12,38 @@ const variants: ReleaseType[] = [
"prepatch",
"prerelease",
];

const isValidTarget = (subject: string): subject is ReleaseType =>
(variants as string[]).includes(subject);

const isDirty = async () => (await $`git status --porcelain`.quiet()).text();

// Bump version
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 or run with FORCE=true.",
);

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");

// Check for uncommitted changes
if (await isDirty())
throw new Error(
"There are uncommitted changes. Commit them before releasing or run with FORCE=true.",
);

console.debug(current, "—>", desired);

await Bun.write(
path,
JSON.stringify(Object.assign(json, { version: desired }), null, 2),
);

// Commit, tag and push
await $`git add ${path}`;
await $`git commit -m v${desired}`;
await $`git tag v${desired}`;
Expand Down

0 comments on commit e02b0ca

Please sign in to comment.