Skip to content

Commit

Permalink
feat: latestOfMajorOnly option to restrict to latest of each major line
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Oct 17, 2023
1 parent 31c43b9 commit bca65cd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ interface Options {
now?: Date;
cache?: Map<any, any>;
mirror?: string;
latestOfMajorOnly?: Boolean
}

interface VersionInfo {
Expand Down
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ module.exports = async function (alias = 'lts_active', opts = {}) {
const now = opts.now || new Date()
const cache = opts.cache || _cache
const mirror = opts.mirror || 'https://nodejs.org/dist/'
const latestOfMajorOnly = opts.latestOfMajorOnly || false

const a = Array.isArray(alias) ? alias : [alias]
const versions = await getLatestVersionsByCodename(now, cache, mirror)

// Reduce to an object
const m = a.reduce((m, a) => {
let m = a.reduce((m, a) => {
const vers = resolveAlias(versions, a)
if (Array.isArray(vers)) {
vers.forEach((v) => {
Expand All @@ -24,6 +25,18 @@ module.exports = async function (alias = 'lts_active', opts = {}) {
return m
}, {})

// If only latest major is true, filter out all but latest
if (latestOfMajorOnly) {
const vers = Object.values(m).reduce((latestMajor, v) => {
// If highest in version range, save off
if (!latestMajor[v.major] || semver.gt(v.version, latestMajor[v.major].version)) {
latestMajor[v.major] = v
}
return latestMajor
}, {})
m = Array.from(Object.values(vers))
}

// Sort and pluck version
return Object.values(m).sort((v1, v2) => {
return semver.gt(v1.version, v2.version) ? 1 : -1
Expand Down
3 changes: 2 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import nv from '.'
await nv(['lts_active'], {
now: new Date(),
cache: new Map(),
mirror: 'http://example.com'
mirror: 'http://example.com',
latestOfMajorOnly: true
})
})()
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@ suite('nv', () => {
assert.strictEqual(versions[0].tag, 'v8-canary20191022e5d3472f57')
assert.strictEqual(versions[0].versionName, 'v13')
})

test('latestOfMajorOnly', async () => {
const versions = await nv('18.x', { now, latestOfMajorOnly: true })
assert.strictEqual(versions.length, 1)
assert.strictEqual(versions[0].major, 18)
})
})

0 comments on commit bca65cd

Please sign in to comment.