Skip to content

fix(ls): make --omit filter npm ls #4744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ test-all: deps
node bin/npm-cli.js run test-all

ls-ok:
node bin/npm-cli.js ls --production >/dev/null
node bin/npm-cli.js ls --omit=dev >/dev/null

gitclean:
git clean -fd
Expand All @@ -97,7 +97,7 @@ link: uninstall
node bin/npm-cli.js link -f --ignore-scripts

prune: deps
node bin/npm-cli.js prune --production --no-save --no-audit --no-fund
node bin/npm-cli.js prune --omit=dev --no-save --no-audit --no-fund
node scripts/git-dirty.js

publish: gitclean ls-ok link test-all docs prune
Expand Down
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 @@ -208,7 +208,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