Skip to content

Commit

Permalink
feat: add --only-version option to skip JSON and only print version…
Browse files Browse the repository at this point in the history
… number
  • Loading branch information
ljharb authored and wesleytodd committed Jul 16, 2024
1 parent 59fd39a commit a5dd9af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion bin/nv
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ require('yargs')
defaultDescription: 'pretty print json spaces, default 2 (--no-pretty-json for new line delimted json)',
description: 'Pretty print json'
})
yargs.option('only-version', {
type: 'boolean',
description: 'show version number only'
})
},
handler: (argv) => {
nv(argv.versions, {
mirror: argv.mirror
})
.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 Down
15 changes: 14 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,14 +18,26 @@ suite('nv cli', () => {
const result = JSON.parse(execFileSync(nv, ['ls', '8'], { cwd: cwd }).toString())
assert.strictEqual(result.codename, 'carbon')
})

test('should contain output newline json', () => {
const result = execFileSync(nv, ['ls', '8.x', '--no-pretty-json'], { cwd: 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))
})
})
})

0 comments on commit a5dd9af

Please sign in to comment.