Skip to content
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
33 changes: 33 additions & 0 deletions lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const reifyOutput = (npm, arb) => {
}

if (diff) {
if (npm.config.get('dry-run')) {
printDiff(npm, diff)
}

depth({
tree: diff,
visit: d => {
Expand Down Expand Up @@ -98,6 +102,35 @@ const printAuditReport = (npm, report) => {
npm.output(`\n${res.report}`)
}

// print the diff tree of actions that would be taken
const printDiff = (npm, diff) => {
const msg = []

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

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

npm.output(msg.join(''))
}

const getAuditReport = (npm, report) => {
if (!report) {
return
Expand Down
44 changes: 44 additions & 0 deletions test/lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,47 @@ t.test('added packages should be looked up within returned tree', async t => {
t.matchSnapshot(out)
})
})

t.test('prints dedupe difference', async t => {
const mock = {
actualTree: {
name: 'foo',
inventory: {
has: () => false,
},
},
diff: {
children: [
{ action: 'ADD', ideal: { name: 'foo', package: { version: '1.0.0' } } },
{ action: 'REMOVE', actual: { name: 'bar', package: { version: '1.0.0' } } },
{
action: 'CHANGE',
actual: { name: 'bar', package: { version: '1.0.0' } },
ideal: { package: { version: '2.1.0' } },
},
],
},
}

const out = await mockReify(t, mock, {
'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'
)
})