Skip to content

Commit 05d9bcf

Browse files
nlffritzy
authored andcommitted
fix(ls): display overridden nodes
1 parent e6d4304 commit 05d9bcf

File tree

3 files changed

+363
-12
lines changed

3 files changed

+363
-12
lines changed

lib/commands/ls.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ const getHumanOutputItem = (node, { args, color, global, long }) => {
329329
? ' ' + (color ? chalk.green.bgBlack('extraneous') : 'extraneous')
330330
: ''
331331
) +
332+
(
333+
node.overridden
334+
? ' ' + (color ? chalk.gray('overridden') : 'overridden')
335+
: ''
336+
) +
332337
(isGitNode(node) ? ` (${node.resolved})` : '') +
333338
(node.isLink ? ` -> ${relativePrefix}${targetLocation}` : '') +
334339
(long ? `${EOL}${node.package.description || ''}` : '')
@@ -347,6 +352,13 @@ const getJsonOutputItem = (node, { global, long }) => {
347352
item.resolved = node.resolved
348353
}
349354

355+
// if the node is the project root, do not add the overridden flag. the project root can't be
356+
// overridden anyway, and if we add the flag it causes undesirable behavior when `npm ls --json`
357+
// is ran in an empty directory since we end up printing an object with only an overridden prop
358+
if (!node.isProjectRoot) {
359+
item.overridden = node.overridden
360+
}
361+
350362
item[_name] = node.name
351363

352364
// special formatting for top-level package name
@@ -555,6 +567,7 @@ const parseableOutput = ({ global, long, seenNodes }) => {
555567
out += node.path !== node.realpath ? `:${node.realpath}` : ''
556568
out += isExtraneous(node, { global }) ? ':EXTRANEOUS' : ''
557569
out += node[_invalid] ? ':INVALID' : ''
570+
out += node.overridden ? ':OVERRIDDEN' : ''
558571
}
559572
out += EOL
560573
}

tap-snapshots/test/lib/commands/ls.js.test.cjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ exports[`test/lib/commands/ls.js TAP ls --parseable no args > should output pars
255255
{CWD}/tap-testdir-ls-ls---parseable-no-args/node_modules/dog
256256
`
257257

258+
exports[`test/lib/commands/ls.js TAP ls --parseable overridden dep > should contain overridden outout 1`] = `
259+
{CWD}/tap-testdir-ls-ls---parseable-overridden-dep:[email protected]
260+
{CWD}/tap-testdir-ls-ls---parseable-overridden-dep/node_modules/foo:[email protected]
261+
{CWD}/tap-testdir-ls-ls---parseable-overridden-dep/node_modules/bar:[email protected]:OVERRIDDEN
262+
`
263+
258264
exports[`test/lib/commands/ls.js TAP ls --parseable resolved points to git ref > should output tree containing git refs 1`] = `
259265
{CWD}/tap-testdir-ls-ls---parseable-resolved-points-to-git-ref
260266
{CWD}/tap-testdir-ls-ls---parseable-resolved-points-to-git-ref/node_modules/abbrev
@@ -567,6 +573,20 @@ [email protected] {CWD}/tap-testdir-ls-ls-no-args
567573
568574
`
569575

576+
exports[`test/lib/commands/ls.js TAP ls overridden dep > should contain overridden outout 1`] = `
577+
[email protected] {CWD}/tap-testdir-ls-ls-overridden-dep
578+
579+
\`-- [email protected] overridden
580+
581+
`
582+
583+
exports[`test/lib/commands/ls.js TAP ls overridden dep w/ color > should contain overridden outout 1`] = `
584+
[[email protected] {CWD}/tap-testdir-ls-ls-overridden-dep-w-color
585+
\`-- [email protected]
586+
 \`-- [email protected] overridden
587+

588+
`
589+
570590
exports[`test/lib/commands/ls.js TAP ls print deduped symlinks > should output tree containing linked deps 1`] = `
571591
[email protected] {CWD}/tap-testdir-ls-ls-print-deduped-symlinks
572592

0 commit comments

Comments
 (0)