Skip to content

Commit 98fb2a9

Browse files
ljharbwesleytodd
authored andcommitted
feat: add --only-version option to skip JSON and only print version number
1 parent 302ff80 commit 98fb2a9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

bin/nv

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ require('yargs')
2323
default: false,
2424
description: 'Only show latest version in each semver major range'
2525
})
26+
yargs.option('only-version', {
27+
type: 'boolean',
28+
description: 'show version number only'
29+
})
2630
},
2731
handler: (argv) => {
2832
nv(argv.versions, {
@@ -31,7 +35,9 @@ require('yargs')
3135
})
3236
.then(result => {
3337
result.forEach(r => {
34-
if (argv.prettyJson === false) {
38+
if (argv['only-version']) {
39+
console.log(r.version)
40+
} else if (argv.prettyJson === false) {
3541
console.log(JSON.stringify(r))
3642
} else if (!isNaN(parseInt(argv.prettyJson, 10))) {
3743
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 }).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 })
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
test('should only contain the latest of each major', () => {

0 commit comments

Comments
 (0)