|
| 1 | +#!/usr/bin/env node |
| 2 | +import { readFile, writeFile } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +const ROOT = process.cwd(); |
| 6 | +const args = process.argv.slice(2); |
| 7 | + |
| 8 | +const usage = () => { |
| 9 | + console.log(`Usage: |
| 10 | + node scripts/bump-version.mjs patch|minor|major |
| 11 | + node scripts/bump-version.mjs --set x.y.z |
| 12 | + node scripts/bump-version.mjs --dry-run [patch|minor|major|--set x.y.z]`); |
| 13 | +}; |
| 14 | + |
| 15 | +const isDryRun = args.includes("--dry-run"); |
| 16 | +const filtered = args.filter((arg) => arg !== "--dry-run"); |
| 17 | + |
| 18 | +if (!filtered.length) { |
| 19 | + usage(); |
| 20 | + process.exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +let mode = filtered[0]; |
| 24 | +let explicit = null; |
| 25 | + |
| 26 | +if (mode === "--set") { |
| 27 | + explicit = filtered[1] ?? null; |
| 28 | + if (!explicit) { |
| 29 | + console.error("--set requires a version like 0.1.21"); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +const semverPattern = /^\d+\.\d+\.\d+$/; |
| 35 | + |
| 36 | +const readJson = async (filePath) => JSON.parse(await readFile(filePath, "utf8")); |
| 37 | + |
| 38 | +const bump = (value, bumpMode) => { |
| 39 | + if (!semverPattern.test(value)) { |
| 40 | + throw new Error(`Invalid version: ${value}`); |
| 41 | + } |
| 42 | + const [major, minor, patch] = value.split(".").map(Number); |
| 43 | + if (bumpMode === "major") return `${major + 1}.0.0`; |
| 44 | + if (bumpMode === "minor") return `${major}.${minor + 1}.0`; |
| 45 | + if (bumpMode === "patch") return `${major}.${minor}.${patch + 1}`; |
| 46 | + throw new Error(`Unknown bump mode: ${bumpMode}`); |
| 47 | +}; |
| 48 | + |
| 49 | +const targetVersion = async () => { |
| 50 | + if (explicit) return explicit; |
| 51 | + const pkg = await readJson(path.join(ROOT, "package.json")); |
| 52 | + return bump(pkg.version, mode); |
| 53 | +}; |
| 54 | + |
| 55 | +const updatePackageJson = async (nextVersion) => { |
| 56 | + const filePath = path.join(ROOT, "package.json"); |
| 57 | + const data = await readJson(filePath); |
| 58 | + data.version = nextVersion; |
| 59 | + if (!isDryRun) { |
| 60 | + await writeFile(filePath, JSON.stringify(data, null, 2) + "\n"); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +const updateCargoToml = async (nextVersion) => { |
| 65 | + const filePath = path.join(ROOT, "src-tauri", "Cargo.toml"); |
| 66 | + const raw = await readFile(filePath, "utf8"); |
| 67 | + const updated = raw.replace(/\bversion\s*=\s*"[^"]+"/m, `version = "${nextVersion}"`); |
| 68 | + if (!isDryRun) { |
| 69 | + await writeFile(filePath, updated); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +const updateTauriConfig = async (nextVersion) => { |
| 74 | + const filePath = path.join(ROOT, "src-tauri", "tauri.conf.json"); |
| 75 | + const data = JSON.parse(await readFile(filePath, "utf8")); |
| 76 | + data.version = nextVersion; |
| 77 | + if (!isDryRun) { |
| 78 | + await writeFile(filePath, JSON.stringify(data, null, 2) + "\n"); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +const main = async () => { |
| 83 | + if (explicit && !semverPattern.test(explicit)) { |
| 84 | + throw new Error(`Invalid explicit version: ${explicit}`); |
| 85 | + } |
| 86 | + if (explicit === null && !["patch", "minor", "major"].includes(mode)) { |
| 87 | + throw new Error(`Unknown mode: ${mode}`); |
| 88 | + } |
| 89 | + |
| 90 | + const nextVersion = await targetVersion(); |
| 91 | + await updatePackageJson(nextVersion); |
| 92 | + await updateCargoToml(nextVersion); |
| 93 | + await updateTauriConfig(nextVersion); |
| 94 | + |
| 95 | + console.log( |
| 96 | + JSON.stringify( |
| 97 | + { |
| 98 | + ok: true, |
| 99 | + version: nextVersion, |
| 100 | + dryRun: isDryRun, |
| 101 | + files: [ |
| 102 | + "package.json", |
| 103 | + "src-tauri/Cargo.toml", |
| 104 | + "src-tauri/tauri.conf.json", |
| 105 | + ], |
| 106 | + }, |
| 107 | + null, |
| 108 | + 2, |
| 109 | + ), |
| 110 | + ); |
| 111 | +}; |
| 112 | + |
| 113 | +main().catch((error) => { |
| 114 | + const message = error instanceof Error ? error.message : String(error); |
| 115 | + console.error(JSON.stringify({ ok: false, error: message })); |
| 116 | + process.exit(1); |
| 117 | +}); |
0 commit comments