Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to ignore future releases #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ interface Options {
now?: Date;
cache?: Map<any, any>;
mirror?: string;
latestOfMajorOnly?: Boolean
latestOfMajorOnly?: Boolean;
ignoreFutureReleases?: Boolean;
}

interface VersionInfo {
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ 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)
const versions = await getLatestVersionsByCodename(now, cache, mirror, ignoreFutureReleases)

// Reduce to an object
let m = a.reduce((m, a) => {
Expand Down Expand Up @@ -70,7 +71,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)

Expand Down Expand Up @@ -112,6 +113,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
}

// All versions get added to all
obj.all.push(v)

Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,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)
})
})