Skip to content

Commit

Permalink
fix: inline diff table code w/ summary code
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jan 24, 2024
1 parent ec77e81 commit ec06f77
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 53 deletions.
86 changes: 35 additions & 51 deletions lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,51 @@ 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({
tree: diff,
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:
Expand All @@ -67,6 +97,10 @@ const reifyOutput = (npm, arb) => {
},
getChildren: d => d.children,
})

if (diffTable) {
npm.output('\n' + diffTable.toString())
}
}

if (npm.flatOptions.fund) {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tap-snapshots/test/lib/utils/reify-output.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
`

0 comments on commit ec06f77

Please sign in to comment.