Skip to content

Commit

Permalink
add version.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
L4Ph committed Dec 23, 2024
1 parent a78273b commit 8437726
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Binary file modified bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "narrow",
"name": "tawri",
"version": "0.1.1",
"description": "",
"type": "module",
Expand All @@ -13,7 +13,7 @@
"tauri": "tauri",
"lint": "biome lint",
"format": "biome format --write",
"bump": "bunx tauri-version patch"
"version": "bun run version.ts"
},
"license": "MIT",
"dependencies": {
Expand Down Expand Up @@ -43,8 +43,10 @@
"@tailwindcss/typography": "^0.5.15",
"@types/bun": "latest",
"@types/node": "^22.10.2",
"@types/semver": "^7.5.8",
"autoprefixer": "^10.4.20",
"postcss": "^8.4.49",
"semver": "^7.6.3",
"svelte": "^5.15.0",
"svelte-check": "^4.1.1",
"tailwindcss": "^3.4.17",
Expand Down
51 changes: 51 additions & 0 deletions version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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`;

0 comments on commit 8437726

Please sign in to comment.