From fa9b5a9038902ee0c836b742fa847e082a06b20c Mon Sep 17 00:00:00 2001 From: Nixinova Date: Wed, 3 Jan 2024 21:59:25 +1300 Subject: [PATCH] Fix max version in `--list` output being wrong --- changelog.md | 1 + src/index.ts | 25 +++++++++++++++++++++---- test/test.js | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index 4de139c..f78d29d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,7 @@ # Changelog ## Next +- Fixed incorrect maximum versions being returned in the output of `--list`. - Fixed snapshots being incorrectly formatted in the output of `--list`. ## 1.3.10 diff --git a/src/index.ts b/src/index.ts index e940597..ac70f65 100644 --- a/src/index.ts +++ b/src/index.ts @@ -132,14 +132,31 @@ function getVersions(format: number, type: PackType = 'resource'): VersionsResul } if (!format || format > LATEST[type] || (type === 'data' && format < 4)) return output + const getVersionBelow = function (ver: VersionName, minVer: VersionName): VersionName { + const formatVer = ([x, y, z]: Array) => [x, y, z].join('.') as VersionName + const [minX, minY, minZ] = minVer.split('.') + const [x, y, z] = ver.split('.') + // (1.X.a) vs 1.X.b + if (minY === y) { + if (z === 'x') return formatVer([x, y, z]) + else return formatVer([x, y, +z - 1]) + } + // (1.X.a) vs 1.Y.b + else { + if (z === 'x') return formatVer([x, +y - 1, z]) + else return formatVer([x, y, +z - 1]) + } + } + // Min and max releases const startReleases = Object.entries(START_RELEASES) const relIndex = startReleases.findIndex(([, data]) => data[type] === format) if (relIndex >= 0) { - const minRelease = startReleases[relIndex][0] - const maxRelease = startReleases[relIndex + 1][0].replace(/\.(\d+)\./, (_, major) => `.${major - 1}.`) - output.releases.min = minRelease as VersionName - output.releases.max = maxRelease as VersionName + const lastWithFormat = startReleases.find(([, obj]) => (obj[type] ?? 0) > format) ?? [] + const minRelease = startReleases[relIndex][0] as VersionName + const maxRelease = getVersionBelow(lastWithFormat[0] as VersionName, minRelease) + output.releases.min = minRelease + output.releases.max = maxRelease } // Min and max snapshots diff --git a/test/test.js b/test/test.js index 4b60854..d13cb92 100644 --- a/test/test.js +++ b/test/test.js @@ -52,6 +52,8 @@ testVersions([10, 'resource'], { releases: { min: '', max: '' }, snapshots: { mi testVersions([6, 'resource'], { releases: { min: '1.16.x', max: '1.16.x' }, snapshots: { min: '', max: '' } }) testVersions([6, 'data'], { releases: { min: '1.16.x', max: '1.16.x' }, snapshots: { min: '20w45a', max: '20w45a' } }) testVersions([7, 'resource'], { releases: { min: '1.17.x', max: '1.17.x' }, snapshots: { min: '20w45a', max: '21w38a' } }) +testVersions([10, 'data'], { releases: { min: '1.19.x', max: '1.19.3' }, snapshots: { min: '22w11a', max: '23w02a' } }) testVersions([11, 'resource'], { releases: { min: '', max: '' }, snapshots: { min: '22w42a', max: '22w44a' } }) +testVersions([15, 'data'], { releases: { min: '1.20.x', max: '1.20.1' }, snapshots: { min: '23w18a', max: '23w30a' } }) console.log(`\nRan ${total} tests | ${passed} passed | ${failed} failed`)