diff --git a/docs/content/commands/npm-ls.md b/docs/content/commands/npm-ls.md index fb752742d2d4c..3c662176327bf 100644 --- a/docs/content/commands/npm-ls.md +++ b/docs/content/commands/npm-ls.md @@ -199,6 +199,16 @@ brand new workspace within the project. This value is not exported to the environment for child processes. +#### `workspaces` + +* Default: false +* Type: Boolean + +Enable running a command in the context of **all** the configured +workspaces. + +This value is not exported to the environment for child processes. + ### See Also diff --git a/lib/ls.js b/lib/ls.js index 7e9e65aa87f33..4e504912a0175 100644 --- a/lib/ls.js +++ b/lib/ls.js @@ -50,7 +50,7 @@ class LS extends ArboristWorkspaceCmd { 'omit', 'link', 'unicode', - 'workspace', + ...super.params, ] } @@ -90,14 +90,22 @@ class LS extends ArboristWorkspaceCmd { const tree = await this.initTree({arb, args }) // filters by workspaces nodes when using -w - let filterSet + // We only have to filter the first layer of edges, so we don't + // explore anything that isn't part of the selected workspace set. + let wsNodes if (this.workspaces && this.workspaces.length) - filterSet = arb.workspaceDependencySet(tree, this.workspaces) + wsNodes = arb.workspaceNodes(tree, this.workspaces) const filterBySelectedWorkspaces = edge => { - const node = edge && edge.to && (edge.to.target || edge.to) - return !filterSet - || filterSet.size === 0 - || (node && filterSet.has(node)) + if (!wsNodes || !wsNodes.length) + return true + + if (edge.from.isProjectRoot) { + return edge.to && + edge.to.isWorkspace & + wsNodes.includes(edge.to.target) + } + + return true } const seenItems = new Set() diff --git a/tap-snapshots/test/lib/load-all-commands.js.test.cjs b/tap-snapshots/test/lib/load-all-commands.js.test.cjs index c48745d67ec69..2e65cd8d9f1b9 100644 --- a/tap-snapshots/test/lib/load-all-commands.js.test.cjs +++ b/tap-snapshots/test/lib/load-all-commands.js.test.cjs @@ -539,6 +539,8 @@ Options: [-a|--all] [--json] [-l|--long] [-p|--parseable] [-g|--global] [--depth ] [--omit [--omit ...]] [--link] [--unicode] +[-w|--workspace [-w|--workspace ...]] +[-ws|--workspaces] alias: la @@ -587,6 +589,8 @@ Options: [-a|--all] [--json] [-l|--long] [-p|--parseable] [-g|--global] [--depth ] [--omit [--omit ...]] [--link] [--unicode] +[-w|--workspace [-w|--workspace ...]] +[-ws|--workspaces] alias: list diff --git a/tap-snapshots/test/lib/utils/npm-usage.js.test.cjs b/tap-snapshots/test/lib/utils/npm-usage.js.test.cjs index 273ccc87ad82f..b7cd5999fa2b5 100644 --- a/tap-snapshots/test/lib/utils/npm-usage.js.test.cjs +++ b/tap-snapshots/test/lib/utils/npm-usage.js.test.cjs @@ -641,6 +641,7 @@ All commands: [--omit [--omit ...]] [--link] [--unicode] [-w|--workspace [-w|--workspace ...]] + [-ws|--workspaces] alias: la @@ -684,6 +685,7 @@ All commands: [--omit [--omit ...]] [--link] [--unicode] [-w|--workspace [-w|--workspace ...]] + [-ws|--workspaces] alias: list