@@ -138,7 +138,12 @@ class LS extends ArboristWorkspaceCmd {
138
138
const item = json
139
139
? getJsonOutputItem ( node , { global, long } )
140
140
: parseable
141
- ? { }
141
+ ? {
142
+ pkgid : node . pkgid ,
143
+ path : node . path ,
144
+ [ _dedupe ] : node [ _dedupe ] ,
145
+ [ _parent ] : node [ _parent ] ,
146
+ }
142
147
: getHumanOutputItem ( node , { args, chalk, global, long } )
143
148
144
149
// loop through list of node problems to add them to global list
@@ -157,13 +162,11 @@ class LS extends ArboristWorkspaceCmd {
157
162
? filterDefaultDepth
158
163
: ( depth || 0 )
159
164
160
- // add root node of tree to list of seenNodes
161
-
162
165
const seenNodes = new Map ( )
163
166
const problems = new Set ( )
164
167
const cache = new Map ( )
165
- const traversePathMap = new Map ( )
166
168
169
+ // add root node of tree to list of seenNodes
167
170
seenNodes . set ( tree . path , tree )
168
171
169
172
const result = exploreDependencyGraph (
@@ -173,8 +176,7 @@ class LS extends ArboristWorkspaceCmd {
173
176
{ json, parseable } ,
174
177
seenNodes ,
175
178
problems ,
176
- cache ,
177
- traversePathMap
179
+ cache
178
180
)
179
181
180
182
// handle the special case of a broken package.json in the root folder
@@ -237,35 +239,40 @@ const exploreDependencyGraph = (
237
239
seenNodes ,
238
240
problems ,
239
241
cache ,
240
- traversePathMap ,
242
+ traversePathMap = new Map ( ) ,
241
243
encounterCount = new Map ( )
242
244
) => {
243
245
// Track the number of encounters for the current node
244
246
// Why because we want to start storing/caching after the node is identified as a deduped edge
247
+
245
248
const count = node . path ? ( encounterCount . get ( node . path ) || 0 ) + 1 : 0
246
249
node . path && encounterCount . set ( node . path , count )
247
250
248
- if ( node . path && cache . has ( node . path ) ) {
251
+ if ( cache . has ( node . path ) ) {
249
252
return cache . get ( node . path )
250
253
}
251
254
252
255
const currentNodeResult = visit ( node , problems )
253
256
257
+ // how the this node is explored
258
+ // so if the explored path contains this node again then it's a cycle
259
+ // and we don't want to explore it again
254
260
const traversePath = [ ...( traversePathMap . get ( currentNodeResult [ _parent ] ) || [ ] ) ]
255
261
const isCircular = traversePath ?. includes ( node . pkgid )
256
262
traversePath . push ( node . pkgid )
257
263
traversePathMap . set ( currentNodeResult , traversePath )
258
264
259
- if ( count > 1 ) {
265
+ // we want to start using cache after node is identified as a deduped
266
+ if ( count > 1 && node . path && node [ _dedupe ] ) {
260
267
cache . set ( node . path , currentNodeResult )
261
268
}
262
269
263
270
// Get children of current node
264
271
const children = isCircular
265
272
? [ ]
266
- : getChildren ( node , currentNodeResult , seenNodes , traversePathMap )
273
+ : getChildren ( node , currentNodeResult , seenNodes )
267
274
268
- // Recurse on each child
275
+ // Recurse on each child node
269
276
for ( const child of children ) {
270
277
const childResult = exploreDependencyGraph (
271
278
child ,
@@ -278,7 +285,9 @@ const exploreDependencyGraph = (
278
285
traversePathMap ,
279
286
encounterCount
280
287
)
288
+ // include current node if any of its children are included
281
289
currentNodeResult [ _include ] = currentNodeResult [ _include ] || childResult [ _include ]
290
+
282
291
if ( childResult [ _include ] && ! parseable ) {
283
292
if ( json ) {
284
293
currentNodeResult . dependencies = currentNodeResult . dependencies || { }
@@ -386,7 +395,7 @@ const getHumanOutputItem = (node, { args, chalk, global, long }) => {
386
395
) +
387
396
( isGitNode ( node ) ? ` (${ node . resolved } )` : '' ) +
388
397
( node . isLink ? ` -> ${ relativePrefix } ${ targetLocation } ` : '' ) +
389
- ( long ? `\n${ node . package . description || '' } ` : '' )
398
+ ( long ? `\n${ node . package ? .description || '' } ` : '' )
390
399
391
400
return ( { label, nodes : [ ] , [ _include ] : node [ _include ] , [ _parent ] : node [ _parent ] } )
392
401
}
@@ -560,16 +569,6 @@ const augmentNodesWithMetadata = ({
560
569
const sortAlphabetically = ( { pkgid : a } , { pkgid : b } ) => localeCompare ( a , b )
561
570
562
571
const humanOutput = ( { chalk, result, unicode } ) => {
563
- // we need to traverse the entire tree in order to determine which items
564
- // should be included (since a nested transitive included dep will make it
565
- // so that all its ancestors should be displayed)
566
- // here is where we put items in their expected place for archy output
567
- // for (const item of seenItems) {
568
- // if (item[_include] && item[_parent]) {
569
- // item[_parent].nodes.push(item)
570
- // }
571
- // }
572
-
573
572
if ( ! result . nodes . length ) {
574
573
result . nodes = [ '(empty)' ]
575
574
}
@@ -591,11 +590,6 @@ const jsonOutput = ({ path, problems, result, rootError }) => {
591
590
result . invalid = true
592
591
}
593
592
594
- // we need to traverse the entire tree in order to determine which items
595
- // should be included (since a nested transitive included dep will make it
596
- // so that all its ancestors should be displayed)
597
- // here is where we put items in their expected place for json output
598
-
599
593
return result
600
594
}
601
595
0 commit comments