Skip to content

Commit 7f1f2b7

Browse files
fix: keep dry-run output valid json (#9928)
Backport of #9823 to `release/v11`. Co-authored-by: martin <martin.ruiz.mares@gmail.com> Copilot-Session: 48e629f8-eb5a-467c-970b-feb313628c00
1 parent f28cca9 commit 7f1f2b7

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎lib/utils/reify-output.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ const reifyOutput = (npm, arb, extras = {}) => {
4444
}
4545

4646
if (diff) {
47-
const showDiff = npm.config.get('dry-run') || npm.config.get('long')
47+
const showDiff = !npm.flatOptions.json &&
48+
(npm.config.get('dry-run') || npm.config.get('long'))
4849
const chalk = npm.chalk
4950

5051
depth({

‎test/lib/utils/reify-output.js‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,53 @@ t.test('prints dedupe difference on dry-run', async t => {
469469
t.matchSnapshot(out, 'diff table')
470470
})
471471

472+
t.test('prints only json for dry-run and long', async t => {
473+
for (const flag of ['dry-run', 'long']) {
474+
await t.test(flag, async t => {
475+
const out = await mockReify(t, {
476+
actualTree: {
477+
inventory: {
478+
has: () => true,
479+
},
480+
children: [],
481+
},
482+
diff: {
483+
children: [
484+
{
485+
action: 'ADD',
486+
ideal: {
487+
path: 'test/foo',
488+
name: 'foo',
489+
package: { version: '1.0.0' },
490+
},
491+
},
492+
],
493+
},
494+
}, {
495+
[flag]: true,
496+
json: true,
497+
})
498+
499+
t.strictSame(JSON.parse(out), {
500+
add: [
501+
{
502+
name: 'foo',
503+
version: '1.0.0',
504+
path: 'test/foo',
505+
},
506+
],
507+
added: 1,
508+
audited: 0,
509+
change: [],
510+
changed: 0,
511+
funding: 0,
512+
remove: [],
513+
removed: 0,
514+
})
515+
})
516+
}
517+
})
518+
472519
t.test('prints dedupe difference on long', async t => {
473520
const mock = {
474521
actualTree: {

0 commit comments

Comments
 (0)