Skip to content

Commit 09090e8

Browse files
committed
fixup: set json config in pkg get explicitly
1 parent 1d64884 commit 09090e8

File tree

4 files changed

+66
-51
lines changed

4 files changed

+66
-51
lines changed

lib/commands/pkg.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Pkg extends BaseCommand {
5656
}
5757

5858
async get (args, { path, workspace }) {
59+
this.npm.config.set('json', true)
5960
const pkgJson = await PackageJson.load(path)
6061

6162
let unwrap = false

lib/npm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class Npm {
301301

302302
output.flush({
303303
[META]: true,
304+
json: this.config.get('json'),
304305
jsonError: jsonError(err, this),
305306
})
306307

lib/utils/display.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ class Display {
172172
#command
173173
#levelIndex
174174
#timing
175-
#json
175+
#jsonConfig
176+
#jsonOutput
176177
#heading
177178
#silent
178179

@@ -208,6 +209,11 @@ class Display {
208209
}
209210
}
210211

212+
// We are in json mode if set from the config or if jsonOutput is set via a handler
213+
get #json () {
214+
return this.#jsonConfig || this.#jsonOutput
215+
}
216+
211217
async load ({
212218
command,
213219
heading,
@@ -239,7 +245,7 @@ class Display {
239245
this.#command = command
240246
this.#levelIndex = LEVEL_OPTIONS[loglevel].index
241247
this.#timing = timing
242-
this.#json = ['pkg'].includes(command) ? true : json
248+
this.#jsonConfig = json
243249
this.#heading = heading
244250
this.#silent = this.#levelIndex <= 0
245251

@@ -290,6 +296,7 @@ class Display {
290296
// Arrow function assigned to a private class field so it can be passed
291297
// directly as a listener and still reference "this"
292298
#outputHandler = withMeta((level, meta, ...args) => {
299+
this.#jsonOutput = meta.json
293300
switch (level) {
294301
case output.KEYS.flush: {
295302
this.#outputState.buffering = false

test/lib/commands/pkg.js

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ t.test('delete nested field', async t => {
578578
})
579579

580580
t.test('workspaces', async t => {
581-
const { pkg, OUTPUT, readPackageJson } = await mockNpm(t, {
581+
const mockWorkspaces = (t) => mockNpm(t, {
582582
prefixDir: {
583583
'package.json': JSON.stringify({
584584
name: 'root',
@@ -605,64 +605,70 @@ t.test('workspaces', async t => {
605605
config: { workspaces: true },
606606
})
607607

608-
await pkg('get', 'name', 'version')
608+
t.test('get', async t => {
609+
const { pkg, OUTPUT } = await mockWorkspaces(t)
610+
await pkg('get', 'name', 'version')
611+
t.strictSame(
612+
JSON.parse(OUTPUT()),
613+
{
614+
a: {
615+
name: 'a',
616+
version: '1.0.0',
617+
},
618+
b: {
619+
name: 'b',
620+
version: '1.2.3',
621+
},
622+
},
623+
'should return expected result for configured workspaces'
624+
)
625+
})
609626

610-
t.strictSame(
611-
JSON.parse(OUTPUT()),
612-
{
613-
a: {
627+
t.test('set', async t => {
628+
const { pkg, readPackageJson } = await mockWorkspaces(t)
629+
630+
await pkg('set', 'funding=http://example.com')
631+
632+
t.strictSame(
633+
readPackageJson('packages/a'),
634+
{
614635
name: 'a',
615636
version: '1.0.0',
637+
funding: 'http://example.com',
616638
},
617-
b: {
639+
'should add field to workspace a'
640+
)
641+
642+
t.strictSame(
643+
readPackageJson('packages/b'),
644+
{
618645
name: 'b',
619646
version: '1.2.3',
647+
funding: 'http://example.com',
620648
},
621-
},
622-
'should return expected result for configured workspaces'
623-
)
624-
625-
await pkg('set', 'funding=http://example.com')
626-
627-
t.strictSame(
628-
readPackageJson('packages/a'),
629-
{
630-
name: 'a',
631-
version: '1.0.0',
632-
funding: 'http://example.com',
633-
},
634-
'should add field to workspace a'
635-
)
636-
637-
t.strictSame(
638-
readPackageJson('packages/b'),
639-
{
640-
name: 'b',
641-
version: '1.2.3',
642-
funding: 'http://example.com',
643-
},
644-
'should add field to workspace b'
645-
)
649+
'should add field to workspace b'
650+
)
646651

647-
await pkg('delete', 'version')
652+
await pkg('delete', 'version')
648653

649-
t.strictSame(
650-
readPackageJson('packages/a'),
651-
{
652-
name: 'a',
653-
funding: 'http://example.com',
654-
},
655-
'should delete version field from workspace a'
656-
)
654+
t.strictSame(
655+
readPackageJson('packages/a'),
656+
{
657+
name: 'a',
658+
funding: 'http://example.com',
659+
},
660+
'should delete version field from workspace a'
661+
)
657662

658-
t.strictSame(
659-
readPackageJson('packages/b'),
660-
{
661-
name: 'b',
662-
funding: 'http://example.com',
663-
},
664-
'should delete version field from workspace b'
665-
)
663+
t.strictSame(
664+
readPackageJson('packages/b'),
665+
{
666+
name: 'b',
667+
funding: 'http://example.com',
668+
},
669+
'should delete version field from workspace b'
670+
)
671+
})
666672
})
667673

668674
t.test('single workspace', async t => {

0 commit comments

Comments
 (0)