Skip to content

Commit a5dd9af

Browse files
ljharbwesleytodd
authored andcommitted
feat: add --only-version option to skip JSON and only print version number
1 parent 59fd39a commit a5dd9af

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

bin/nv

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ require('yargs')
1818
defaultDescription: 'pretty print json spaces, default 2 (--no-pretty-json for new line delimted json)',
1919
description: 'Pretty print json'
2020
})
21+
yargs.option('only-version', {
22+
type: 'boolean',
23+
description: 'show version number only'
24+
})
2125
},
2226
handler: (argv) => {
2327
nv(argv.versions, {
2428
mirror: argv.mirror
2529
})
2630
.then(result => {
2731
result.forEach(r => {
28-
if (argv.prettyJson === false) {
32+
if (argv['only-version']) {
33+
console.log(r.version)
34+
} else if (argv.prettyJson === false) {
2935
console.log(JSON.stringify(r))
3036
} else if (!isNaN(parseInt(argv.prettyJson, 10))) {
3137
console.log(JSON.stringify(r, null, parseInt(argv.prettyJson, 10)))

test/cli.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const assert = require('assert')
33
const { suite, test } = require('mocha')
44
const { execFileSync } = require('child_process')
55
const path = require('path')
6+
const semver = require('semver')
67

78
const nv = path.join(__dirname, '..', 'bin', 'nv')
89
const cwd = path.join(__dirname, '..')
@@ -17,14 +18,26 @@ suite('nv cli', () => {
1718
const result = JSON.parse(execFileSync(nv, ['ls', '8'], { cwd: cwd }).toString())
1819
assert.strictEqual(result.codename, 'carbon')
1920
})
21+
2022
test('should contain output newline json', () => {
2123
const result = execFileSync(nv, ['ls', '8.x', '--no-pretty-json'], { cwd: cwd })
2224
.toString().trim().split('\n')
2325
.map((line) => JSON.parse(line))
2426

2527
assert(Array.isArray(result))
2628
result.forEach((r) => {
27-
assert(r.version.startsWith('8.'))
29+
assert(semver.satisfies(r.version, '8.x'))
30+
})
31+
})
32+
33+
test('only outputs the version number', () => {
34+
const result = execFileSync(nv, ['ls', '8.x', '--only-version'], { cwd: cwd })
35+
.toString().trim().split('\n')
36+
37+
assert(Array.isArray(result))
38+
result.forEach((r) => {
39+
assert(semver.satisfies(r, '8.x'))
40+
assert(semver.valid(r))
2841
})
2942
})
3043
})

0 commit comments

Comments
 (0)