diff --git a/scripts/check-release.mjs b/scripts/check-release.mjs index 156897b4..cd41562d 100644 --- a/scripts/check-release.mjs +++ b/scripts/check-release.mjs @@ -1,42 +1,12 @@ import { readFile } from "node:fs/promises"; import path from "node:path"; +import { compareVersions, isExpectedNextVersion, parseVersion, readJson } from "./release-utils.mjs"; function fail(message) { console.error(`release check failed: ${message}`); process.exitCode = 1; } -async function readJson(file) { - return JSON.parse(await readFile(file, "utf8")); -} - -function parseVersion(version) { - const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version); - if (!match) return null; - return { - major: Number(match[1]), - minor: Number(match[2]), - patch: Number(match[3]), - }; -} - -function compareVersions(a, b) { - return a.major - b.major || a.minor - b.minor || a.patch - b.patch; -} - -function isExpectedNextVersion(previous, current) { - if (current.major === previous.major && current.minor === previous.minor) { - return current.patch === previous.patch + 1; - } - if (current.major === previous.major && current.minor === previous.minor + 1) { - return current.patch === 0; - } - if (current.major === previous.major + 1) { - return current.minor === 0 && current.patch === 0; - } - return false; -} - const packageJson = await readJson("package.json"); const packageLockJson = await readJson("package-lock.json"); const manifestJson = await readJson("manifest.json"); diff --git a/scripts/prepare-release.mjs b/scripts/prepare-release.mjs index edf48d9e..cec2c590 100644 --- a/scripts/prepare-release.mjs +++ b/scripts/prepare-release.mjs @@ -1,46 +1,12 @@ import { mkdir, readFile, writeFile } from "node:fs/promises"; import path from "node:path"; +import { compareVersions, isExpectedNextVersion, parseVersion, readJson, writeJson } from "./release-utils.mjs"; function fail(message) { console.error(`release prepare failed: ${message}`); process.exit(1); } -async function readJson(file) { - return JSON.parse(await readFile(file, "utf8")); -} - -async function writeJson(file, value) { - await writeFile(file, `${JSON.stringify(value, null, 2)}\n`); -} - -function parseVersion(version) { - const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version); - if (!match) return null; - return { - major: Number(match[1]), - minor: Number(match[2]), - patch: Number(match[3]), - }; -} - -function compareVersions(a, b) { - return a.major - b.major || a.minor - b.minor || a.patch - b.patch; -} - -function isExpectedNextVersion(previous, current) { - if (current.major === previous.major && current.minor === previous.minor) { - return current.patch === previous.patch + 1; - } - if (current.major === previous.major && current.minor === previous.minor + 1) { - return current.patch === 0; - } - if (current.major === previous.major + 1) { - return current.minor === 0 && current.patch === 0; - } - return false; -} - const releaseVersion = process.argv[2]; if (!releaseVersion) fail("usage: npm run release:prepare -- X.Y.Z"); diff --git a/scripts/release-utils.mjs b/scripts/release-utils.mjs new file mode 100644 index 00000000..bc9402ec --- /dev/null +++ b/scripts/release-utils.mjs @@ -0,0 +1,36 @@ +import { readFile, writeFile } from "node:fs/promises"; + +export async function readJson(file) { + return JSON.parse(await readFile(file, "utf8")); +} + +export async function writeJson(file, value) { + await writeFile(file, `${JSON.stringify(value, null, 2)}\n`); +} + +export function parseVersion(version) { + const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version); + if (!match) return null; + return { + major: Number(match[1]), + minor: Number(match[2]), + patch: Number(match[3]), + }; +} + +export function compareVersions(a, b) { + return a.major - b.major || a.minor - b.minor || a.patch - b.patch; +} + +export function isExpectedNextVersion(previous, current) { + if (current.major === previous.major && current.minor === previous.minor) { + return current.patch === previous.patch + 1; + } + if (current.major === previous.major && current.minor === previous.minor + 1) { + return current.patch === 0; + } + if (current.major === previous.major + 1) { + return current.minor === 0 && current.patch === 0; + } + return false; +}