diff --git a/README.md b/README.md index 3a5d7fc9..2ca36540 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ Options -p, --production Skip devDependencies. -d, --dev-only Look at devDependencies only (skip dependencies). -i, --ignore Ignore dependencies based on succeeding glob. + -f, --filter Filter dependencies based on glob. -E, --save-exact Save exact version (x.y.z) instead of caret (^x.y.z) in package.json. --specials List of depcheck specials to include in check for unused dependencies. --no-color Force or disable color output. @@ -144,6 +145,12 @@ Ignore dependencies that match specified glob. `$ npm-check -i babel-*` will ignore all dependencies starting with 'babel-'. +#### `-f, --filter` + +Only include dependencies that match specified glob. + +`$ npm-check -f babel-*` will only include dependencies starting with 'babel-'. + #### `-E, --save-exact` Install packages using `--save-exact`, meaning exact versions will be saved in package.json. diff --git a/lib/cli.js b/lib/cli.js index 6523783d..8aa25334 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -32,6 +32,7 @@ const cli = meow({ -p, --production Skip devDependencies. -d, --dev-only Look at devDependencies only (skip dependencies). -i, --ignore Ignore dependencies based on succeeding glob. + -f, --filter Include dependencies based on glob. -E, --save-exact Save exact version (x.y.z) instead of caret (^x.y.z) in package.json. --specials List of depcheck specials to include in check for unused dependencies. --no-color Force or disable color output. @@ -52,7 +53,8 @@ const cli = meow({ p: 'production', d: 'dev-only', E: 'save-exact', - i: 'ignore' + i: 'ignore', + f: 'filter' }, default: { dir: pkgDir.sync() || process.cwd(), @@ -73,7 +75,8 @@ const cli = meow({ ], string: [ 'ignore', - 'specials' + 'specials', + 'filter' ] }); @@ -91,7 +94,8 @@ const options = { installer: process.env.NPM_CHECK_INSTALLER || 'auto', debug: cli.flags.debug, spinner: cli.flags.spinner, - ignore: cli.flags.ignore + ignore: cli.flags.ignore, + filter: cli.flags.filter }; if (options.debug) { diff --git a/lib/in/create-package-summary.js b/lib/in/create-package-summary.js index bd449659..049f1288 100644 --- a/lib/in/create-package-summary.js +++ b/lib/in/create-package-summary.js @@ -41,6 +41,16 @@ function createPackageSummary(moduleName, currentState) { } } + // If --filter is defined, only include specified package globs + const filter = currentState.get('filter'); + if (filter) { + const filterMatch = Array.isArray(filter) ? filter.some(filteredModule => minimatch(moduleName, filteredModule)) : minimatch(moduleName, filter); + + if (!filterMatch) { + return false; + } + } + const unusedDependencies = currentState.get('unusedDependencies'); const missingFromPackageJson = currentState.get('missingFromPackageJson'); diff --git a/lib/state/state.js b/lib/state/state.js index d4bb3d72..a14186c4 100644 --- a/lib/state/state.js +++ b/lib/state/state.js @@ -20,6 +20,7 @@ const defaultOptions = { spinner: false, installer: 'npm', ignore: [], + filter: [], globalPackages: {}, cwdPackageJson: {devDependencies: {}, dependencies: {}},