From a1c9b9b8def57ace50390126f8865104f52d25de Mon Sep 17 00:00:00 2001 From: Blue Mouse Date: Sat, 13 Jan 2024 00:12:41 +0000 Subject: [PATCH 1/2] Added pretty print for dedupe command with --dry-run flag --- lib/utils/reify-output.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/utils/reify-output.js b/lib/utils/reify-output.js index 3b79fc2be1898..30c2428679183 100644 --- a/lib/utils/reify-output.js +++ b/lib/utils/reify-output.js @@ -41,6 +41,10 @@ const reifyOutput = (npm, arb) => { } if (diff) { + if (npm.config.get('dry-run')) { + printDiff(npm, diff) + } + depth({ tree: diff, visit: d => { @@ -98,6 +102,35 @@ const printAuditReport = (npm, report) => { npm.output(`\n${res.report}`) } +// print the diff tree of actions that would be taken +const printDiff = (npm, diff) => { + const msg = [] + + for (let i = 0; i < diff.children.length; ++i) { + const child = diff.children[i] + msg.push('\n', child.action.toLowerCase(), '\t') + + switch (child.action) { + case 'ADD': + msg.push([child.ideal.name, child.ideal.package.version].join('\t')) + break + case 'REMOVE': + msg.push([child.actual.name, child.actual.package.version].join('\t')) + break + case 'CHANGE': + msg.push( + [ + child.actual.name, + child.actual.package.version + ' -> ' + child.ideal.package.version, + ].join('\t') + ) + break + } + } + + npm.output(msg.join('')) +} + const getAuditReport = (npm, report) => { if (!report) { return From cba6b5d33840246356c0fbaa5c3041ac691c2449 Mon Sep 17 00:00:00 2001 From: Blue Mouse Date: Sat, 13 Jan 2024 00:12:49 +0000 Subject: [PATCH 2/2] pretty print test coverage --- test/lib/utils/reify-output.js | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/lib/utils/reify-output.js b/test/lib/utils/reify-output.js index 1c6215ab33bef..192840b90d2de 100644 --- a/test/lib/utils/reify-output.js +++ b/test/lib/utils/reify-output.js @@ -380,3 +380,47 @@ t.test('added packages should be looked up within returned tree', async t => { t.matchSnapshot(out) }) }) + +t.test('prints dedupe difference', async t => { + const mock = { + actualTree: { + name: 'foo', + inventory: { + has: () => false, + }, + }, + diff: { + children: [ + { action: 'ADD', ideal: { name: 'foo', package: { version: '1.0.0' } } }, + { action: 'REMOVE', actual: { name: 'bar', package: { version: '1.0.0' } } }, + { + action: 'CHANGE', + actual: { name: 'bar', package: { version: '1.0.0' } }, + ideal: { package: { version: '2.1.0' } }, + }, + ], + }, + } + + const out = await mockReify(t, mock, { + 'dry-run': true, + }) + + t.match( + out, + 'add\tfoo\t1.0.0', + 'should print added package' + ) + + t.match( + out, + 'remove\tbar\t1.0.0', + 'should print removed package' + ) + + t.match( + out, + 'change\tbar\t1.0.0 -> 2.1.0', + 'should print changed package' + ) +})