Skip to content

Commit ba8b2a7

Browse files
lukekarrysfritzy
authored andcommitted
fix(ls): make --omit filter npm ls
This makes `npm ls` use the same logic as other commands (eg `outdated`) when parsing config items that filter the output based on package type. Previously `--development` and `--production` has special semantics when used with `npm ls` that were inconsistent with the rest of the CLI. To achieve the same behavior as these deprecated flags use: - in place of `--development` use `--omit peer --omit prod --omit optional` - in place of `--production` use `--omit dev --omit peer` Fixes #4739
1 parent c22fb1e commit ba8b2a7

File tree

4 files changed

+44
-252
lines changed

4 files changed

+44
-252
lines changed

lib/commands/ls.js

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,12 @@ class LS extends ArboristWorkspaceCmd {
5252
const all = this.npm.config.get('all')
5353
const color = this.npm.color
5454
const depth = this.npm.config.get('depth')
55-
const dev = this.npm.config.get('dev')
56-
const development = this.npm.config.get('development')
5755
const global = this.npm.config.get('global')
5856
const json = this.npm.config.get('json')
5957
const link = this.npm.config.get('link')
6058
const long = this.npm.config.get('long')
61-
const only = this.npm.config.get('only')
59+
const omit = this.npm.flatOptions.omit
6260
const parseable = this.npm.config.get('parseable')
63-
const prod = this.npm.config.get('prod')
64-
const production = this.npm.config.get('production')
6561
const unicode = this.npm.config.get('unicode')
6662
const packageLockOnly = this.npm.config.get('package-lock-only')
6763
const workspacesEnabled = this.npm.flatOptions.workspacesEnabled
@@ -138,15 +134,10 @@ class LS extends ArboristWorkspaceCmd {
138134
? []
139135
: [...(node.target).edgesOut.values()]
140136
.filter(filterBySelectedWorkspaces)
141-
.filter(filterByEdgesTypes({
142-
currentDepth,
143-
dev,
144-
development,
137+
.filter(currentDepth === 0 ? filterByEdgesTypes({
145138
link,
146-
prod,
147-
production,
148-
only,
149-
}))
139+
omit,
140+
}) : () => true)
150141
.map(mapEdgesToNodes({ seenPaths }))
151142
.concat(appendExtraneousChildren({ node, seenPaths }))
152143
.sort(sortAlphabetically)
@@ -399,27 +390,13 @@ const getJsonOutputItem = (node, { global, long }) => {
399390
return augmentItemWithIncludeMetadata(node, item)
400391
}
401392

402-
const filterByEdgesTypes = ({
403-
currentDepth,
404-
dev,
405-
development,
406-
link,
407-
prod,
408-
production,
409-
only,
410-
}) => {
411-
// filter deps by type, allows for: `npm ls --dev`, `npm ls --prod`,
412-
// `npm ls --link`, `npm ls --only=dev`, etc
413-
const filterDev = currentDepth === 0 &&
414-
(dev || development || /^dev(elopment)?$/.test(only))
415-
const filterProd = currentDepth === 0 &&
416-
(prod || production || /^prod(uction)?$/.test(only))
417-
const filterLink = currentDepth === 0 && link
418-
419-
return (edge) =>
420-
(filterDev ? edge.dev : true) &&
421-
(filterProd ? (!edge.dev && !edge.peer && !edge.peerOptional) : true) &&
422-
(filterLink ? (edge.to && edge.to.isLink) : true)
393+
const filterByEdgesTypes = ({ link, omit = [] }) => (edge) => {
394+
for (const omitType of omit) {
395+
if (edge[omitType]) {
396+
return false
397+
}
398+
}
399+
return link ? edge.to && edge.to.isLink : true
423400
}
424401

425402
const appendExtraneousChildren = ({ node, seenPaths }) =>

lib/commands/outdated.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class Outdated extends ArboristWorkspaceCmd {
207207
: edge.dev ? 'devDependencies'
208208
: 'dependencies'
209209

210-
for (const omitType of this.npm.config.get('omit')) {
210+
for (const omitType of this.npm.flatOptions.omit) {
211211
if (node[omitType]) {
212212
return
213213
}

0 commit comments

Comments
 (0)