diff --git a/lib/commands/pkg.js b/lib/commands/pkg.js index a011fc10be107..5a236f6e62270 100644 --- a/lib/commands/pkg.js +++ b/lib/commands/pkg.js @@ -63,12 +63,12 @@ class Pkg extends BaseCommand { if (args.length) { result = new Queryable(result).query(args) - // in case there's only a single result from the query - // just prints that one element to stdout + // in case there's only a single argument and a single result from the query + // just prints that one element to stdout. // TODO(BREAKING_CHANGE): much like other places where we unwrap single // item arrays this should go away. it makes the behavior unknown for users // who don't already know the shape of the data. - if (Object.keys(result).length === 1) { + if (Object.keys(result).length === 1 && args.length === 1) { result = result[args] } } diff --git a/test/lib/commands/pkg.js b/test/lib/commands/pkg.js index 2306fe10db025..f4d0278b04d9b 100644 --- a/test/lib/commands/pkg.js +++ b/test/lib/commands/pkg.js @@ -108,6 +108,26 @@ t.test('get multiple arg', async t => { ) }) +t.test('get multiple arg with only one arg existing', async t => { + const { pkg, OUTPUT } = await mockNpm(t, { + prefixDir: { + 'package.json': JSON.stringify({ + name: 'foo', + }), + }, + }) + + await pkg('get', 'name', 'version', 'dependencies') + + t.strictSame( + JSON.parse(OUTPUT()), + { + name: 'foo', + }, + 'should print retrieved package.json field' + ) +}) + t.test('get multiple arg with empty value', async t => { const { pkg, OUTPUT } = await mockNpm(t, { prefixDir: {