|
| 1 | +// SPDX-FileCopyrightText: 2026 MesTTo |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +// Bump every publishable package to a new version in lockstep. A MeTTaScript release versions all of |
| 5 | +// packages/* and compat/* together; their inter-package and shim dependencies use pnpm `workspace:*`, |
| 6 | +// which `pnpm publish` rewrites to the exact version, so only each package's own `version` field changes |
| 7 | +// here. The private root package carries no version and is left untouched. |
| 8 | +// |
| 9 | +// Usage: |
| 10 | +// node scripts/release.mjs 2.5.1 set an explicit version |
| 11 | +// node scripts/release.mjs patch bump the shared patch version (also: minor, major) |
| 12 | +// npm run release -- patch same, via the package script |
| 13 | +// |
| 14 | +// After bumping: add the RELEASE_NOTES.md section, commit as "MeTTaScript <version>", tag `v<version>`, |
| 15 | +// and push the commit and tag. `.github/workflows/release.yml` builds and publishes on the pushed `v*` tag. |
| 16 | + |
| 17 | +import { readFileSync, writeFileSync, readdirSync, existsSync } from "node:fs"; |
| 18 | +import { join, dirname } from "node:path"; |
| 19 | +import { fileURLToPath } from "node:url"; |
| 20 | + |
| 21 | +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 22 | + |
| 23 | +/** Every publishable package.json under packages/* and compat/*. */ |
| 24 | +function packageManifests() { |
| 25 | + return ["packages", "compat"].flatMap((base) => { |
| 26 | + const dir = join(root, base); |
| 27 | + if (!existsSync(dir)) return []; |
| 28 | + return readdirSync(dir, { withFileTypes: true }) |
| 29 | + .filter((entry) => entry.isDirectory()) |
| 30 | + .map((entry) => join(dir, entry.name, "package.json")) |
| 31 | + .filter(existsSync); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +/** The target version for an explicit `x.y.z` (optionally with a prerelease suffix) or a semver bump keyword. */ |
| 36 | +function nextVersion(current, spec) { |
| 37 | + if (/^\d+\.\d+\.\d+(-[0-9A-Za-z.]+)?$/.test(spec)) return spec; |
| 38 | + const [major, minor, patch] = current.split(".").map((n) => Number.parseInt(n, 10)); |
| 39 | + if (spec === "major") return `${major + 1}.0.0`; |
| 40 | + if (spec === "minor") return `${major}.${minor + 1}.0`; |
| 41 | + if (spec === "patch") return `${major}.${minor}.${patch + 1}`; |
| 42 | + return undefined; |
| 43 | +} |
| 44 | + |
| 45 | +function fail(message) { |
| 46 | + console.error(message); |
| 47 | + process.exit(1); |
| 48 | +} |
| 49 | + |
| 50 | +const spec = process.argv[2]; |
| 51 | +if (spec === undefined) fail("usage: node scripts/release.mjs <version | patch | minor | major>"); |
| 52 | + |
| 53 | +const manifests = packageManifests(); |
| 54 | +if (manifests.length === 0) fail("no publishable packages found under packages/ or compat/"); |
| 55 | + |
| 56 | +const current = new Set(manifests.map((file) => JSON.parse(readFileSync(file, "utf8")).version)); |
| 57 | +if (current.size !== 1) |
| 58 | + fail(`packages are not in lockstep, refusing to bump: ${[...current].sort().join(", ")}`); |
| 59 | +const from = [...current][0]; |
| 60 | + |
| 61 | +const to = nextVersion(from, spec); |
| 62 | +if (to === undefined) fail(`not a version or a patch/minor/major bump: ${spec}`); |
| 63 | +if (to === from) fail(`already at ${to}`); |
| 64 | + |
| 65 | +for (const file of manifests) { |
| 66 | + const text = readFileSync(file, "utf8"); |
| 67 | + // Replace only the package's own top-level version field. `workspace:*` dependencies carry no literal |
| 68 | + // version, so this string is unambiguous and formatting (indentation, key order) is preserved. |
| 69 | + const bumped = text.replace(/"version":\s*"[^"]+"/, `"version": "${to}"`); |
| 70 | + if (bumped === text) fail(`no version field in ${file}`); |
| 71 | + writeFileSync(file, bumped); |
| 72 | +} |
| 73 | + |
| 74 | +console.log(`bumped ${manifests.length} packages: ${from} -> ${to}`); |
| 75 | +console.log(`next: update RELEASE_NOTES.md, commit "MeTTaScript ${to}", tag v${to}, push commit + tag`); |
0 commit comments