Skip to content

Commit d2a6f15

Browse files
committed
feat: add option to ignore future releases
1 parent ee7a4b9 commit d2a6f15

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ interface Options {
22
now?: Date;
33
cache?: Map<any, any>;
44
mirror?: string;
5-
latestOfMajorOnly?: Boolean
5+
latestOfMajorOnly?: Boolean;
6+
ignoreFutureReleases?: Boolean;
67
}
78

89
interface VersionInfo {

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ module.exports = async function (alias = 'lts_active', opts = {}) {
88
const cache = opts.cache || _cache
99
const mirror = opts.mirror || 'https://nodejs.org/dist/'
1010
const latestOfMajorOnly = opts.latestOfMajorOnly || false
11+
const ignoreFutureReleases = opts.ignoreFutureReleases || false
1112

1213
const a = Array.isArray(alias) ? alias : [alias]
13-
const versions = await getLatestVersionsByCodename(now, cache, mirror)
14+
const versions = await getLatestVersionsByCodename(now, cache, mirror, ignoreFutureReleases)
1415

1516
// Reduce to an object
1617
let m = a.reduce((m, a) => {
@@ -70,7 +71,7 @@ function getVersions (cache, mirror) {
7071
}).json()
7172
}
7273

73-
async function getLatestVersionsByCodename (now, cache, mirror) {
74+
async function getLatestVersionsByCodename (now, cache, mirror, ignoreFutureReleases) {
7475
const schedule = await getSchedule(cache)
7576
const versions = await getVersions(cache, mirror)
7677

@@ -112,6 +113,11 @@ async function getLatestVersionsByCodename (now, cache, mirror) {
112113
}
113114
}
114115

116+
// This version is from future; completely ignore it (i.e. we may have specified a `now` from the past)
117+
if (ignoreFutureReleases && now < v.releaseDate) {
118+
return obj
119+
}
120+
115121
// All versions get added to all
116122
obj.all.push(v)
117123

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,20 @@ suite('nv', () => {
177177
assert.strictEqual(versions[0].major, 0)
178178
assert.strictEqual(versions[0].isLts, false)
179179
})
180+
181+
test('ignoreFutureReleases=false', async () => {
182+
const versions = await nv('v8', { now, ignoreFutureReleases: false })
183+
assert.strictEqual(versions.length, 1)
184+
assert.strictEqual(versions[0].major, 8)
185+
assert.strictEqual(versions[0].minor, 17)
186+
assert.strictEqual(versions[0].patch, 0)
187+
})
188+
189+
test('ignoreFutureReleases=true', async () => {
190+
const versions = await nv('v8', { now, ignoreFutureReleases: true })
191+
assert.strictEqual(versions.length, 1)
192+
assert.strictEqual(versions[0].major, 8)
193+
assert.strictEqual(versions[0].minor, 16)
194+
assert.strictEqual(versions[0].patch, 1)
195+
})
180196
})

0 commit comments

Comments
 (0)