Skip to content

Commit 6d5f9ac

Browse files
authored
feat: dedupe - display difference when --dry-run is enabled (#7133)
* Added pretty print for dedupe command with --dry-run flag * pretty print test coverage
1 parent a50b03b commit 6d5f9ac

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/utils/reify-output.js

+33
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ const reifyOutput = (npm, arb) => {
4141
}
4242

4343
if (diff) {
44+
if (npm.config.get('dry-run')) {
45+
printDiff(npm, diff)
46+
}
47+
4448
depth({
4549
tree: diff,
4650
visit: d => {
@@ -98,6 +102,35 @@ const printAuditReport = (npm, report) => {
98102
npm.output(`\n${res.report}`)
99103
}
100104

105+
// print the diff tree of actions that would be taken
106+
const printDiff = (npm, diff) => {
107+
const msg = []
108+
109+
for (let i = 0; i < diff.children.length; ++i) {
110+
const child = diff.children[i]
111+
msg.push('\n', child.action.toLowerCase(), '\t')
112+
113+
switch (child.action) {
114+
case 'ADD':
115+
msg.push([child.ideal.name, child.ideal.package.version].join('\t'))
116+
break
117+
case 'REMOVE':
118+
msg.push([child.actual.name, child.actual.package.version].join('\t'))
119+
break
120+
case 'CHANGE':
121+
msg.push(
122+
[
123+
child.actual.name,
124+
child.actual.package.version + ' -> ' + child.ideal.package.version,
125+
].join('\t')
126+
)
127+
break
128+
}
129+
}
130+
131+
npm.output(msg.join(''))
132+
}
133+
101134
const getAuditReport = (npm, report) => {
102135
if (!report) {
103136
return

test/lib/utils/reify-output.js

+44
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,47 @@ t.test('added packages should be looked up within returned tree', async t => {
380380
t.matchSnapshot(out)
381381
})
382382
})
383+
384+
t.test('prints dedupe difference', async t => {
385+
const mock = {
386+
actualTree: {
387+
name: 'foo',
388+
inventory: {
389+
has: () => false,
390+
},
391+
},
392+
diff: {
393+
children: [
394+
{ action: 'ADD', ideal: { name: 'foo', package: { version: '1.0.0' } } },
395+
{ action: 'REMOVE', actual: { name: 'bar', package: { version: '1.0.0' } } },
396+
{
397+
action: 'CHANGE',
398+
actual: { name: 'bar', package: { version: '1.0.0' } },
399+
ideal: { package: { version: '2.1.0' } },
400+
},
401+
],
402+
},
403+
}
404+
405+
const out = await mockReify(t, mock, {
406+
'dry-run': true,
407+
})
408+
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+
)
426+
})

0 commit comments

Comments
 (0)