|
| 1 | +#!/usr/bin/env node |
| 2 | +// Copyright (c) 2026 Lark Technologies Pte. Ltd. |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +const fs = require("node:fs"); |
| 6 | +const path = require("node:path"); |
| 7 | + |
| 8 | +const STABLE_VERSION_PATTERN = /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$/; |
| 9 | + |
| 10 | +function isStableVersion(value) { |
| 11 | + return typeof value === "string" && STABLE_VERSION_PATTERN.test(value); |
| 12 | +} |
| 13 | + |
| 14 | +function releaseError(message, observed, hint) { |
| 15 | + return { ok: false, error: { type: "release_preflight", message, observed, hint } }; |
| 16 | +} |
| 17 | + |
| 18 | +function validateReleasePreflight(packageJson, packageLockJson, tag) { |
| 19 | + const packageVersion = packageJson?.version; |
| 20 | + const lockVersion = packageLockJson?.version; |
| 21 | + const lockRootVersion = packageLockJson?.packages?.[""]?.version; |
| 22 | + const observed = { |
| 23 | + packageVersion: packageVersion ?? null, |
| 24 | + lockVersion: lockVersion ?? null, |
| 25 | + lockRootVersion: lockRootVersion ?? null, |
| 26 | + tagVersion: null, |
| 27 | + }; |
| 28 | + |
| 29 | + for (const [field, value] of [ |
| 30 | + ["package.json.version", packageVersion], |
| 31 | + ["package-lock.json.version", lockVersion], |
| 32 | + ['package-lock.json.packages[""].version', lockRootVersion], |
| 33 | + ]) { |
| 34 | + if (!isStableVersion(value)) { |
| 35 | + return releaseError( |
| 36 | + `${field} must be a stable release version in X.Y.Z form`, |
| 37 | + observed, |
| 38 | + "Use the same stable X.Y.Z version in all package fields; prerelease and build metadata are not allowed for production releases.", |
| 39 | + ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (packageVersion !== lockVersion || packageVersion !== lockRootVersion) { |
| 44 | + return releaseError( |
| 45 | + "Package version fields do not match", |
| 46 | + observed, |
| 47 | + "Synchronize package.json.version and both package-lock.json version fields.", |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + if (tag === undefined) { |
| 52 | + return { ok: true, data: observed }; |
| 53 | + } |
| 54 | + if (typeof tag !== "string" || !tag.startsWith("v") || !isStableVersion(tag.slice(1))) { |
| 55 | + return releaseError( |
| 56 | + "--tag must use the stable release form vX.Y.Z", |
| 57 | + { ...observed, tag }, |
| 58 | + `Use --tag v${packageVersion}; prerelease and build metadata are not allowed for production releases.`, |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + const tagVersion = tag.slice(1); |
| 63 | + if (tagVersion !== packageVersion) { |
| 64 | + return releaseError( |
| 65 | + "Tag version does not match the package version", |
| 66 | + { ...observed, tagVersion, tag }, |
| 67 | + `Use --tag v${packageVersion}.`, |
| 68 | + ); |
| 69 | + } |
| 70 | + return { ok: true, data: { ...observed, tagVersion } }; |
| 71 | +} |
| 72 | + |
| 73 | +function writeResult(result) { |
| 74 | + (result.ok ? process.stdout : process.stderr).write(`${JSON.stringify(result)}\n`); |
| 75 | + if (!result.ok) process.exitCode = 1; |
| 76 | +} |
| 77 | + |
| 78 | +function main() { |
| 79 | + const args = process.argv.slice(2); |
| 80 | + let tag; |
| 81 | + if (args.length === 2 && args[0] === "--tag") { |
| 82 | + tag = args[1]; |
| 83 | + } else if (args.length !== 0) { |
| 84 | + writeResult(releaseError( |
| 85 | + "Expected no arguments or --tag vX.Y.Z", |
| 86 | + { arguments: args }, |
| 87 | + "Run release:check without arguments or pass exactly one --tag value.", |
| 88 | + )); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const repoRoot = path.resolve(__dirname, ".."); |
| 93 | + try { |
| 94 | + const packageJson = JSON.parse(fs.readFileSync(path.join(repoRoot, "package.json"), "utf8")); |
| 95 | + const packageLockJson = JSON.parse(fs.readFileSync(path.join(repoRoot, "package-lock.json"), "utf8")); |
| 96 | + writeResult(validateReleasePreflight(packageJson, packageLockJson, tag)); |
| 97 | + } catch (error) { |
| 98 | + writeResult(releaseError( |
| 99 | + "Could not read release package metadata", |
| 100 | + { reason: error.message }, |
| 101 | + "Ensure package.json and package-lock.json exist and contain valid JSON.", |
| 102 | + )); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +module.exports = { validateReleasePreflight }; |
| 107 | + |
| 108 | +if (require.main === module) main(); |
0 commit comments