|
| 1 | +#!/usr/bin/env node |
| 2 | +// The ONE place the Maven coordinate is derived from the npm version. |
| 3 | +// |
| 4 | +// ADR-0035's decoupled cut fixes the JVM major at **npm major + 7**: every `0.x` line has |
| 5 | +// shipped as `7.x`, and when npm/PyPI/NuGet cut `1.0.0` Maven Central cuts `8.0.0`. That is a |
| 6 | +// forward major, not a continuation, and a hardcoded `7` is wrong the moment the npm line |
| 7 | +// leaves `0.x`. |
| 8 | +// |
| 9 | +// WHY THIS FILE EXISTS. Four scripts derived this independently and two of them were wrong: |
| 10 | +// * `finish-release.mjs` — correct (`npmMajor >= 1 ? 7 + npmMajor : 7`) |
| 11 | +// * `release-verify.mjs` — correct (`npmMajor + 7`), fixed only after it asked Maven |
| 12 | +// Central for `7.0.0` — a version BELOW the last release — and |
| 13 | +// would have declared all nine live modules missing |
| 14 | +// * `prerelease.mjs` — hardcoded `7`, so a `1.0.0` base derived `7.0.0-rc.N` |
| 15 | +// * `release.mjs` — printed `java-v7.<minor>.<patch>` as the tag to create, which |
| 16 | +// is an instruction to a human and therefore the hardest of the |
| 17 | +// four to catch: nothing executes it |
| 18 | +// Two of those are the SAME defect wearing a different hat, which is what a rule with four |
| 19 | +// doors always eventually produces. Import from here; do not re-derive. |
| 20 | +// |
| 21 | +// import { mavenVersion, mavenMajor } from "./maven-coordinate.mjs"; |
| 22 | + |
| 23 | +/** The historical offset: the JVM line has always run `npm major + 7`. */ |
| 24 | +export const MAVEN_MAJOR_OFFSET = 7; |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {string} npmVersion e.g. "0.25.0", "1.0.0", "1.0.0-rc.5" |
| 28 | + * @returns {number} the Maven major |
| 29 | + */ |
| 30 | +export function mavenMajor(npmVersion) { |
| 31 | + const major = Number(String(npmVersion).split(".")[0]); |
| 32 | + if (!Number.isInteger(major) || major < 0) { |
| 33 | + throw new Error(`maven-coordinate: cannot read an npm major from "${npmVersion}"`); |
| 34 | + } |
| 35 | + // `0.x` maps to 7, which the offset already gives; stated as one rule rather than a |
| 36 | + // special case, so there is nothing to keep in sync when the npm line moves past 1. |
| 37 | + return major + MAVEN_MAJOR_OFFSET; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * The full Maven coordinate for an npm version, pre-release suffix preserved verbatim. |
| 42 | + * `0.25.0` → `7.25.0`; `1.0.0` → `8.0.0`; `1.0.0-rc.5` → `8.0.0-rc.5`. |
| 43 | + * @param {string} npmVersion |
| 44 | + * @returns {string} |
| 45 | + */ |
| 46 | +export function mavenVersion(npmVersion) { |
| 47 | + const s = String(npmVersion); |
| 48 | + const m = /^(\d+)\.(\d+)\.(\d+)(-.+)?$/.exec(s); |
| 49 | + if (!m) throw new Error(`maven-coordinate: "${s}" is not a three-part version`); |
| 50 | + return `${Number(m[1]) + MAVEN_MAJOR_OFFSET}.${m[2]}.${m[3]}${m[4] ?? ""}`; |
| 51 | +} |
0 commit comments