-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tighten up the output of dep node explanations
Reduce visual noise, make the more important information more obvious.
- Loading branch information
Showing
5 changed files
with
1,227 additions
and
878 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const chalk = require('chalk') | ||
const nocolor = { | ||
bold: s => s, | ||
dim: s => s, | ||
red: s => s, | ||
yellow: s => s, | ||
cyan: s => s, | ||
magenta: 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 style = type === 'extraneous' ? red | ||
: type === 'dev' ? yellow | ||
: type === 'optional' ? cyan | ||
: type === 'peer' ? magenta | ||
: /* istanbul ignore next */ s => s | ||
return style(type) | ||
} | ||
|
||
const printNode = (node, color) => { | ||
const { | ||
name, | ||
version, | ||
location, | ||
extraneous, | ||
dev, | ||
optional, | ||
peer | ||
} = node | ||
const { bold, dim } = color ? chalk : nocolor | ||
const extra = [] | ||
if (extraneous) { | ||
extra.push(' ' + bold(colorType('extraneous', color))) | ||
} | ||
if (dev) { | ||
extra.push(' ' + bold(colorType('dev', color))) | ||
} | ||
if (optional) { | ||
extra.push(' ' + bold(colorType('optional', color))) | ||
} | ||
if (peer) { | ||
extra.push(' ' + bold(colorType('peer', color))) | ||
} | ||
|
||
return `${bold(name)}@${bold(version)}${extra.join('')}` + | ||
(location ? dim(`\n${location}`) : '') | ||
} | ||
|
||
const explainDependents = ({ name, dependents }, depth, color) => { | ||
if (!dependents || !dependents.length || depth <= 0) { | ||
return '' | ||
} | ||
|
||
const max = Math.ceil(depth / 2) | ||
const messages = dependents.slice(0, max) | ||
.map(dep => explainDependency(name, dep, depth, color)) | ||
|
||
// show just the names of the first 5 deps that overflowed the list | ||
if (dependents.length > max) { | ||
let len = 0 | ||
const maxLen = 30 | ||
const showNames = [] | ||
for (let i = max; i < dependents.length; i++) { | ||
const { from: { name } } = dependents[i] | ||
len += name.length | ||
if (len >= maxLen && i < dependents.length - 1) { | ||
showNames.push('...') | ||
break | ||
} | ||
showNames.push(name) | ||
} | ||
const show = `(${showNames.join(', ')})` | ||
messages.push(`${dependents.length - max} more ${show}`) | ||
} | ||
|
||
const str = '\n' + messages.join('\n') | ||
return str.split('\n').join('\n ') | ||
} | ||
|
||
const explainDependency = (name, { type, from, spec }, depth, color) => { | ||
const { bold } = color ? chalk : nocolor | ||
return (type === 'prod' ? '' : `${colorType(type, color)} `) + | ||
`${bold(name)}@"${bold(spec)}" from ` + | ||
explainFrom(from, depth, color) | ||
} | ||
|
||
const explainFrom = (from, depth, color) => { | ||
if (!from.name && !from.version) { | ||
return 'the root project' | ||
} | ||
|
||
return printNode(from, color) + | ||
explainDependents(from, depth - 1, color) | ||
} | ||
|
||
module.exports = { explainNode, printNode } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* IMPORTANT | ||
* This snapshot file is auto-generated, but designed for humans. | ||
* It should be checked into source control and tracked carefully. | ||
* Re-generate by setting TAP_SNAPSHOT=1 and running tests. | ||
* Make sure to inspect the output below. Do not ignore changes! | ||
*/ | ||
'use strict' | ||
exports[`test/lib/utils/explain-dep.js TAP > ellipses test one 1`] = ` | ||
[email protected] | ||
manydep@"1.0.0" from [email protected] | ||
node_modules/prod-dep | ||
prod-dep@"1.x" from the root project | ||
6 more (optdep, extra-neos, deep-dev, peer, ...) | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP > ellipses test two 1`] = ` | ||
[email protected] | ||
manydep@"1.0.0" from [email protected] | ||
node_modules/prod-dep | ||
prod-dep@"1.x" from the root project | ||
5 more (optdep, extra-neos, deep-dev, peer, a package with a pretty long name) | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP deepDev > explain color deep 1`] = ` | ||
[1mdeep-dev[22m@[1m2.3.4[22m [1m[33mdev[39m[22m[2m[22m | ||
[2mnode_modules/deep-dev[22m | ||
[1mdeep-dev[22m@"[1m2.x[22m" from [1mmetadev[22m@[1m3.4.5[22m[2m[22m | ||
[2mnode_modules/dev/node_modules/metadev[22m | ||
[1mmetadev[22m@"[1m3.x[22m" from [1mtopdev[22m@[1m4.5.6[22m[2m[22m | ||
[2mnode_modules/topdev[22m | ||
[33mdev[39m [1mtopdev[22m@"[1m4.x[22m" from the root project | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP deepDev > explain nocolor shallow 1`] = ` | ||
[email protected] dev | ||
node_modules/deep-dev | ||
deep-dev@"2.x" from [email protected] | ||
node_modules/dev/node_modules/metadev | ||
metadev@"3.x" from [email protected] | ||
node_modules/topdev | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP deepDev > print color 1`] = ` | ||
[1mdeep-dev[22m@[1m2.3.4[22m [1m[33mdev[39m[22m[2m[22m | ||
[2mnode_modules/deep-dev[22m | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP deepDev > print nocolor 1`] = ` | ||
[email protected] dev | ||
node_modules/deep-dev | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP extraneous > explain color deep 1`] = ` | ||
[1mextra-neos[22m@[1m1337.420.69-lol[22m [1m[31mextraneous[39m[22m[2m[22m | ||
[2mnode_modules/extra-neos[22m | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP extraneous > explain nocolor shallow 1`] = ` | ||
[email protected] extraneous | ||
node_modules/extra-neos | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP extraneous > print color 1`] = ` | ||
[1mextra-neos[22m@[1m1337.420.69-lol[22m [1m[31mextraneous[39m[22m[2m[22m | ||
[2mnode_modules/extra-neos[22m | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP extraneous > print nocolor 1`] = ` | ||
[email protected] extraneous | ||
node_modules/extra-neos | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP manyDeps > explain color deep 1`] = ` | ||
[1mmanydep[22m@[1m1.0.0[22m | ||
[1mmanydep[22m@"[1m1.0.0[22m" from [1mprod-dep[22m@[1m1.2.3[22m[2m[22m | ||
[2mnode_modules/prod-dep[22m | ||
[1mprod-dep[22m@"[1m1.x[22m" from the root project | ||
[36moptional[39m [1mmanydep[22m@"[1m1.x[22m" from [1moptdep[22m@[1m1.0.0[22m [1m[36moptional[39m[22m[2m[22m | ||
[2mnode_modules/optdep[22m | ||
optdep [1moptdep[22m@"[1m1.0.0[22m" from the root project | ||
[1mmanydep[22m@"[1m1.0.x[22m" from [1mextra-neos[22m@[1m1337.420.69-lol[22m [1m[31mextraneous[39m[22m[2m[22m | ||
[2mnode_modules/extra-neos[22m | ||
[33mdev[39m [1mmanydep[22m@"[1m*[22m" from [1mdeep-dev[22m@[1m2.3.4[22m [1m[33mdev[39m[22m[2m[22m | ||
[2mnode_modules/deep-dev[22m | ||
[1mdeep-dev[22m@"[1m2.x[22m" from [1mmetadev[22m@[1m3.4.5[22m[2m[22m | ||
[2mnode_modules/dev/node_modules/metadev[22m | ||
[1mmetadev[22m@"[1m3.x[22m" from [1mtopdev[22m@[1m4.5.6[22m[2m[22m | ||
[2mnode_modules/topdev[22m | ||
[33mdev[39m [1mtopdev[22m@"[1m4.x[22m" from the root project | ||
[35mpeer[39m [1mmanydep[22m@"[1m>1.0.0-beta <1.0.1[22m" from [1mpeer[22m@[1m1.0.0[22m [1m[35mpeer[39m[22m[2m[22m | ||
[2mnode_modules/peer[22m | ||
[35mpeer[39m [1mpeer[22m@"[1m1.0.0[22m" from the root project | ||
[1mmanydep[22m@"[1m1[22m" from [1ma package with a pretty long name[22m@[1m1.2.3[22m | ||
[1mmanydep[22m@"[1m1[22m" from [1manother package with a pretty long name[22m@[1m1.2.3[22m | ||
[1mmanydep[22m@"[1m1[22m" from [1myet another a package with a pretty long name[22m@[1m1.2.3[22m | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP manyDeps > explain nocolor shallow 1`] = ` | ||
[email protected] | ||
manydep@"1.0.0" from [email protected] | ||
node_modules/prod-dep | ||
prod-dep@"1.x" from the root project | ||
7 more (optdep, extra-neos, deep-dev, peer, ...) | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP manyDeps > print color 1`] = ` | ||
[1mmanydep[22m@[1m1.0.0[22m | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP manyDeps > print nocolor 1`] = ` | ||
[email protected] | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP optional > explain color deep 1`] = ` | ||
[1moptdep[22m@[1m1.0.0[22m [1m[36moptional[39m[22m[2m[22m | ||
[2mnode_modules/optdep[22m | ||
optdep [1moptdep[22m@"[1m1.0.0[22m" from the root project | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP optional > explain nocolor shallow 1`] = ` | ||
[email protected] optional | ||
node_modules/optdep | ||
optdep optdep@"1.0.0" from the root project | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP optional > print color 1`] = ` | ||
[1moptdep[22m@[1m1.0.0[22m [1m[36moptional[39m[22m[2m[22m | ||
[2mnode_modules/optdep[22m | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP optional > print nocolor 1`] = ` | ||
[email protected] optional | ||
node_modules/optdep | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP peer > explain color deep 1`] = ` | ||
[1mpeer[22m@[1m1.0.0[22m [1m[35mpeer[39m[22m[2m[22m | ||
[2mnode_modules/peer[22m | ||
[35mpeer[39m [1mpeer[22m@"[1m1.0.0[22m" from the root project | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP peer > explain nocolor shallow 1`] = ` | ||
[email protected] peer | ||
node_modules/peer | ||
peer peer@"1.0.0" from the root project | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP peer > print color 1`] = ` | ||
[1mpeer[22m@[1m1.0.0[22m [1m[35mpeer[39m[22m[2m[22m | ||
[2mnode_modules/peer[22m | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP peer > print nocolor 1`] = ` | ||
[email protected] peer | ||
node_modules/peer | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP prodDep > explain color deep 1`] = ` | ||
[1mprod-dep[22m@[1m1.2.3[22m[2m[22m | ||
[2mnode_modules/prod-dep[22m | ||
[1mprod-dep[22m@"[1m1.x[22m" from the root project | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP prodDep > explain nocolor shallow 1`] = ` | ||
[email protected] | ||
node_modules/prod-dep | ||
prod-dep@"1.x" from the root project | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP prodDep > print color 1`] = ` | ||
[1mprod-dep[22m@[1m1.2.3[22m[2m[22m | ||
[2mnode_modules/prod-dep[22m | ||
` | ||
|
||
exports[`test/lib/utils/explain-dep.js TAP prodDep > print nocolor 1`] = ` | ||
[email protected] | ||
node_modules/prod-dep | ||
` |
Oops, something went wrong.