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 --only-version option to skip JSON and only print version number #36

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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
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))
})
})
})