Skip to content

Commit dffca29

Browse files
authored
feat(format): print --dry-run diffs in table format (#7174)
1 parent 332ed08 commit dffca29

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

lib/utils/reify-output.js

+32-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const ms = require('ms')
1515
const npmAuditReport = require('npm-audit-report')
1616
const { readTree: getFundingInfo } = require('libnpmfund')
1717
const auditError = require('./audit-error.js')
18+
const Table = require('cli-table3')
1819

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

105106
// print the diff tree of actions that would be taken
106107
const printDiff = (npm, diff) => {
107-
const msg = []
108+
const table = new Table({
109+
chars: {
110+
top: '',
111+
'top-mid': '',
112+
'top-left': '',
113+
'top-right': '',
114+
bottom: '',
115+
'bottom-mid': '',
116+
'bottom-left': '',
117+
'bottom-right': '',
118+
left: '',
119+
'left-mid': '',
120+
mid: '',
121+
'mid-mid': '',
122+
right: '',
123+
'right-mid': '',
124+
middle: ' ',
125+
},
126+
style: {
127+
'padding-left': 0,
128+
'padding-right': 0,
129+
border: 0,
130+
},
131+
})
108132

109133
for (let i = 0; i < diff.children.length; ++i) {
110134
const child = diff.children[i]
111-
msg.push('\n', child.action.toLowerCase(), '\t')
135+
table[i] = [child.action.toLowerCase()]
112136

113137
switch (child.action) {
114138
case 'ADD':
115-
msg.push([child.ideal.name, child.ideal.package.version].join('\t'))
139+
table[i].push(child.ideal.name, child.ideal.package.version)
116140
break
117141
case 'REMOVE':
118-
msg.push([child.actual.name, child.actual.package.version].join('\t'))
142+
table[i].push(child.actual.name, child.actual.package.version)
119143
break
120144
case 'CHANGE':
121-
msg.push(
122-
[
123-
child.actual.name,
124-
child.actual.package.version + ' -> ' + child.ideal.package.version,
125-
].join('\t')
145+
table[i].push(
146+
child.actual.name,
147+
child.actual.package.version + ' -> ' + child.ideal.package.version
126148
)
127149
break
128150
}
129151
}
130152

131-
npm.output(msg.join(''))
153+
npm.output('\n' + table.toString())
132154
}
133155

134156
const getAuditReport = (npm, report) => {

tap-snapshots/test/lib/utils/reify-output.js.test.cjs

+9
Original file line numberDiff line numberDiff line change
@@ -1632,3 +1632,12 @@ exports[`test/lib/utils/reify-output.js TAP packages changed message > {"added"
16321632
}
16331633
}
16341634
`
1635+
1636+
exports[`test/lib/utils/reify-output.js TAP prints dedupe difference > diff table 1`] = `
1637+
1638+
add foo 1.0.0
1639+
remove bar 1.0.0
1640+
change bar 1.0.0 -> 2.1.0
1641+
1642+
removed 1 package, and changed 1 package in {TIME}
1643+
`

test/lib/utils/reify-output.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -406,21 +406,5 @@ t.test('prints dedupe difference', async t => {
406406
'dry-run': true,
407407
})
408408

409-
t.match(
410-
out,
411-
'add\tfoo\t1.0.0',
412-
'should print added package'
413-
)
414-
415-
t.match(
416-
out,
417-
'remove\tbar\t1.0.0',
418-
'should print removed package'
419-
)
420-
421-
t.match(
422-
out,
423-
'change\tbar\t1.0.0 -> 2.1.0',
424-
'should print changed package'
425-
)
409+
t.matchSnapshot(out, 'diff table')
426410
})

0 commit comments

Comments
 (0)