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

fix(pkg): display if any of multiple attributes exist #7855

Merged
merged 1 commit into from
Oct 18, 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
6 changes: 3 additions & 3 deletions lib/commands/pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/lib/commands/pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Loading