Skip to content

Commit

Permalink
fix(ls): make --omit filter npm ls
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lukekarrys authored and fritzy committed Apr 13, 2022
1 parent c22fb1e commit ba8b2a7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 252 deletions.
45 changes: 11 additions & 34 deletions lib/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,12 @@ class LS extends ArboristWorkspaceCmd {
const all = this.npm.config.get('all')
const color = this.npm.color
const depth = this.npm.config.get('depth')
const dev = this.npm.config.get('dev')
const development = this.npm.config.get('development')
const global = this.npm.config.get('global')
const json = this.npm.config.get('json')
const link = this.npm.config.get('link')
const long = this.npm.config.get('long')
const only = this.npm.config.get('only')
const omit = this.npm.flatOptions.omit
const parseable = this.npm.config.get('parseable')
const prod = this.npm.config.get('prod')
const production = this.npm.config.get('production')
const unicode = this.npm.config.get('unicode')
const packageLockOnly = this.npm.config.get('package-lock-only')
const workspacesEnabled = this.npm.flatOptions.workspacesEnabled
Expand Down Expand Up @@ -138,15 +134,10 @@ class LS extends ArboristWorkspaceCmd {
? []
: [...(node.target).edgesOut.values()]
.filter(filterBySelectedWorkspaces)
.filter(filterByEdgesTypes({
currentDepth,
dev,
development,
.filter(currentDepth === 0 ? filterByEdgesTypes({
link,
prod,
production,
only,
}))
omit,
}) : () => true)
.map(mapEdgesToNodes({ seenPaths }))
.concat(appendExtraneousChildren({ node, seenPaths }))
.sort(sortAlphabetically)
Expand Down Expand Up @@ -399,27 +390,13 @@ const getJsonOutputItem = (node, { global, long }) => {
return augmentItemWithIncludeMetadata(node, item)
}

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

return (edge) =>
(filterDev ? edge.dev : true) &&
(filterProd ? (!edge.dev && !edge.peer && !edge.peerOptional) : true) &&
(filterLink ? (edge.to && edge.to.isLink) : true)
const filterByEdgesTypes = ({ link, omit = [] }) => (edge) => {
for (const omitType of omit) {
if (edge[omitType]) {
return false
}
}
return link ? edge.to && edge.to.isLink : true
}

const appendExtraneousChildren = ({ node, seenPaths }) =>
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class Outdated extends ArboristWorkspaceCmd {
: edge.dev ? 'devDependencies'
: 'dependencies'

for (const omitType of this.npm.config.get('omit')) {
for (const omitType of this.npm.flatOptions.omit) {
if (node[omitType]) {
return
}
Expand Down
Loading

0 comments on commit ba8b2a7

Please sign in to comment.