Skip to content

Commit ab54475

Browse files
Sreeram Jayanisaacs
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 3b96b23 commit ab54475

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

lib/outdated.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ 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 || list.length === 0 && !opts.json) return cb(er, list)
105105
if (opts.json) {
106106
output(makeJSON(list, opts))
107107
} else if (opts.parseable) {
@@ -129,7 +129,7 @@ function outdated (args, silent, cb) {
129129
}
130130
output(table(outTable, tableOpts))
131131
}
132-
process.exitCode = 1
132+
process.exitCode = list.length ? 1 : 0
133133
cb(null, list.map(function (item) { return [item[0].parent.path].concat(item.slice(1, 7)) }))
134134
})
135135
}))

test/tap/outdated-json.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ var expected = {
4242
}
4343

4444
test('setup', function (t) {
45-
cleanup()
46-
mkdirp.sync(pkg)
4745
fs.writeFileSync(
4846
path.join(pkg, 'package.json'),
4947
JSON.stringify(json, null, 2)
@@ -92,14 +90,37 @@ test('it should log json data', function (t) {
9290
)
9391
})
9492

93+
test('it should log json data even when the list is empty', function (t) {
94+
common.npm(
95+
[
96+
'rm',
97+
'request',
98+
'underscore',
99+
],
100+
EXEC_OPTS,
101+
function (er, code, stdout) {
102+
t.ifError(er, 'run without error')
103+
t.is(code, 0, 'successful exit status')
104+
common.npm(
105+
[
106+
'--registry', common.registry,
107+
'--silent',
108+
'--json',
109+
'outdated',
110+
],
111+
EXEC_OPTS,
112+
function (err, code, stdout) {
113+
t.ifError(er, 'run without error')
114+
t.is(code, 0, 'successful exit status')
115+
t.same(JSON.parse(stdout), {}, 'got an empty object printed')
116+
t.end()
117+
}
118+
)
119+
}
120+
)
121+
})
122+
95123
test('cleanup', function (t) {
96124
server.close()
97-
cleanup()
98125
t.end()
99126
})
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)