From 87fef4e356f30941f2ee39f2f774ad2086239a00 Mon Sep 17 00:00:00 2001 From: Sreeram Jayan Date: Wed, 20 Mar 2019 10:06:06 -0500 Subject: [PATCH] fix: Always return JSON for outdated --json Close: https://github.com/npm/cli/pull/176 EDIT: Added test, do not set exitStatus to 1 if we're just printing an empty list as JSON. -- @isaacs --- lib/outdated.js | 7 +++++-- test/tap/outdated-json.js | 42 ++++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/lib/outdated.js b/lib/outdated.js index 17b9414611d7c..bb4c346f9a654 100644 --- a/lib/outdated.js +++ b/lib/outdated.js @@ -101,7 +101,10 @@ function outdated (args, silent, cb) { return aa[0].path.localeCompare(bb[0].path) || aa[1].localeCompare(bb[1]) }) - if (er || silent || list.length === 0) return cb(er, list) + if (er || silent || + (list.length === 0 && !opts.json)) { + return cb(er, list) + } if (opts.json) { output(makeJSON(list, opts)) } else if (opts.parseable) { @@ -129,7 +132,7 @@ function outdated (args, silent, cb) { } output(table(outTable, tableOpts)) } - process.exitCode = 1 + process.exitCode = list.length ? 1 : 0 cb(null, list.map(function (item) { return [item[0].parent.path].concat(item.slice(1, 7)) })) }) })) diff --git a/test/tap/outdated-json.js b/test/tap/outdated-json.js index e0040d0285f2f..77db52cc72521 100644 --- a/test/tap/outdated-json.js +++ b/test/tap/outdated-json.js @@ -1,10 +1,7 @@ var fs = require('graceful-fs') var path = require('path') -var mkdirp = require('mkdirp') var mr = require('npm-registry-mock') -var osenv = require('osenv') -var rimraf = require('rimraf') var test = require('tap').test var common = require('../common-tap.js') @@ -42,8 +39,6 @@ var expected = { } test('setup', function (t) { - cleanup() - mkdirp.sync(pkg) fs.writeFileSync( path.join(pkg, 'package.json'), JSON.stringify(json, null, 2) @@ -92,14 +87,37 @@ test('it should log json data', function (t) { ) }) +test('it should log json data even when the list is empty', function (t) { + common.npm( + [ + 'rm', + 'request', + 'underscore' + ], + EXEC_OPTS, + function (er, code, stdout) { + t.ifError(er, 'run without error') + t.is(code, 0, 'successful exit status') + common.npm( + [ + '--registry', common.registry, + '--silent', + '--json', + 'outdated' + ], + EXEC_OPTS, + function (er, code, stdout) { + t.ifError(er, 'run without error') + t.is(code, 0, 'successful exit status') + t.same(JSON.parse(stdout), {}, 'got an empty object printed') + t.end() + } + ) + } + ) +}) + test('cleanup', function (t) { server.close() - cleanup() t.end() }) - -function cleanup () { - // windows fix for locked files - process.chdir(osenv.tmpdir()) - rimraf.sync(pkg) -}