Skip to content

Commit e225ddc

Browse files
ruyadornonlf
authored andcommitted
fix: npm ls <pkg> with depth cli config
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 PR-URL: #1862 Credit: @ruyadorno Close: #1862 Reviewed-by: @nlf
1 parent 2715220 commit e225ddc

File tree

5 files changed

+114
-9
lines changed

5 files changed

+114
-9
lines changed

lib/ls.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,12 @@ const ls = async (args) => {
406406
const seenItems = new Set()
407407
const seenNodes = new Map()
408408
const problems = new Set()
409-
const depthToPrint = (all || args.length) ? Infinity : (depth || 0)
409+
410+
// defines special handling of printed depth when filtering with args
411+
const filterDefaultDepth = depth === null ? Infinity : depth
412+
const depthToPrint = (all || args.length)
413+
? filterDefaultDepth
414+
: (depth || 0)
410415

411416
// add root node of tree to list of seenNodes
412417
seenNodes.set(tree.path, tree)

lib/utils/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const defaults = {
7373

7474
color: process.env.NO_COLOR == null,
7575
call: '',
76-
depth: 0,
76+
depth: null,
7777
description: true,
7878
dev: false,
7979
'dry-run': false,
@@ -208,7 +208,7 @@ const types = {
208208
cidr: [null, String, Array],
209209
color: ['always', Boolean],
210210
call: String,
211-
depth: Number,
211+
depth: [null, Number],
212212
description: Boolean,
213213
dev: Boolean,
214214
'dry-run': Boolean,

tap-snapshots/test-lib-ls.js-TAP.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,26 @@ [email protected] {CWD}/ls-ls-extraneous-deps
373373
374374
`
375375

376+
exports[`test/lib/ls.js TAP ls filter pkg arg using depth option > should list a in top-level only 1`] = `
377+
[email protected] {CWD}/ls-ls-filter-pkg-arg-using-depth-option
378+
379+
380+
`
381+
382+
exports[`test/lib/ls.js TAP ls filter pkg arg using depth option > should print empty results msg 1`] = `
383+
[email protected] {CWD}/ls-ls-filter-pkg-arg-using-depth-option
384+
\`-- (empty)
385+
386+
`
387+
388+
exports[`test/lib/ls.js TAP ls filter pkg arg using depth option > should print expected result 1`] = `
389+
[email protected] {CWD}/ls-ls-filter-pkg-arg-using-depth-option
390+
391+
392+
393+
394+
`
395+
376396
exports[`test/lib/ls.js TAP ls filtering by child of missing dep > should print tree and not duplicate child of missing items 1`] = `
377397
[email protected] {CWD}/ls-ls-filtering-by-child-of-missing-dep
378398
+-- [email protected] extraneous

tap-snapshots/test-lib-utils-config.js-TAP.test.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Object {
3232
"cidr": null,
3333
"color": true,
3434
"commit-hooks": true,
35-
"depth": 0,
35+
"depth": null,
3636
"description": true,
3737
"dev": false,
3838
"dry-run": false,
@@ -326,7 +326,10 @@ Object {
326326
"{Boolean TYPE}",
327327
],
328328
"commit-hooks": "{Boolean TYPE}",
329-
"depth": "{Number TYPE}",
329+
"depth": Array [
330+
null,
331+
"{Number TYPE}",
332+
],
330333
"description": "{Boolean TYPE}",
331334
"dev": "{Boolean TYPE}",
332335
"dry-run": "{Boolean TYPE}",
@@ -541,7 +544,7 @@ Object {
541544
"cidr": null,
542545
"color": true,
543546
"commit-hooks": true,
544-
"depth": 0,
547+
"depth": null,
545548
"description": true,
546549
"dev": false,
547550
"dry-run": false,
@@ -835,7 +838,10 @@ Object {
835838
"{Boolean TYPE}",
836839
],
837840
"commit-hooks": "{Boolean TYPE}",
838-
"depth": "{Number TYPE}",
841+
"depth": Array [
842+
null,
843+
"{Number TYPE}",
844+
],
839845
"description": "{Boolean TYPE}",
840846
"dev": "{Boolean TYPE}",
841847
"dry-run": "{Boolean TYPE}",
@@ -1050,7 +1056,7 @@ Object {
10501056
"cidr": null,
10511057
"color": true,
10521058
"commit-hooks": true,
1053-
"depth": 0,
1059+
"depth": null,
10541060
"description": true,
10551061
"dev": false,
10561062
"dry-run": false,
@@ -1344,7 +1350,10 @@ Object {
13441350
"{Boolean TYPE}",
13451351
],
13461352
"commit-hooks": "{Boolean TYPE}",
1347-
"depth": "{Number TYPE}",
1353+
"depth": Array [
1354+
null,
1355+
"{Number TYPE}",
1356+
],
13481357
"description": "{Boolean TYPE}",
13491358
"dev": "{Boolean TYPE}",
13501359
"dry-run": "{Boolean TYPE}",

test/lib/ls.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,77 @@ t.test('ls', (t) => {
14261426
})
14271427
})
14281428

1429+
t.test('filter pkg arg using depth option', (t) => {
1430+
_flatOptions.depth = 0
1431+
prefix = t.testdir({
1432+
'package.json': JSON.stringify({
1433+
name: 'test-pkg-arg-filter-with-depth-opt',
1434+
version: '1.0.0',
1435+
dependencies: {
1436+
a: '^1.0.0',
1437+
b: '^1.0.0'
1438+
}
1439+
}),
1440+
node_modules: {
1441+
a: {
1442+
'package.json': JSON.stringify({
1443+
name: 'a',
1444+
version: '1.0.0'
1445+
})
1446+
},
1447+
b: {
1448+
'package.json': JSON.stringify({
1449+
name: 'b',
1450+
version: '1.0.0',
1451+
dependencies: {
1452+
c: '^1.0.0'
1453+
}
1454+
})
1455+
},
1456+
c: {
1457+
'package.json': JSON.stringify({
1458+
name: 'c',
1459+
version: '1.0.0',
1460+
dependencies: {
1461+
d: '^1.0.0'
1462+
}
1463+
})
1464+
},
1465+
d: {
1466+
'package.json': JSON.stringify({
1467+
name: 'd',
1468+
version: '1.0.0',
1469+
dependencies: {
1470+
a: '^1.0.0'
1471+
}
1472+
})
1473+
}
1474+
}
1475+
})
1476+
1477+
t.plan(6)
1478+
ls(['a'], (err) => {
1479+
t.ifError(err, 'should NOT have ELSPROBLEMS error code')
1480+
t.matchSnapshot(redactCwd(result), 'should list a in top-level only')
1481+
1482+
ls(['d'], (err) => {
1483+
t.ifError(err, 'should NOT have ELSPROBLEMS error code when filter')
1484+
t.matchSnapshot(redactCwd(result), 'should print empty results msg')
1485+
1486+
// if no --depth config is defined, should print path to dep
1487+
_flatOptions.depth = null // default config value
1488+
ls(['d'], (err) => {
1489+
t.ifError(err, 'should NOT have ELSPROBLEMS error code when filter')
1490+
t.matchSnapshot(redactCwd(result), 'should print expected result')
1491+
})
1492+
})
1493+
})
1494+
})
1495+
1496+
t.teardown(() => {
1497+
_flatOptions.depth = Infinity
1498+
})
1499+
14291500
t.end()
14301501
})
14311502

0 commit comments

Comments
 (0)