import { readFileSync, writeFileSync } from "node:fs"; import { resolve } from "node:path"; const files = [ "frontend/package.json", "frontend/package-lock.json", "build/config.yml", "frontend/src/lib/dialogs/SettingsModal.svelte", "flake.nix", ]; const semver_re = /^(\d+)\.(\d+)\.(\d+)$/; const input = process.argv[2]; if (!input) { console.error("usage: node scripts/version.mjs "); process.exit(1); } const current_version = JSON.parse( readFileSync(resolve("frontend/package.json"), "utf8"), ).version; const match = current_version.match(semver_re); if (!match) { throw new Error( `frontend/package.json version not semver: ${current_version}`, ); } const [, major, minor, patch] = match; const new_version = input === "major" ? `${Number(major) + 1}.0.0` : input === "minor" ? `${major}.${Number(minor) + 1}.0` : input === "patch" ? `${major}.${minor}.${Number(patch) + 1}` : input; if (!semver_re.test(new_version)) { throw new Error(`invalid semver: ${input}`); } if (new_version === current_version) { console.log(`version unchanged: ${current_version}`); process.exit(0); } for (const path of files) { const resolved = resolve(path); const original = readFileSync(resolved, "utf8"); const count = original.split(current_version).length - 1; if (count === 0) { throw new Error(`missing ${current_version} in ${path}`); } writeFileSync(resolved, original.replaceAll(current_version, new_version)); console.log(`updated ${path} (${count})`); } console.log(`bumped ${current_version} -> ${new_version}`);