Skip to content

Commit

Permalink
feat(format): print --dry-run diffs in table format (#7174)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritaaktay committed Jan 23, 2024
1 parent 332ed08 commit dffca29
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
42 changes: 32 additions & 10 deletions lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ms = require('ms')
const npmAuditReport = require('npm-audit-report')
const { readTree: getFundingInfo } = require('libnpmfund')
const auditError = require('./audit-error.js')
const Table = require('cli-table3')

// TODO: output JSON if flatOptions.json is true
const reifyOutput = (npm, arb) => {
Expand Down Expand Up @@ -104,31 +105,52 @@ const printAuditReport = (npm, report) => {

// print the diff tree of actions that would be taken
const printDiff = (npm, diff) => {
const msg = []
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]
msg.push('\n', child.action.toLowerCase(), '\t')
table[i] = [child.action.toLowerCase()]

switch (child.action) {
case 'ADD':
msg.push([child.ideal.name, child.ideal.package.version].join('\t'))
table[i].push(child.ideal.name, child.ideal.package.version)
break
case 'REMOVE':
msg.push([child.actual.name, child.actual.package.version].join('\t'))
table[i].push(child.actual.name, child.actual.package.version)
break
case 'CHANGE':
msg.push(
[
child.actual.name,
child.actual.package.version + ' -> ' + child.ideal.package.version,
].join('\t')
table[i].push(
child.actual.name,
child.actual.package.version + ' -> ' + child.ideal.package.version
)
break
}
}

npm.output(msg.join(''))
npm.output('\n' + table.toString())
}

const getAuditReport = (npm, report) => {
Expand Down
9 changes: 9 additions & 0 deletions tap-snapshots/test/lib/utils/reify-output.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,3 +1632,12 @@ 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
removed 1 package, and changed 1 package in {TIME}
`
18 changes: 1 addition & 17 deletions test/lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,21 +406,5 @@ t.test('prints dedupe difference', async t => {
'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'
)
t.matchSnapshot(out, 'diff table')
})

0 comments on commit dffca29

Please sign in to comment.