From ec06f7712bbdc3b38c546593e9970962fee127ed Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 23 Jan 2024 13:09:58 -0800 Subject: [PATCH] fix: inline diff table code w/ summary code --- lib/utils/reify-output.js | 86 ++++++++----------- .../test/lib/utils/reify-output.js.test.cjs | 4 +- 2 files changed, 37 insertions(+), 53 deletions(-) diff --git a/lib/utils/reify-output.js b/lib/utils/reify-output.js index ae7f4396c2c45..ff63f845e458b 100644 --- a/lib/utils/reify-output.js +++ b/lib/utils/reify-output.js @@ -42,8 +42,32 @@ const reifyOutput = (npm, arb) => { } if (diff) { + let diffTable if (npm.config.get('dry-run')) { - printDiff(npm, diff) + diffTable = new Table({ + chars: { + top: '', + 'top-mid': '', + 'top-left': '', + 'top-right': '', + bottom: '', + 'bottom-mid': '', + 'bottom-left': '', + 'bottom-right': '', + left: '', + 'left-mid': '', + mid: '', + 'mid-mid': '', + right: '', + 'right-mid': '', + middle: ' ', + }, + style: { + 'padding-left': 0, + 'padding-right': 0, + border: 0, + }, + }) } depth({ @@ -51,12 +75,18 @@ const reifyOutput = (npm, arb) => { visit: d => { switch (d.action) { case 'REMOVE': + diffTable?.push(['remove', d.actual.name, d.actual.package.version]) summary.removed++ break case 'ADD': + diffTable?.push(['add', d.ideal.name, d.ideal.package.version]) actualTree.inventory.has(d.ideal) && summary.added++ break case 'CHANGE': + diffTable?.push(['change', + d.actual.name, + d.actual.package.version + ' -> ' + d.ideal.package.version, + ]) summary.changed++ break default: @@ -67,6 +97,10 @@ const reifyOutput = (npm, arb) => { }, getChildren: d => d.children, }) + + if (diffTable) { + npm.output('\n' + diffTable.toString()) + } } if (npm.flatOptions.fund) { @@ -103,56 +137,6 @@ const printAuditReport = (npm, report) => { npm.output(`\n${res.report}`) } -// print the diff tree of actions that would be taken -const printDiff = (npm, diff) => { - const table = new Table({ - chars: { - top: '', - 'top-mid': '', - 'top-left': '', - 'top-right': '', - bottom: '', - 'bottom-mid': '', - 'bottom-left': '', - 'bottom-right': '', - left: '', - 'left-mid': '', - mid: '', - 'mid-mid': '', - right: '', - 'right-mid': '', - middle: ' ', - }, - style: { - 'padding-left': 0, - 'padding-right': 0, - border: 0, - }, - }) - - for (let i = 0; i < diff.children.length; ++i) { - const child = diff.children[i] - table[i] = [child.action.toLowerCase()] - - switch (child.action) { - case 'ADD': - table[i].push(child.ideal.name, child.ideal.package.version) - break - case 'REMOVE': - table[i].push(child.actual.name, child.actual.package.version) - break - case 'CHANGE': - table[i].push( - child.actual.name, - child.actual.package.version + ' -> ' + child.ideal.package.version - ) - break - } - } - - npm.output('\n' + table.toString()) -} - const getAuditReport = (npm, report) => { if (!report) { return diff --git a/tap-snapshots/test/lib/utils/reify-output.js.test.cjs b/tap-snapshots/test/lib/utils/reify-output.js.test.cjs index c698255588d66..09307271d6054 100644 --- a/tap-snapshots/test/lib/utils/reify-output.js.test.cjs +++ b/tap-snapshots/test/lib/utils/reify-output.js.test.cjs @@ -1635,9 +1635,9 @@ exports[`test/lib/utils/reify-output.js TAP packages changed message > {"added" exports[`test/lib/utils/reify-output.js TAP prints dedupe difference > diff table 1`] = ` -add foo 1.0.0 -remove bar 1.0.0 change bar 1.0.0 -> 2.1.0 +remove bar 1.0.0 +add foo 1.0.0 removed 1 package, and changed 1 package in {TIME} `