diff --git a/index.d.ts b/index.d.ts index 372edd0..1f7daa9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,7 +2,8 @@ interface Options { now?: Date; cache?: Map; mirror?: string; - latestOfMajorOnly?: Boolean + latestOfMajorOnly?: Boolean; + ignoreFutureReleases?: Boolean; } interface VersionInfo { diff --git a/index.js b/index.js index c2c8671..8d890d0 100644 --- a/index.js +++ b/index.js @@ -8,12 +8,14 @@ module.exports = async function (alias = 'lts_active', opts = {}) { const cache = opts.cache || _cache const mirror = opts.mirror || 'https://nodejs.org/dist/' const latestOfMajorOnly = opts.latestOfMajorOnly || false + const ignoreFutureReleases = opts.ignoreFutureReleases || false const a = Array.isArray(alias) ? alias : [alias] const versions = await getLatestVersionsByCodename({ now, cache, - mirror + mirror, + ignoreFutureReleases }) // Reduce to an object @@ -74,7 +76,7 @@ function getVersions (cache, mirror) { }).json() } -async function getLatestVersionsByCodename ({ now, cache, mirror }) { +async function getLatestVersionsByCodename ({ now, cache, mirror, ignoreFutureReleases }) { const schedule = await getSchedule(cache) const versions = await getVersions(cache, mirror) @@ -120,6 +122,11 @@ async function getLatestVersionsByCodename ({ now, cache, mirror }) { } } + // This version is from future; completely ignore it (i.e. we may have specified a `now` from the past) + if (ignoreFutureReleases && now < v.releaseDate) { + return obj + } + // Is in any supported period if (now > v.start && now < v.end) { v.isSupported = true diff --git a/test/index.js b/test/index.js index 516352c..9b95d8c 100644 --- a/test/index.js +++ b/test/index.js @@ -188,4 +188,20 @@ suite('nv', () => { assert.strictEqual(versions[0].major, 0) assert.strictEqual(versions[0].isLts, false) }) + + test('ignoreFutureReleases=false', async () => { + const versions = await nv('v8', { now, ignoreFutureReleases: false }) + assert.strictEqual(versions.length, 1) + assert.strictEqual(versions[0].major, 8) + assert.strictEqual(versions[0].minor, 17) + assert.strictEqual(versions[0].patch, 0) + }) + + test('ignoreFutureReleases=true', async () => { + const versions = await nv('v8', { now, ignoreFutureReleases: true }) + assert.strictEqual(versions.length, 1) + assert.strictEqual(versions[0].major, 8) + assert.strictEqual(versions[0].minor, 16) + assert.strictEqual(versions[0].patch, 1) + }) })