Skip to content

Commit 90239bc

Browse files
committed
ci(prerelease): prioritze base semver when calculating highest next pre-release
1 parent 469cb68 commit 90239bc

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

.github/scripts/calculate-prerelease-version.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,22 @@ async function main() {
4646
perPackageVersions.push({ npmName, nextNpmVersion });
4747
}
4848

49-
// Pick the highest prerelease version across all projects by sorting on the numeric suffix that
50-
// follows "<preid>.". For example, given "2.0.1-alpha.3" and "2.0.1-alpha.7", this resolves to
51-
// "2.0.1-alpha.7". The cast is safe because uniqueNextNpmVersions always has at least one entry.
49+
// Pick the highest prerelease version across all projects by comparing the base semver version
50+
// first (major.minor.patch) and then the numeric prerelease suffix. For example, given
51+
// "2.0.320-alpha.2" and "2.0.324-alpha.0", this resolves to "2.0.324-alpha.0" because the base
52+
// version 2.0.324 is higher. The cast is safe because uniqueNextNpmVersions always has at least
53+
// one entry.
5254
const resolvedVersion = /** @type {string} */ (
5355
Array.from(uniqueNextNpmVersions)
5456
.sort((a, b) => {
55-
const aPrereleaseNum = Number(a.split(`${preid}.`)[1] ?? '0');
56-
const bPrereleaseNum = Number(b.split(`${preid}.`)[1] ?? '0');
57-
return aPrereleaseNum - bPrereleaseNum;
57+
const [aBase, aPrereleaseSuffix] = a.split(`-${preid}.`);
58+
const [bBase, bPrereleaseSuffix] = b.split(`-${preid}.`);
59+
const [aMajor, aMinor, aPatch] = (aBase ?? '').split('.').map(Number);
60+
const [bMajor, bMinor, bPatch] = (bBase ?? '').split('.').map(Number);
61+
if (aMajor !== bMajor) return aMajor - bMajor;
62+
if (aMinor !== bMinor) return aMinor - bMinor;
63+
if (aPatch !== bPatch) return aPatch - bPatch;
64+
return Number(aPrereleaseSuffix ?? '0') - Number(bPrereleaseSuffix ?? '0');
5865
})
5966
.at(-1)
6067
);

0 commit comments

Comments
 (0)