From 983d218f7e68e3c7866f2efa23ea2aff7ff3881e Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 22 Feb 2021 14:16:02 +0800 Subject: [PATCH] fix(explain): mark when dependency is bundled When using `npm explain ` 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: https://github.com/npm/cli/pull/2750 Credit: @kumavis Close: #2750 Reviewed-by: @nlf --- lib/explain.js | 3 ++- lib/utils/explain-dep.js | 8 ++++++- .../test-lib-utils-explain-dep.js-TAP.test.js | 22 +++++++++++++++++++ test/lib/utils/explain-dep.js | 17 ++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/explain.js b/lib/explain.js index 9176f2e6c247..a0a4427bccf2 100644 --- a/lib/explain.js +++ b/lib/explain.js @@ -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 @@ -39,6 +39,7 @@ const explain = async (args) => { expl.optional = optional expl.devOptional = devOptional expl.peer = peer + expl.bundled = inBundle } expls.push(expl) } diff --git a/lib/utils/explain-dep.js b/lib/utils/explain-dep.js index ed69a02c143c..213493c654b0 100644 --- a/lib/utils/explain-dep.js +++ b/lib/utils/explain-dep.js @@ -6,6 +6,7 @@ const nocolor = { yellow: s => s, cyan: s => s, magenta: s => s, + blue: s => s, } const explainNode = (node, depth, color) => @@ -13,11 +14,12 @@ const explainNode = (node, depth, 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) } @@ -31,6 +33,7 @@ const printNode = (node, color) => { dev, optional, peer, + bundled, } = node const { bold, dim } = color ? chalk : nocolor const extra = [] @@ -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}`) : '') } diff --git a/tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js b/tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js index 54a77bc122b4..6d169bca6980 100644 --- a/tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js +++ b/tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js @@ -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 diff --git a/test/lib/utils/explain-dep.js b/test/lib/utils/explain-dep.js index 28f14477ab70..1b8c4ae3805c 100644 --- a/test/lib/utils/explain-dep.js +++ b/test/lib/utils/explain-dep.js @@ -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',