Skip to content
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

Adding filter command-line option #323

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(),
Expand All @@ -73,7 +75,8 @@ const cli = meow({
],
string: [
'ignore',
'specials'
'specials',
'filter'
]
});

Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions lib/in/create-package-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
1 change: 1 addition & 0 deletions lib/state/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const defaultOptions = {
spinner: false,
installer: 'npm',
ignore: [],
filter: [],

globalPackages: {},
cwdPackageJson: {devDependencies: {}, dependencies: {}},
Expand Down