Skip to content

Commit

Permalink
fix(explain): display override information
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf authored and fritzy committed Aug 17, 2022
1 parent 05d9bcf commit cd2b1e6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/commands/explain.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Explain extends ArboristWorkspaceCmd {

const expls = []
for (const node of nodes) {
const { extraneous, dev, optional, devOptional, peer, inBundle } = node
const { extraneous, dev, optional, devOptional, peer, inBundle, overridden } = node
const expl = node.explain()
if (extraneous) {
expl.extraneous = true
Expand All @@ -69,6 +69,7 @@ class Explain extends ArboristWorkspaceCmd {
expl.devOptional = devOptional
expl.peer = peer
expl.bundled = inBundle
expl.overridden = overridden
}
expls.push(expl)
}
Expand Down
17 changes: 14 additions & 3 deletions lib/utils/explain-dep.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const nocolor = {
magenta: s => s,
blue: s => s,
green: s => s,
gray: s => s,
}

const { relative } = require('path')
Expand All @@ -18,13 +19,14 @@ const explainNode = (node, depth, color) =>
explainLinksIn(node, depth, color)

const colorType = (type, color) => {
const { red, yellow, cyan, magenta, blue, green } = color ? chalk : nocolor
const { red, yellow, cyan, magenta, blue, green, gray } = color ? chalk : nocolor
const style = type === 'extraneous' ? red
: type === 'dev' ? yellow
: type === 'optional' ? cyan
: type === 'peer' ? magenta
: type === 'bundled' ? blue
: type === 'workspace' ? green
: type === 'overridden' ? gray
: /* istanbul ignore next */ s => s
return style(type)
}
Expand All @@ -40,6 +42,7 @@ const printNode = (node, color) => {
peer,
bundled,
isWorkspace,
overridden,
} = node
const { bold, dim, green } = color ? chalk : nocolor
const extra = []
Expand All @@ -63,6 +66,10 @@ const printNode = (node, color) => {
extra.push(' ' + bold(colorType('bundled', color)))
}

if (overridden) {
extra.push(' ' + bold(colorType('overridden', color)))
}

const pkgid = isWorkspace
? green(`${name}@${version}`)
: `${bold(name)}@${bold(version)}`
Expand Down Expand Up @@ -112,11 +119,15 @@ const explainDependents = ({ name, dependents }, depth, color) => {
return str.split('\n').join('\n ')
}

const explainEdge = ({ name, type, bundled, from, spec }, depth, color) => {
const explainEdge = ({ name, type, bundled, from, spec, rawSpec, overridden }, depth, color) => {
const { bold } = color ? chalk : nocolor
const dep = type === 'workspace'
let dep = type === 'workspace'
? bold(relative(from.location, spec.slice('file:'.length)))
: `${bold(name)}@"${bold(spec)}"`
if (overridden) {
dep = `${colorType('overridden', color)} ${dep} (was "${rawSpec}")`
}

const fromMsg = ` from ${explainFrom(from, depth, color)}`

return (type === 'prod' ? '' : `${colorType(type, color)} `) +
Expand Down
22 changes: 22 additions & 0 deletions tap-snapshots/test/lib/utils/explain-dep.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ optdep@1.0.0 optional
node_modules/optdep
`

exports[`test/lib/utils/explain-dep.js TAP overridden > explain color deep 1`] = `
overridden-root@1.0.0 overridden
node_modules/overridden-root
overridden overridden-dep@"1.0.0" (was "^2.0.0") from the root project
`

exports[`test/lib/utils/explain-dep.js TAP overridden > explain nocolor shallow 1`] = `
overridden-root@1.0.0 overridden
node_modules/overridden-root
overridden overridden-dep@"1.0.0" (was "^2.0.0") from the root project
`

exports[`test/lib/utils/explain-dep.js TAP overridden > print color 1`] = `
overridden-root@1.0.0 overridden
node_modules/overridden-root
`

exports[`test/lib/utils/explain-dep.js TAP overridden > print nocolor 1`] = `
overridden-root@1.0.0 overridden
node_modules/overridden-root
`

exports[`test/lib/utils/explain-dep.js TAP peer > explain color deep 1`] = `
peer@1.0.0 peer
node_modules/peer
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 @@ -129,6 +129,23 @@ const cases = {
dependents: [],
extraneous: true,
},

overridden: {
name: 'overridden-root',
version: '1.0.0',
location: 'node_modules/overridden-root',
overridden: true,
dependents: [{
type: 'prod',
name: 'overridden-dep',
spec: '1.0.0',
rawSpec: '^2.0.0',
overridden: true,
from: {
location: '/path/to/project',
},
}],
},
}

cases.manyDeps = {
Expand Down

0 comments on commit cd2b1e6

Please sign in to comment.