Skip to content
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
15 changes: 1 addition & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,4 @@ jobs:
- name: npm lint
run: npm run lint
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 13.x, 14.x, 16.x, 18.x, 20.x, 22.x, 24.x]
steps:
- uses: actions/checkout@v5
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v5
with:
node-version: ${{ matrix.node-version }}
- name: npm install
run: npm install
- name: npm test
run: npm run test
uses: pkgjs/action/.github/workflows/node-test.yaml@v0
103 changes: 103 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,109 @@ console.log(versions.map((v) => v.version))
*/
```

## Command line interface (CLI)

Options:
- `--only-version`: instead of the entire output, only print one version number per line
- `--engines`: only print versions that match the current directory's `engines.node` field
- `--engines=lts`: only print LTS versions that also match the current directory's `engines.node` field

```sh
$ nv ls lts
{
"version": "18.20.4",
"major": 18,
"minor": 20,
"patch": 4,
"tag": "",
"codename": "hydrogen",
"versionName": "v18",
"start": "2022-04-19T00:00:00.000Z",
"lts": "2022-10-25T00:00:00.000Z",
"maintenance": "2023-10-18T00:00:00.000Z",
"end": "2025-04-30T00:00:00.000Z",
"releaseDate": "2024-07-08T00:00:00.000Z",
"isLts": true,
"files": [
"aix-ppc64",
"headers",
"linux-arm64",
"linux-armv7l",
"linux-ppc64le",
"linux-s390x",
"linux-x64",
"osx-arm64-tar",
"osx-x64-pkg",
"osx-x64-tar",
"src",
"win-x64-7z",
"win-x64-exe",
"win-x64-msi",
"win-x64-zip",
"win-x86-7z",
"win-x86-exe",
"win-x86-msi",
"win-x86-zip"
],
"dependencies": {
"npm": "10.7.0",
"v8": "10.2.154.26",
"uv": "1.44.2",
"zlib": "1.3.0.1-motley",
"openssl": "3.0.13+quic"
}
}
{
"version": "20.15.1",
"major": 20,
"minor": 15,
"patch": 1,
"tag": "",
"codename": "iron",
"versionName": "v20",
"start": "2023-04-18T00:00:00.000Z",
"lts": "2023-10-24T00:00:00.000Z",
"maintenance": "2024-10-22T00:00:00.000Z",
"end": "2026-04-30T00:00:00.000Z",
"releaseDate": "2024-07-08T00:00:00.000Z",
"isLts": true,
"files": [
"aix-ppc64",
"headers",
"linux-arm64",
"linux-armv7l",
"linux-ppc64le",
"linux-s390x",
"linux-x64",
"osx-arm64-tar",
"osx-x64-pkg",
"osx-x64-tar",
"src",
"win-arm64-7z",
"win-arm64-zip",
"win-x64-7z",
"win-x64-exe",
"win-x64-msi",
"win-x64-zip",
"win-x86-7z",
"win-x86-exe",
"win-x86-msi",
"win-x86-zip"
],
"dependencies": {
"npm": "10.7.0",
"v8": "11.3.244.8",
"uv": "1.46.0",
"zlib": "1.3.0.1-motley",
"openssl": "3.0.13+quic"
}
}

