Skip to content

Commit b19e56c

Browse files
ruyadornowraithgar
authored andcommitted
fix(ls): respect prod config for workspaces
`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
1 parent d341bd8 commit b19e56c

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

lib/ls.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class LS extends ArboristWorkspaceCmd {
141141
: [...(node.target || node).edgesOut.values()]
142142
.filter(filterBySelectedWorkspaces)
143143
.filter(filterByEdgesTypes({
144+
currentDepth,
144145
dev,
145146
development,
146147
link,
@@ -387,6 +388,7 @@ const getJsonOutputItem = (node, { global, long }) => {
387388
}
388389

389390
const filterByEdgesTypes = ({
391+
currentDepth,
390392
dev,
391393
development,
392394
link,
@@ -398,11 +400,11 @@ const filterByEdgesTypes = ({
398400
}) => {
399401
// filter deps by type, allows for: `npm ls --dev`, `npm ls --prod`,
400402
// `npm ls --link`, `npm ls --only=dev`, etc
401-
const filterDev = node === tree &&
403+
const filterDev = currentDepth === 0 &&
402404
(dev || development || /^dev(elopment)?$/.test(only))
403-
const filterProd = node === tree &&
405+
const filterProd = currentDepth === 0 &&
404406
(prod || production || /^prod(uction)?$/.test(only))
405-
const filterLink = node === tree && link
407+
const filterLink = currentDepth === 0 && link
406408

407409
return (edge) =>
408410
(filterDev ? edge.dev : true) &&

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

+17
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ [email protected] {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspac
496496
exports[`test/lib/ls.js TAP ls loading a tree containing workspaces > should filter using workspace config 1`] = `
497497
[email protected] {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspaces
498498
\`-- [email protected] -> ./a
499+
499500
500501
\`-- [email protected] -> ./d
501502
@@ -506,6 +507,21 @@ [email protected] {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspac
506507
exports[`test/lib/ls.js TAP ls loading a tree containing workspaces > should list --all workspaces properly 1`] = `
507508
[email protected] {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspaces
508509
510+
511+
512+
| \`-- [email protected] deduped -> ./d
513+
514+
515+
516+
517+
+-- [email protected] -> ./group/e
518+
\`-- [email protected] -> ./group/f
519+
520+
`
521+
522+
exports[`test/lib/ls.js TAP ls loading a tree containing workspaces > should list only prod deps of workspaces 1`] = `
523+
[email protected] {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspaces
524+
509525
510526
| \`-- [email protected] deduped -> ./d
511527
@@ -520,6 +536,7 @@ [email protected] {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspac
520536
exports[`test/lib/ls.js TAP ls loading a tree containing workspaces > should list workspaces properly with default configs 1`] = `
521537
[[email protected] {CWD}/tap-testdir-ls-ls-loading-a-tree-containing-workspaces
522538
+-- [[email protected] -> ./a
539+
| +-- [email protected]
523540
| +-- [email protected]
524541
| \`-- [email protected] deduped -> ./d
525542
+-- [[email protected] -> ./b

test/lib/ls.js

+21
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,9 @@ t.test('ls', (t) => {
14491449
bar: {
14501450
'package.json': JSON.stringify({ name: 'bar', version: '1.0.0' }),
14511451
},
1452+
baz: {
1453+
'package.json': JSON.stringify({ name: 'baz', version: '1.0.0' }),
1454+
},
14521455
},
14531456
a: {
14541457
'package.json': JSON.stringify({
@@ -1458,6 +1461,9 @@ t.test('ls', (t) => {
14581461
c: '^1.0.0',
14591462
d: '^1.0.0',
14601463
},
1464+
devDependencies: {
1465+
baz: '^1.0.0',
1466+
},
14611467
}),
14621468
},
14631469
b: {
@@ -1520,6 +1526,21 @@ t.test('ls', (t) => {
15201526
})
15211527
})
15221528

1529+
// --production
1530+
await new Promise((res, rej) => {
1531+
config.production = true
1532+
ls.exec([], (err) => {
1533+
if (err)
1534+
rej(err)
1535+
1536+
t.matchSnapshot(redactCwd(result),
1537+
'should list only prod deps of workspaces')
1538+
1539+
config.production = false
1540+
res()
1541+
})
1542+
})
1543+
15231544
// filter out a single workspace using args
15241545
await new Promise((res, rej) => {
15251546
ls.exec(['d'], (err) => {

0 commit comments

Comments
 (0)