Skip to content

feat(format): print --dry-run diffs in table format #7174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})