Skip to content

Commit e07bc57

Browse files
committed
fix json output for cycles
1 parent cd0199f commit e07bc57

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

lib/commands/ls.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,14 @@ class LS extends ArboristWorkspaceCmd {
104104
return true
105105
}
106106

107-
const getChildren = (node, nodeResult, seenNodes, traversePathMap) => {
107+
const getChildren = (node, nodeResult, seenNodes) => {
108108
const seenPaths = new Set()
109109
const workspace = node.isWorkspace
110110
const currentDepth = workspace ? 0 : node[_depth]
111111
const target = (node.target)?.edgesOut
112112

113-
const traversePath = [...(traversePathMap.get(nodeResult[_parent]) || [])]
114-
const isCircular = traversePath?.includes(node.realpath + '-' + node.pkgid)
115-
traversePath.push(node.realpath + '-' + node.pkgid)
116-
traversePathMap.set(nodeResult, traversePath)
117-
118113
const shouldSkipChildren =
119114
(currentDepth > depthToPrint) || !nodeResult
120-
|| isCircular
121115

122116
return (shouldSkipChildren || !target)
123117
? []
@@ -256,12 +250,20 @@ const exploreDependencyGraph = (
256250
}
257251

258252
const currentNodeResult = visit(node, problems)
259-
if (count > 2) {
253+
254+
const traversePath = [...(traversePathMap.get(currentNodeResult[_parent]) || [])]
255+
const isCircular = traversePath?.includes(node.pkgid)
256+
traversePath.push(node.pkgid)
257+
traversePathMap.set(currentNodeResult, traversePath)
258+
259+
if (count > 1) {
260260
cache.set(node.path, currentNodeResult)
261261
}
262262

263263
// Get children of current node
264-
const children = getChildren(node, currentNodeResult, seenNodes, traversePathMap)
264+
const children = isCircular
265+
? []
266+
: getChildren(node, currentNodeResult, seenNodes, traversePathMap)
265267

266268
// Recurse on each child
267269
for (const child of children) {
@@ -390,7 +392,7 @@ const getHumanOutputItem = (node, { args, chalk, global, long }) => {
390392
}
391393

392394
const getJsonOutputItem = (node, { global, long }) => {
393-
const item = { [_include]: node[_include] }
395+
const item = { [_include]: node[_include], [_parent]: node[_parent] }
394396

395397
if (node.version) {
396398
item.version = node.version

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ exports[`test/lib/commands/ls.js TAP ls with args and dedupe entries > should pr
600600
| \`-- @npmcli/b@1.1.2 deduped
601601
+-- @npmcli/b@1.1.2
602602
\`-- @npmcli/c@1.0.0
603-
[0m \`-- [33m@npmcli/b@1.1.2[39m [2mdeduped[22m[0m
603+
[0m \`-- [33m@npmcli/b@1.1.2[39m[0m
604604

605605
`
606606

@@ -610,7 +610,7 @@ dedupe-entries@1.0.0 {CWD}/prefix
610610
| \`-- @npmcli/c@1.0.0 deduped
611611
+-- @npmcli/b@1.1.2
612612
| \`-- @npmcli/c@1.0.0 deduped
613-
\`-- @npmcli/c@1.0.0
613+
\`-- @npmcli/c@1.0.0 deduped
614614
`
615615

616616
exports[`test/lib/commands/ls.js TAP ls with dot filter arg > should output tree contaning only occurrences of filtered by package and colored output 1`] = `
@@ -648,7 +648,7 @@ dedupe-entries@1.0.0 {CWD}/prefix
648648
| \`-- @npmcli/b@1.1.2 deduped
649649
+-- @npmcli/b@1.1.2
650650
\`-- @npmcli/c@1.0.0
651-
\`-- @npmcli/b@1.1.2 deduped
651+
\`-- @npmcli/b@1.1.2
652652
`
653653

654654
exports[`test/lib/commands/ls.js TAP ls with no args dedupe entries and not displaying all > should print tree output containing deduped ref 1`] = `
@@ -672,6 +672,6 @@ test-npm-ls@1.0.0 {CWD}/prefix
672672
+-- chai@1.0.0 extraneous
673673
| \`-- dog@1.0.0 deduped invalid: "^1.2.3" from the root project, "^2.0.0" from node_modules/cat, "2.x" from node_modules/chai
674674
| \`-- cat@1.0.0 deduped invalid: "^2.0.0" from the root project
675-
| \`-- dog@1.0.0 deduped invalid: "^1.2.3" from the root project, "^2.0.0" from node_modules/cat, "2.x" from node_modules/chai, "^2.0.0" from node_modules/cat
676-
\`-- dog@1.0.0 deduped invalid: "^1.2.3" from the root project, "^2.0.0" from node_modules/cat, "2.x" from node_modules/chai, "^2.0.0" from node_modules/cat
675+
\`-- dog@1.0.0 deduped invalid: "^1.2.3" from the root project, "^2.0.0" from node_modules/cat, "2.x" from node_modules/chai
676+
\`-- cat@1.0.0 deduped invalid: "^2.0.0" from the root project
677677
`

0 commit comments

Comments
 (0)