Skip to content

Commit

Permalink
fix: npm ls <pkg> with depth cli config
Browse files Browse the repository at this point in the history
Using the cli option --depth is currently not resulting in the expected
behavior of filtering depth level when running npm ls <pkg> - that's due
the special behavior of printing all results when using a filter arg.

This commit fixes it by adding support to limiting depth if a config
value is set by the user.

Fixes #1861
  • Loading branch information
ruyadorno committed Sep 25, 2020
1 parent 9bc6966 commit 8cdeab8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ const ls = async (args) => {
const only = npm.config.get('only')
const prod = npm.config.get('prod')
const production = npm.config.get('production')
const depthCli = // retrieves only user-defined values
npm.config.get('depth', 'cli') || npm.config.get('depth', 'user')

const arb = new Arborist({
global,
Expand All @@ -406,7 +408,12 @@ const ls = async (args) => {
const seenItems = new Set()
const seenNodes = new Map()
const problems = new Set()
const depthToPrint = (all || args.length) ? Infinity : (depth || 0)

// defines special handling of printed depth when filtering with args
const filterDefaultDepth = depthCli === undefined ? Infinity : depthCli
const depthToPrint = (all || args.length)
? filterDefaultDepth
: (depth || 0)

// add root node of tree to list of seenNodes
seenNodes.set(tree.path, tree)
Expand Down
12 changes: 12 additions & 0 deletions tap-snapshots/test-lib-ls.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,18 @@ [email protected] {CWD}/ls-ls-extraneous-deps
`

exports[`test/lib/ls.js TAP ls filter pkg arg using depth option > should list a in top-level only 1`] = `
[email protected] {CWD}/ls-ls-filter-pkg-arg-using-depth-option
\`-- [email protected]
`

exports[`test/lib/ls.js TAP ls filter pkg arg using depth option > should print empty results msg 1`] = `
[email protected] {CWD}/ls-ls-filter-pkg-arg-using-depth-option
\`-- (empty)
`

exports[`test/lib/ls.js TAP ls filtering by child of missing dep > should print tree and not duplicate child of missing items 1`] = `
[email protected] {CWD}/ls-ls-filtering-by-child-of-missing-dep
+-- [email protected] extraneous
Expand Down
64 changes: 64 additions & 0 deletions test/lib/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,70 @@ t.test('ls', (t) => {
})
})

t.test('filter pkg arg using depth option', (t) => {
_flatOptions.depth = 0
prefix = t.testdir({
'package.json': JSON.stringify({
name: 'test-pkg-arg-filter-with-depth-opt',
version: '1.0.0',
dependencies: {
a: '^1.0.0',
b: '^1.0.0'
}
}),
node_modules: {
a: {
'package.json': JSON.stringify({
name: 'a',
version: '1.0.0'
})
},
b: {
'package.json': JSON.stringify({
name: 'b',
version: '1.0.0',
dependencies: {
c: '^1.0.0'
}
})
},
c: {
'package.json': JSON.stringify({
name: 'c',
version: '1.0.0',
dependencies: {
d: '^1.0.0'
}
})
},
d: {
'package.json': JSON.stringify({
name: 'd',
version: '1.0.0',
dependencies: {
a: '^1.0.0'
}
})
}
}
})

t.plan(4)
ls(['a'], (err) => {
t.ifError(err, 'should NOT have ELSPROBLEMS error code')
t.matchSnapshot(redactCwd(result), 'should list a in top-level only')

ls(['d'], (err) => {
t.ifError(err, 'should NOT have ELSPROBLEMS error code when filter')
t.matchSnapshot(redactCwd(result), 'should print empty results msg')
})
})
})

t.teardown(() => {
_flatOptions.depth = Infinity
})

t.end()
})

Expand Down

0 comments on commit 8cdeab8

Please sign in to comment.