$ nv ls lts --only-version
18.20.4
20.15.1
```

## Supported Aliases

**Support Aliases**
Expand Down
15 changes: 13 additions & 2 deletions bin/nv
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,26 @@ require('yargs')
default: false,
description: 'Only show latest version in each semver major range'
})
yargs.option('only-version', {
type: 'boolean',
description: 'show version number only'
})
yargs.option('engines', {
type: 'string',
description: 'read the value of `engines.node`. if a value is provided, and it satisfies, the version is shown; if not, the max satisfying version is shown'
})
},
handler: (argv) => {
nv(argv.versions, {
engines: argv.engines,
mirror: argv.mirror,
latestOfMajorOnly: argv.latestOfMajorOnly
})
.then(result => {
result.forEach(r => {
if (argv.prettyJson === false) {
if (argv['only-version']) {
console.log(r.version)
} else if (argv.prettyJson === false) {
console.log(JSON.stringify(r))
} else if (!isNaN(parseInt(argv.prettyJson, 10))) {
console.log(JSON.stringify(r, null, parseInt(argv.prettyJson, 10)))
Expand All @@ -41,7 +52,7 @@ require('yargs')
})
}).catch(e => {
console.error(e)
process.exitCode = e.code || 1
process.exitCode ||= e.code || 1
})
}
})
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface VersionInfo {
major: number;
minor: number;
patch: number;
tag: string;
prerelease?: string[];
codename: string;
versionName: string;
start?: Date;
Expand Down
29 changes: 20 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict'
const { readFile } = require('fs/promises')
const { join } = require('path')

const got = require('got')
const semver = require('semver')
const _cache = new Map()
Expand All @@ -9,6 +12,8 @@ module.exports = async function (alias = 'lts_active', opts = {}) {
const mirror = opts.mirror || 'https://nodejs.org/dist/'
const latestOfMajorOnly = opts.latestOfMajorOnly || false
const ignoreFutureReleases = opts.ignoreFutureReleases || false
const engines = opts.engines
const cwd = opts.cwd || process.cwd()

const a = Array.isArray(alias) ? alias : [alias]
const versions = await getLatestVersionsByCodename({
Expand All @@ -31,6 +36,18 @@ module.exports = async function (alias = 'lts_active', opts = {}) {
return m
}, {})

if (typeof engines === 'string' || engines === true) {
const { engines: { node } = { node: '*' } } = JSON.parse(await readFile(join(cwd, 'package.json'), 'utf8'))

m = Object.fromEntries(Object.entries(m).filter(([version]) => semver.satisfies(version, node)))

const matching = Object.entries(m).filter(([version]) => semver.satisfies(version, engines))

if (matching.length > 0) {
m = Object.fromEntries(matching)
}
}

// If only latest major is true, filter out all but latest
if (latestOfMajorOnly) {
const vers = Object.values(m).reduce((latestMajor, v) => {
Expand Down Expand Up @@ -87,10 +104,9 @@ async function getLatestVersionsByCodename ({ now, cache, mirror, ignoreFutureRe
const lts = {}

const aliases = versions.reduce((obj, ver) => {
const { major, minor, patch, tag } = splitVersion(ver.version)
const { version, major, minor, patch, prerelease } = semver.parse(ver.version)
const versionName = major !== '0' ? `v${major}` : `v${major}.${minor}`
const codename = ver.lts ? ver.lts.toLowerCase() : versionName
const version = tag !== '' ? `${major}.${minor}.${patch}-${tag}` : `${major}.${minor}.${patch}`
const codename = ver.lts ? ver.lts.toLowerCase() : null
const s = schedule[versionName]

// Version Object
Expand All @@ -99,7 +115,7 @@ async function getLatestVersionsByCodename ({ now, cache, mirror, ignoreFutureRe
major,
minor,
patch,
tag,
prerelease,
codename,
versionName,
start: s && s.start && new Date(s.start),
Expand Down Expand Up @@ -215,8 +231,3 @@ async function getLatestVersionsByCodename ({ now, cache, mirror, ignoreFutureRe

return aliases
}

function splitVersion (ver) {
const [, major, minor, patch, tag] = /^v([0-9]*)\.([0-9]*)\.([0-9]*)(?:-([0-9A-Za-z-_]+))?/.exec(ver).map((n, i) => i < 4 ? parseInt(n, 10) : n || '')
return { major, minor, patch, tag }
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import assert from 'node:assert'
assert(versions[0].major)
assert(versions[0].minor)
assert(versions[0].patch)
assert(versions[0].tag)
assert(!versions[0].prerelease)
assert(versions[0].codename)
assert(versions[0].versionName)
assert(versions[0].start)
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@
"yargs": "^16.2.0"
},
"devDependencies": {
"gen-esm-wrapper": "^1.1.0",
"mocha": "^9.2.2",
"gen-esm-wrapper": "^1.1.3",
"mocha": "^10.6.0",
"standard": "^17.1.2",
"standard-version": "^9.3.2",
"tsd": "^0.27.0"
"standard-version": "^9.5.0",
"tsd": "^0.31.1"
},
"bin": {
"nv": "./bin/nv"
},
"engines": {
"node": "^18 || ^20 || ^22"
}
}
74 changes: 73 additions & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const assert = require('assert')
const { suite, test } = require('mocha')
const { execFileSync } = require('child_process')
const path = require('path')
const semver = require('semver')

const nv = path.join(__dirname, '..', 'bin', 'nv')
const cwd = path.join(__dirname, '..')
Expand All @@ -17,16 +18,29 @@ suite('nv cli', () => {
const result = JSON.parse(execFileSync(nv, ['ls', '8'], { cwd }).toString())
assert.strictEqual(result.codename, 'carbon')
})

test('should contain output newline json', () => {
const result = execFileSync(nv, ['ls', '8.x', '--no-pretty-json'], { cwd })
.toString().trim().split('\n')
.map((line) => JSON.parse(line))

assert(Array.isArray(result))
result.forEach((r) => {
assert(r.version.startsWith('8.'))
assert(semver.satisfies(r.version, '8.x'))
})
})

test('only outputs the version number', () => {
const result = execFileSync(nv, ['ls', '8.x', '--only-version'], { cwd: cwd })
.toString().trim().split('\n')

assert(Array.isArray(result))
result.forEach((r) => {
assert(semver.satisfies(r, '8.x'))
assert(semver.valid(r))
})
})

test('should only contain the latest of each major', () => {
const result = execFileSync(nv, ['ls', '16.x || 18.x', '--no-pretty-json', '--latest-of-major-only'], { cwd })
.toString().trim().split('\n')
Expand All @@ -37,4 +51,62 @@ suite('nv cli', () => {
assert(result[0].version.startsWith('16.'))
assert(result[1].version.startsWith('18.'))
})

test('works with `--engines`', () => {
const result = execFileSync(nv, ['ls', '8.x', '--only-version', '--engines=">=8"'], { cwd: path.join(__dirname, 'fixtures', 'engines') })
.toString().trim().split('\n')

assert.deepEqual(result, [
'8.10.0',
'8.11.0',
'8.11.1',
'8.11.2',
'8.11.3',
'8.11.4',
'8.12.0',
'8.13.0',
'8.14.0',
'8.14.1',
'8.15.0',
'8.15.1',
'8.16.0',
'8.16.1',
'8.16.2',
'8.17.0'
])

const result2 = execFileSync(nv, ['ls', '8.x', '--only-version', '--engines=">=8.15"'], { cwd: path.join(__dirname, 'fixtures', 'engines') })
.toString().trim().split('\n')

assert.deepEqual(result2, [
'8.15.0',
'8.15.1',
'8.16.0',
'8.16.1',
'8.16.2',
'8.17.0'
])

const result3 = execFileSync(nv, ['ls', '8.x', '--only-version', '--engines'], { cwd: path.join(__dirname, 'fixtures', 'engines') })
.toString().trim().split('\n')

assert.deepEqual(result3, [
'8.10.0',
'8.11.0',
'8.11.1',
'8.11.2',
'8.11.3',
'8.11.4',
'8.12.0',
'8.13.0',
'8.14.0',
'8.14.1',
'8.15.0',
'8.15.1',
'8.16.0',
'8.16.1',
'8.16.2',
'8.17.0'
])
})
})
5 changes: 5 additions & 0 deletions test/fixtures/engines/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"engines": {
"node": "^8.10"
}
}
Loading