Skip to content

Commit 780afc5

Browse files
authored
fix(pkg): display if any of multiple attributes exist (#7855)
<!-- What / Why --> <!-- Describe the request in detail. What it does and why it's being changed. --> See #7854 for the why and more details. I changed the unwrap logic so that it only runs when the result contains one entry and the amount of arguments is one. Otherwise, when multiple arguments are provided the unwrap results in an undefined. Additionally, I think it makes sense to skip the unwrap logic when the result contains one entry and pkg has multiple arguments because you would expect an object. In the existing comment above the if-statement, it is mentioned the CLI should not unwrap at all. I chose not to do this because it is not clear to me if this is a final decision and has a large impact. However, if it is the desired direction, I am happy to do the work. ## References Fixes #7854
1 parent ecd2d23 commit 780afc5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/commands/pkg.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ class Pkg extends BaseCommand {
6363

6464
if (args.length) {
6565
result = new Queryable(result).query(args)
66-
// in case there's only a single result from the query
67-
// just prints that one element to stdout
66+
// in case there's only a single argument and a single result from the query
67+
// just prints that one element to stdout.
6868
// TODO(BREAKING_CHANGE): much like other places where we unwrap single
6969
// item arrays this should go away. it makes the behavior unknown for users
7070
// who don't already know the shape of the data.
71-
if (Object.keys(result).length === 1) {
71+
if (Object.keys(result).length === 1 && args.length === 1) {
7272
result = result[args]
7373
}
7474
}

test/lib/commands/pkg.js

+20
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ t.test('get multiple arg', async t => {
108108
)
109109
})
110110

111+
t.test('get multiple arg with only one arg existing', async t => {
112+
const { pkg, OUTPUT } = await mockNpm(t, {
113+
prefixDir: {
114+
'package.json': JSON.stringify({
115+
name: 'foo',
116+
}),
117+
},
118+
})
119+
120+
await pkg('get', 'name', 'version', 'dependencies')
121+
122+
t.strictSame(
123+
JSON.parse(OUTPUT()),
124+
{
125+
name: 'foo',
126+
},
127+
'should print retrieved package.json field'
128+
)
129+
})
130+
111131
t.test('get multiple arg with empty value', async t => {
112132
const { pkg, OUTPUT } = await mockNpm(t, {
113133
prefixDir: {

0 commit comments

Comments
 (0)