Skip to content

Commit

Permalink
fix(ls): respect prod config for workspaces
Browse files Browse the repository at this point in the history
`npm ls --prod` is currently not omitting devDependencies for configured
workspaces, this changes it by properly checking for the tweaked
`currentDepth` value instead of root check that was in place.

Fixes: #3382

PR-URL: #3429
Credit: @ruyadorno
Close: #3429
Reviewed-by: @wraithgar
  • Loading branch information
ruyadorno authored and wraithgar committed Jun 17, 2021
1 parent d341bd8 commit b19e56c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class LS extends ArboristWorkspaceCmd {
: [...(node.target || node).edgesOut.values()]
.filter(filterBySelectedWorkspaces)
.filter(filterByEdgesTypes({
currentDepth,
dev,
development,
link,
Expand Down Expand Up @@ -387,6 +388,7 @@ const getJsonOutputItem = (node, { global, long }) => {
}

const filterByEdgesTypes = ({
currentDepth,
dev,
development,
link,
Expand All @@ -398,11 +400,11 @@ const filterByEdgesTypes = ({
}) => {
// filter deps by type, allows for: `npm ls --dev`, `npm ls --prod`,
// `npm ls --link`, `npm ls --only=dev`, etc
const filterDev = node === tree &&
const filterDev = currentDepth === 0 &&
(dev || development || /^dev(elopment)?$/.test(only))
const filterProd = node === tree &&
const filterProd = currentDepth === 0 &&
(prod || production || /^prod(uction)?$/.test(only))
const filterLink = node === tree && link
const filterLink = currentDepth === 0 && link

return (edge) =>
(filterDev ? edge.dev : true) &&
Expand Down
17 changes: 17 additions & 0 deletions tap-snapshots/test/lib/ls.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ workspaces-tree@1.0.0 {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspac
exports[`test/lib/ls.js TAP ls loading a tree containing workspaces > should filter using workspace config 1`] = `
workspaces-tree@1.0.0 {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspaces
\`-- a@1.0.0 -> ./a
+-- baz@1.0.0
+-- c@1.0.0
\`-- d@1.0.0 -> ./d
\`-- foo@1.1.1
Expand All @@ -506,6 +507,21 @@ workspaces-tree@1.0.0 {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspac
exports[`test/lib/ls.js TAP ls loading a tree containing workspaces > should list --all workspaces properly 1`] = `
workspaces-tree@1.0.0 {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspaces
+-- a@1.0.0 -> ./a
| +-- baz@1.0.0
| +-- c@1.0.0
| \`-- d@1.0.0 deduped -> ./d
+-- b@1.0.0 -> ./b
+-- d@1.0.0 -> ./d
| \`-- foo@1.1.1
| \`-- bar@1.0.0
+-- e@1.0.0 -> ./group/e
\`-- f@1.0.0 -> ./group/f
`

exports[`test/lib/ls.js TAP ls loading a tree containing workspaces > should list only prod deps of workspaces 1`] = `
workspaces-tree@1.0.0 {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspaces
+-- a@1.0.0 -> ./a
| +-- c@1.0.0
| \`-- d@1.0.0 deduped -> ./d
+-- b@1.0.0 -> ./b
Expand All @@ -520,6 +536,7 @@ workspaces-tree@1.0.0 {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspac
exports[`test/lib/ls.js TAP ls loading a tree containing workspaces > should list workspaces properly with default configs 1`] = `
workspaces-tree@1.0.0 {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspaces
+-- a@1.0.0 -> ./a
| +-- baz@1.0.0
| +-- c@1.0.0
| \`-- d@1.0.0 deduped -> ./d
+-- b@1.0.0 -> ./b
Expand Down
21 changes: 21 additions & 0 deletions test/lib/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,9 @@ t.test('ls', (t) => {
bar: {
'package.json': JSON.stringify({ name: 'bar', version: '1.0.0' }),
},
baz: {
'package.json': JSON.stringify({ name: 'baz', version: '1.0.0' }),
},
},
a: {
'package.json': JSON.stringify({
Expand All @@ -1458,6 +1461,9 @@ t.test('ls', (t) => {
c: '^1.0.0',
d: '^1.0.0',
},
devDependencies: {
baz: '^1.0.0',
},
}),
},
b: {
Expand Down Expand Up @@ -1520,6 +1526,21 @@ t.test('ls', (t) => {
})
})

// --production
await new Promise((res, rej) => {
config.production = true
ls.exec([], (err) => {
if (err)
rej(err)

t.matchSnapshot(redactCwd(result),
'should list only prod deps of workspaces')

config.production = false
res()
})
})

// filter out a single workspace using args
await new Promise((res, rej) => {
ls.exec(['d'], (err) => {
Expand Down

0 comments on commit b19e56c

Please sign in to comment.