Skip to content

Commit 72bbda7

Browse files
committed
BREAKING: changed npm ls default to depth=0
- npm ls now defaults to only printing root children - added --all flag that is same as --depth=Infinity - --depth now defaults to 0 and can still be used if --all is not truthy
1 parent 433c5e5 commit 72bbda7

File tree

3 files changed

+121
-7
lines changed

3 files changed

+121
-7
lines changed

lib/ls.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ const parseableOutput = ({ long, seenNodes }) => {
344344
const ls = async (args) => {
345345
const path = npm.prefix
346346
const {
347+
all,
347348
color,
348349
depth,
349350
json,
@@ -373,6 +374,7 @@ const ls = async (args) => {
373374
const seenNodes = new Set()
374375
const problems = new Set()
375376
let topLevelChildren = 0
377+
const depthToPrint = all ? Infinity : (depth || 0)
376378

377379
// tree traversal happens here, using treeverse.breadth
378380
const result = breadth({
@@ -381,7 +383,7 @@ const ls = async (args) => {
381383
// the `tree` obj) that was just visited in the `visit` method below
382384
// `nodeResult` is going to be the returned `item` from `visit`
383385
getChildren (node, nodeResult) {
384-
return (!(node instanceof Arborist.Node) || node[_depth] > depth)
386+
return (!(node instanceof Arborist.Node) || (node[_depth] > depthToPrint))
385387
? []
386388
: [...node.edgesOut.values()]
387389
.filter(filterByEdgesTypes({

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ exports[`test/lib/ls.js TAP ls --parseable cycle deps > should print tree output
193193
{CWD}/ls-ls-parseable-cycle-deps/node_modules/b
194194
`
195195

196+
exports[`test/lib/ls.js TAP ls --parseable default --depth value should be 0 > should output parseable output containing only top-level dependencies 1`] = `
197+
{CWD}/ls-ls-parseable-default-depth-value-should-be-0
198+
{CWD}/ls-ls-parseable-default-depth-value-should-be-0/node_modules/foo
199+
{CWD}/ls-ls-parseable-default-depth-value-should-be-0/node_modules/lorem
200+
`
201+
196202
exports[`test/lib/ls.js TAP ls --parseable empty location > should print empty result 1`] = `
197203
{CWD}/ls-ls-parseable-empty-location
198204
`
@@ -282,7 +288,7 @@ exports[`test/lib/ls.js TAP ls --parseable with filter arg nested dep > should o
282288
{CWD}/ls-ls-parseable-with-filter-arg-nested-dep/node_modules/bar
283289
`
284290

285-
exports[`test/lib/ls.js TAP ls --parseable with missing filter arg > should output tree containing no dependencies info 1`] = `
291+
exports[`test/lib/ls.js TAP ls --parseable with missing filter arg > should output parseable output containing no dependencies info 1`] = `
286292
287293
`
288294

@@ -323,6 +329,13 @@ test-npm-ls@1.0.0 {CWD}/ls-ls-cycle-deps
323329
324330
`
325331

332+
exports[`test/lib/ls.js TAP ls default --depth value should be 0 > should output tree containing only top-level dependencies 1`] = `
333+
test-npm-ls@1.0.0 {CWD}/ls-ls-default-depth-value-should-be-0
334+
+-- foo@1.0.0
335+
\`-- lorem@1.0.0
336+
337+
`
338+
326339
exports[`test/lib/ls.js TAP ls empty location > should print empty result 1`] = `
327340
{CWD}/ls-ls-empty-location
328341
\`-- (empty)

test/lib/ls.js

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ let result = ''
8181
// note this _flatOptions representations is for tests-only and does not
8282
// represent exactly the properties found in the actual flatOptions obj
8383
const _flatOptions = {
84+
all: true,
8485
color: false,
8586
dev: false,
8687
depth: Infinity,
@@ -270,7 +271,31 @@ test('ls', (t) => {
270271
})
271272
})
272273

274+
t.test('default --depth value should be 0', (t) => {
275+
_flatOptions.all = false
276+
_flatOptions.depth = undefined
277+
prefix = t.testdir({
278+
'package.json': JSON.stringify({
279+
name: 'test-npm-ls',
280+
version: '1.0.0',
281+
dependencies: {
282+
foo: '^1.0.0',
283+
lorem: '^1.0.0'
284+
}
285+
}),
286+
...simpleNmFixture
287+
})
288+
ls([], (err) => {
289+
t.ifError(err, 'npm ls')
290+
t.matchSnapshot(redactCwd(result), 'should output tree containing only top-level dependencies')
291+
_flatOptions.all = true
292+
_flatOptions.depth = Infinity
293+
t.end()
294+
})
295+
})
296+
273297
t.test('--depth=0', (t) => {
298+
_flatOptions.all = false
274299
_flatOptions.depth = 0
275300
prefix = t.testdir({
276301
'package.json': JSON.stringify({
@@ -286,12 +311,14 @@ test('ls', (t) => {
286311
ls([], (err) => {
287312
t.ifError(err, 'npm ls')
288313
t.matchSnapshot(redactCwd(result), 'should output tree containing only top-level dependencies')
314+
_flatOptions.all = true
289315
_flatOptions.depth = Infinity
290316
t.end()
291317
})
292318
})
293319

294320
t.test('--depth=1', (t) => {
321+
_flatOptions.all = false
295322
_flatOptions.depth = 1
296323
prefix = t.testdir({
297324
'package.json': JSON.stringify({
@@ -345,13 +372,13 @@ test('ls', (t) => {
345372
ls([], (err) => {
346373
t.ifError(err, 'npm ls')
347374
t.matchSnapshot(redactCwd(result), 'should output tree containing top-level deps and their deps only')
375+
_flatOptions.all = true
348376
_flatOptions.depth = Infinity
349377
t.end()
350378
})
351379
})
352380

353381
t.test('missing/invalid/extraneous', (t) => {
354-
_flatOptions.depth = 1
355382
prefix = t.testdir({
356383
'package.json': JSON.stringify({
357384
name: 'test-npm-ls',
@@ -373,7 +400,6 @@ test('ls', (t) => {
373400
'should log missing/invalid/extraneous errors'
374401
)
375402
t.matchSnapshot(redactCwd(result), 'should output tree containing missing, invalid, extraneous labels')
376-
_flatOptions.depth = Infinity
377403
t.end()
378404
})
379405
})
@@ -584,6 +610,7 @@ test('ls', (t) => {
584610
})
585611

586612
t.test('--long --depth=0', (t) => {
613+
_flatOptions.all = false
587614
_flatOptions.depth = 0
588615
_flatOptions.long = true
589616
prefix = t.testdir({
@@ -608,6 +635,7 @@ test('ls', (t) => {
608635
})
609636
ls([], () => {
610637
t.matchSnapshot(redactCwd(result), 'should output tree containing top-level deps with descriptions')
638+
_flatOptions.all = true
611639
_flatOptions.depth = Infinity
612640
_flatOptions.long = false
613641
t.end()
@@ -1040,7 +1068,7 @@ test('ls --parseable', (t) => {
10401068
})
10411069
ls(['notadep'], (err) => {
10421070
t.ifError(err, 'npm ls')
1043-
t.matchSnapshot(redactCwd(result), 'should output tree containing no dependencies info')
1071+
t.matchSnapshot(redactCwd(result), 'should output parseable output containing no dependencies info')
10441072
t.equal(
10451073
process.exitCode,
10461074
1,
@@ -1051,7 +1079,31 @@ test('ls --parseable', (t) => {
10511079
})
10521080
})
10531081

1082+
t.test('default --depth value should be 0', (t) => {
1083+
_flatOptions.all = false
1084+
_flatOptions.depth = undefined
1085+
prefix = t.testdir({
1086+
'package.json': JSON.stringify({
1087+
name: 'test-npm-ls',
1088+
version: '1.0.0',
1089+
dependencies: {
1090+
foo: '^1.0.0',
1091+
lorem: '^1.0.0'
1092+
}
1093+
}),
1094+
...simpleNmFixture
1095+
})
1096+
ls([], (err) => {
1097+
t.ifError(err, 'npm ls')
1098+
t.matchSnapshot(redactCwd(result), 'should output parseable output containing only top-level dependencies')
1099+
_flatOptions.all = true
1100+
_flatOptions.depth = Infinity
1101+
t.end()
1102+
})
1103+
})
1104+
10541105
t.test('--depth=0', (t) => {
1106+
_flatOptions.all = false
10551107
_flatOptions.depth = 0
10561108
prefix = t.testdir({
10571109
'package.json': JSON.stringify({
@@ -1067,12 +1119,14 @@ test('ls --parseable', (t) => {
10671119
ls([], (err) => {
10681120
t.ifError(err, 'npm ls')
10691121
t.matchSnapshot(redactCwd(result), 'should output tree containing only top-level dependencies')
1122+
_flatOptions.all = true
10701123
_flatOptions.depth = Infinity
10711124
t.end()
10721125
})
10731126
})
10741127

10751128
t.test('--depth=1', (t) => {
1129+
_flatOptions.all = false
10761130
_flatOptions.depth = 1
10771131
prefix = t.testdir({
10781132
'package.json': JSON.stringify({
@@ -1088,6 +1142,7 @@ test('ls --parseable', (t) => {
10881142
ls([], (err) => {
10891143
t.ifError(err, 'npm ls')
10901144
t.matchSnapshot(redactCwd(result), 'should output parseable containing top-level deps and their deps only')
1145+
_flatOptions.all = true
10911146
_flatOptions.depth = Infinity
10921147
t.end()
10931148
})
@@ -1377,6 +1432,7 @@ test('ls --parseable', (t) => {
13771432
})
13781433

13791434
t.test('--long --depth=0', (t) => {
1435+
_flatOptions.all = false
13801436
_flatOptions.depth = 0
13811437
_flatOptions.long = true
13821438
prefix = t.testdir({
@@ -1401,6 +1457,7 @@ test('ls --parseable', (t) => {
14011457
})
14021458
ls([], () => {
14031459
t.matchSnapshot(redactCwd(result), 'should output tree containing top-level deps with descriptions')
1460+
_flatOptions.all = true
14041461
_flatOptions.depth = Infinity
14051462
_flatOptions.long = false
14061463
t.end()
@@ -1923,7 +1980,46 @@ test('ls --json', (t) => {
19231980
})
19241981
})
19251982

1983+
t.test('default --depth value should now be 0', (t) => {
1984+
_flatOptions.all = false
1985+
_flatOptions.depth = undefined
1986+
prefix = t.testdir({
1987+
'package.json': JSON.stringify({
1988+
name: 'test-npm-ls',
1989+
version: '1.0.0',
1990+
dependencies: {
1991+
foo: '^1.0.0',
1992+
lorem: '^1.0.0'
1993+
}
1994+
}),
1995+
...simpleNmFixture
1996+
})
1997+
ls([], (err) => {
1998+
t.ifError(err, 'npm ls')
1999+
t.deepEqual(
2000+
jsonParse(result),
2001+
{
2002+
name: 'test-npm-ls',
2003+
version: '1.0.0',
2004+
'dependencies': {
2005+
'foo': {
2006+
'version': '1.0.0'
2007+
},
2008+
'lorem': {
2009+
'version': '1.0.0'
2010+
}
2011+
}
2012+
},
2013+
'should output json containing only top-level dependencies'
2014+
)
2015+
_flatOptions.all = true
2016+
_flatOptions.depth = Infinity
2017+
t.end()
2018+
})
2019+
})
2020+
19262021
t.test('--depth=0', (t) => {
2022+
_flatOptions.all = false
19272023
_flatOptions.depth = 0
19282024
prefix = t.testdir({
19292025
'package.json': JSON.stringify({
@@ -1954,12 +2050,14 @@ test('ls --json', (t) => {
19542050
},
19552051
'should output json containing only top-level dependencies'
19562052
)
2053+
_flatOptions.all = true
19572054
_flatOptions.depth = Infinity
19582055
t.end()
19592056
})
19602057
})
19612058

19622059
t.test('--depth=1', (t) => {
2060+
_flatOptions.all = false
19632061
_flatOptions.depth = 1
19642062
prefix = t.testdir({
19652063
'package.json': JSON.stringify({
@@ -1995,13 +2093,13 @@ test('ls --json', (t) => {
19952093
},
19962094
'should output json containing top-level deps and their deps only'
19972095
)
2096+
_flatOptions.all = true
19982097
_flatOptions.depth = Infinity
19992098
t.end()
20002099
})
20012100
})
20022101

20032102
t.test('missing/invalid/extraneous', (t) => {
2004-
_flatOptions.depth = 1
20052103
prefix = t.testdir({
20062104
'package.json': JSON.stringify({
20072105
name: 'test-npm-ls',
@@ -2056,7 +2154,6 @@ test('ls --json', (t) => {
20562154
},
20572155
'should output json containing top-level deps and their deps only'
20582156
)
2059-
_flatOptions.depth = Infinity
20602157
t.end()
20612158
})
20622159
})
@@ -2555,6 +2652,7 @@ test('ls --json', (t) => {
25552652
})
25562653

25572654
t.test('--long --depth=0', (t) => {
2655+
_flatOptions.all = false
25582656
_flatOptions.depth = 0
25592657
_flatOptions.long = true
25602658
prefix = t.testdir({
@@ -2649,6 +2747,7 @@ test('ls --json', (t) => {
26492747
},
26502748
'should output json containing top-level deps in long format'
26512749
)
2750+
_flatOptions.all = true
26522751
_flatOptions.depth = Infinity
26532752
_flatOptions.long = false
26542753
t.end()

0 commit comments

Comments
 (0)