Skip to content

Commit

Permalink
feat(explain): mark when dependency is bundled
Browse files Browse the repository at this point in the history
When using `npm explain <package>` it's useful to see if the package has
been bundled. This is especially useful when trying to understand the
provenance of a package's content

PR-URL: #2750
Credit: @kumavis
Close: #2750
Reviewed-by: @nlf
  • Loading branch information
kumavis authored and wraithgar committed Feb 25, 2021
1 parent 28d036a commit 881a855
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/explain.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const explain = async (args) => {

const expls = []
for (const node of nodes) {
const { extraneous, dev, optional, devOptional, peer } = node
const { extraneous, dev, optional, devOptional, peer, inBundle } = node
const expl = node.explain()
if (extraneous)
expl.extraneous = true
Expand All @@ -39,6 +39,7 @@ const explain = async (args) => {
expl.optional = optional
expl.devOptional = devOptional
expl.peer = peer
expl.bundled = inBundle
}
expls.push(expl)
}
Expand Down
8 changes: 7 additions & 1 deletion lib/utils/explain-dep.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ const nocolor = {
yellow: s => s,
cyan: s => s,
magenta: s => s,
blue: s => s,
}

const explainNode = (node, depth, color) =>
printNode(node, color) +
explainDependents(node, depth, color)

const colorType = (type, color) => {
const { red, yellow, cyan, magenta } = color ? chalk : nocolor
const { red, yellow, cyan, magenta, blue } = color ? chalk : nocolor
const style = type === 'extraneous' ? red
: type === 'dev' ? yellow
: type === 'optional' ? cyan
: type === 'peer' ? magenta
: type === 'bundled' ? blue
: /* istanbul ignore next */ s => s
return style(type)
}
Expand All @@ -31,6 +33,7 @@ const printNode = (node, color) => {
dev,
optional,
peer,
bundled,
} = node
const { bold, dim } = color ? chalk : nocolor
const extra = []
Expand All @@ -46,6 +49,9 @@ const printNode = (node, color) => {
if (peer)
extra.push(' ' + bold(colorType('peer', color)))

if (bundled)
extra.push(' ' + bold(colorType('bundled', color)))

return `${bold(name)}@${bold(version)}${extra.join('')}` +
(location ? dim(`\n${location}`) : '')
}
Expand Down
22 changes: 22 additions & 0 deletions tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ manydep@1.0.0
6 more (optdep, extra-neos, deep-dev, peer, the root project, a package with a pretty long name)
`

exports[`test/lib/utils/explain-dep.js TAP bundled > explain color deep 1`] = `
bundle-of-joy@1.0.0 bundled
node_modules/bundle-of-joy
prod-dep@"1.x" from the root project
`

exports[`test/lib/utils/explain-dep.js TAP bundled > explain nocolor shallow 1`] = `
bundle-of-joy@1.0.0 bundled
node_modules/bundle-of-joy
prod-dep@"1.x" from the root project
`

exports[`test/lib/utils/explain-dep.js TAP bundled > print color 1`] = `
bundle-of-joy@1.0.0 bundled
node_modules/bundle-of-joy
`

exports[`test/lib/utils/explain-dep.js TAP bundled > print nocolor 1`] = `
bundle-of-joy@1.0.0 bundled
node_modules/bundle-of-joy
`

exports[`test/lib/utils/explain-dep.js TAP deepDev > explain color deep 1`] = `
deep-dev@2.3.4 dev
node_modules/deep-dev
Expand Down
17 changes: 17 additions & 0 deletions test/lib/utils/explain-dep.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ const cases = {
],
},

bundled: {
name: 'bundle-of-joy',
version: '1.0.0',
location: 'node_modules/bundle-of-joy',
bundled: true,
dependents: [
{
type: 'prod',
name: 'prod-dep',
spec: '1.x',
from: {
location: '/path/to/project',
},
},
],
},

extraneous: {
name: 'extra-neos',
version: '1337.420.69-lol',
Expand Down

0 comments on commit 881a855

Please sign in to comment.