Skip to content

Commit

Permalink
fix(ls): display overridden nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf authored and fritzy committed Aug 17, 2022
1 parent e6d4304 commit 05d9bcf
Show file tree
Hide file tree
Showing 3 changed files with 363 additions and 12 deletions.
13 changes: 13 additions & 0 deletions lib/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ const getHumanOutputItem = (node, { args, color, global, long }) => {
? ' ' + (color ? chalk.green.bgBlack('extraneous') : 'extraneous')
: ''
) +
(
node.overridden
? ' ' + (color ? chalk.gray('overridden') : 'overridden')
: ''
) +
(isGitNode(node) ? ` (${node.resolved})` : '') +
(node.isLink ? ` -> ${relativePrefix}${targetLocation}` : '') +
(long ? `${EOL}${node.package.description || ''}` : '')
Expand All @@ -347,6 +352,13 @@ const getJsonOutputItem = (node, { global, long }) => {
item.resolved = node.resolved
}

// if the node is the project root, do not add the overridden flag. the project root can't be
// overridden anyway, and if we add the flag it causes undesirable behavior when `npm ls --json`
// is ran in an empty directory since we end up printing an object with only an overridden prop
if (!node.isProjectRoot) {
item.overridden = node.overridden
}

item[_name] = node.name

// special formatting for top-level package name
Expand Down Expand Up @@ -555,6 +567,7 @@ const parseableOutput = ({ global, long, seenNodes }) => {
out += node.path !== node.realpath ? `:${node.realpath}` : ''
out += isExtraneous(node, { global }) ? ':EXTRANEOUS' : ''
out += node[_invalid] ? ':INVALID' : ''
out += node.overridden ? ':OVERRIDDEN' : ''
}
out += EOL
}
Expand Down
20 changes: 20 additions & 0 deletions tap-snapshots/test/lib/commands/ls.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ exports[`test/lib/commands/ls.js TAP ls --parseable no args > should output pars
{CWD}/tap-testdir-ls-ls---parseable-no-args/node_modules/dog
`

exports[`test/lib/commands/ls.js TAP ls --parseable overridden dep > should contain overridden outout 1`] = `
{CWD}/tap-testdir-ls-ls---parseable-overridden-dep:test-overridden@1.0.0
{CWD}/tap-testdir-ls-ls---parseable-overridden-dep/node_modules/foo:foo@1.0.0
{CWD}/tap-testdir-ls-ls---parseable-overridden-dep/node_modules/bar:bar@1.0.0:OVERRIDDEN
`

exports[`test/lib/commands/ls.js TAP ls --parseable resolved points to git ref > should output tree containing git refs 1`] = `
{CWD}/tap-testdir-ls-ls---parseable-resolved-points-to-git-ref
{CWD}/tap-testdir-ls-ls---parseable-resolved-points-to-git-ref/node_modules/abbrev
Expand Down Expand Up @@ -567,6 +573,20 @@ test-npm-ls@1.0.0 {CWD}/tap-testdir-ls-ls-no-args
`

exports[`test/lib/commands/ls.js TAP ls overridden dep > should contain overridden outout 1`] = `
test-overridden@1.0.0 {CWD}/tap-testdir-ls-ls-overridden-dep
\`-- foo@1.0.0
\`-- bar@1.0.0 overridden
`

exports[`test/lib/commands/ls.js TAP ls overridden dep w/ color > should contain overridden outout 1`] = `
test-overridden@1.0.0 {CWD}/tap-testdir-ls-ls-overridden-dep-w-color
\`-- foo@1.0.0
 \`-- bar@1.0.0 overridden

`

exports[`test/lib/commands/ls.js TAP ls print deduped symlinks > should output tree containing linked deps 1`] = `
print-deduped-symlinks@1.0.0 {CWD}/tap-testdir-ls-ls-print-deduped-symlinks
+-- a@1.0.0
Expand Down
Loading

0 comments on commit 05d9bcf

Please sign in to comment.