Skip to content

Commit 87fef4e

Browse files
Sreeram Jayanisaacs
Sreeram Jayan
authored andcommitted
fix: Always return JSON for outdated --json
Close: #176 EDIT: Added test, do not set exitStatus to 1 if we're just printing an empty list as JSON. -- @isaacs
1 parent d9238af commit 87fef4e

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

lib/outdated.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ function outdated (args, silent, cb) {
101101
return aa[0].path.localeCompare(bb[0].path) ||
102102
aa[1].localeCompare(bb[1])
103103
})
104-
if (er || silent || list.length === 0) return cb(er, list)
104+
if (er || silent ||
105+
(list.length === 0 && !opts.json)) {
106+
return cb(er, list)
107+
}
105108
if (opts.json) {
106109
output(makeJSON(list, opts))
107110
} else if (opts.parseable) {
@@ -129,7 +132,7 @@ function outdated (args, silent, cb) {
129132
}
130133
output(table(outTable, tableOpts))
131134
}
132-
process.exitCode = 1
135+
process.exitCode = list.length ? 1 : 0
133136
cb(null, list.map(function (item) { return [item[0].parent.path].concat(item.slice(1, 7)) }))
134137
})
135138
}))

test/tap/outdated-json.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
var fs = require('graceful-fs')
22
var path = require('path')
33

4-
var mkdirp = require('mkdirp')
54
var mr = require('npm-registry-mock')
6-
var osenv = require('osenv')
7-
var rimraf = require('rimraf')
85
var test = require('tap').test
96

107
var common = require('../common-tap.js')
@@ -42,8 +39,6 @@ var expected = {
4239
}
4340

4441
test('setup', function (t) {
45-
cleanup()
46-
mkdirp.sync(pkg)
4742
fs.writeFileSync(
4843
path.join(pkg, 'package.json'),
4944
JSON.stringify(json, null, 2)
@@ -92,14 +87,37 @@ test('it should log json data', function (t) {
9287
)
9388
})
9489

90+
test('it should log json data even when the list is empty', function (t) {
91+
common.npm(
92+
[
93+
'rm',
94+
'request',
95+
'underscore'
96+
],
97+
EXEC_OPTS,
98+
function (er, code, stdout) {
99+
t.ifError(er, 'run without error')
100+
t.is(code, 0, 'successful exit status')
101+
common.npm(
102+
[
103+
'--registry', common.registry,
104+
'--silent',
105+
'--json',
106+
'outdated'
107+
],
108+
EXEC_OPTS,
109+
function (er, code, stdout) {
110+
t.ifError(er, 'run without error')
111+
t.is(code, 0, 'successful exit status')
112+
t.same(JSON.parse(stdout), {}, 'got an empty object printed')
113+
t.end()
114+
}
115+
)
116+
}
117+
)
118+
})
119+
95120
test('cleanup', function (t) {
96121
server.close()
97-
cleanup()
98122
t.end()
99123
})
100-
101-
function cleanup () {
102-
// windows fix for locked files
103-
process.chdir(osenv.tmpdir())
104-
rimraf.sync(pkg)
105-
}

0 commit comments

Comments
 (0)