Skip to content

Commit

Permalink
Add filter support for wildcards and glob expressions. Fixes #219.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Mar 7, 2021
1 parent 5137c29 commit ee27448
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 33 deletions.
40 changes: 19 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,32 @@ Check global packages:
ncu -g
```

You can upgrade specific packages using the `--filter` option or adding additional cli arguments. You can exclude specific packages with the `--reject` option. They accept strings, comma-or-space-delimited lists, or regular expressions:
Filter packages using the `--filter` option or adding additional cli arguments. You can exclude specific packages with the `--reject` option or prefixing a filter with `!`. Supports strings, wildcards, globs, comma-or-space-delimited lists, and regular expressions:

```sh
# upgrade only mocha
ncu mocha
ncu --filter mocha
ncu -f mocha
ncu --filter mocha

# upgrade only chalk, mocha, and react
ncu chalk mocha react
ncu chalk,mocha,react
ncu -f "chalk mocha react"
# upgrade packages that start with "react-"
ncu react-*
ncu "/^react-.*$/"

# do not upgrade nodemon
# upgrade everything except nodemon
ncu \!nodemon
ncu -x nodemon
ncu --reject nodemon

# upgrade packages that start with "gulp-" using regex
ncu "/^gulp-.*$/"

# upgrade packages that do not start with "gulp-".
ncu '/^(?!gulp-).*$/' # mac/linux
ncu "/^(?!gulp-).*$/" # windows
```

Detailed output with links to each repository:
# upgrade only chalk, mocha, and react
ncu chalk mocha react
ncu chalk, mocha, react
ncu -f "chalk mocha react"

```sh
ncu --format repo
# upgrade packages that do not start with "react-".
ncu \!react-*
ncu '/^(?!react-).*$/' # mac/linux
ncu "/^(?!react-).*$/" # windows
```

## How dependency updates are determined
Expand Down Expand Up @@ -143,8 +140,8 @@ ncu --format repo
if no packages need updating (useful for
continuous integration). (default: 1)
-f, --filter <matches> Include only package names matching the given
string, comma-or-space-delimited list, or
/regex/.
string, wildcard, glob, comma-or-space-delimited
list, or /regex/.
--filterVersion <matches> Filter on package version using
comma-or-space-delimited list, or /regex/.
--format <value> Enable additional output data, string or
Expand Down Expand Up @@ -180,7 +177,8 @@ ncu --format repo
--prefix <path> Current working directory of npm.
-r, --registry <url> Third-party npm registry.
-x, --reject <matches> Exclude packages matching the given string,
comma-or-space-delimited list, or /regex/.
wildcard, glob, comma-or-space-delimited list,
or /regex/.
--rejectVersion <matches> Exclude package.json versions using
comma-or-space-delimited list, or /regex/.
--removeRange Remove version ranges from the final package
Expand Down
4 changes: 2 additions & 2 deletions lib/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const cliOptions = [
long: 'filter',
short: 'f',
arg: 'matches',
description: 'Include only package names matching the given string, comma-or-space-delimited list, or /regex/.',
description: 'Include only package names matching the given string, wildcard, glob, comma-or-space-delimited list, or /regex/.',
type: 'string | string[] | RegExp'
},
{
Expand Down Expand Up @@ -201,7 +201,7 @@ const cliOptions = [
long: 'reject',
short: 'x',
arg: 'matches',
description: 'Exclude packages matching the given string, comma-or-space-delimited list, or /regex/.',
description: 'Exclude packages matching the given string, wildcard, glob, comma-or-space-delimited list, or /regex/.',
type: 'string | string[] | RegExp'
},
{
Expand Down
4 changes: 2 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ declare namespace ncu {
errorLevel?: number;

/**
* Include only package names matching the given string, comma-or-space-delimited list, or /regex/.
* Include only package names matching the given string, wildcard, glob, comma-or-space-delimited list, or /regex/.
*/
filter?: string | string[] | RegExp;

Expand Down Expand Up @@ -156,7 +156,7 @@ declare namespace ncu {
registry?: string;

/**
* Exclude packages matching the given string, comma-or-space-delimited list, or /regex/.
* Exclude packages matching the given string, wildcard, glob, comma-or-space-delimited list, or /regex/.
*/
reject?: string | string[] | RegExp;

Expand Down
9 changes: 5 additions & 4 deletions lib/versionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const semver = require('semver')
const _ = require('lodash')
const cint = require('cint')
const chalk = require('chalk')
const minimatch = require('minimatch')
const semverutils = require('semver-utils')
const ProgressBar = require('progress')
const prompts = require('prompts')
Expand Down Expand Up @@ -126,7 +127,7 @@ function isUpgradeable(current, latest) {

/**
* Creates a filter function from a given filter string. Supports
* strings, comma-or-space-delimited lists, and regexes.
* strings, wildcards, comma-or-space-delimited lists, and regexes.
*
* @param [filter]
* @returns
Expand All @@ -146,10 +147,10 @@ function composeFilter(filterPattern) {
const regexp = new RegExp(filterPattern.slice(1, -1))
predicate = s => regexp.test(s)
}
// plain string
// glob string
else {
const packages = filterPattern.split(/[\s,]+/)
predicate = s => packages.includes(s)
const patterns = filterPattern.split(/[\s,]+/)
predicate = s => patterns.some(pattern => minimatch(s, pattern))
}
}
// array
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"libnpmconfig": "^1.2.1",
"lodash": "^4.17.21",
"mem": "^8.0.0",
"minimatch": "^3.0.4",
"p-map": "^4.0.0",
"pacote": "^11.2.7",
"parse-github-url": "^1.0.2",
Expand Down
37 changes: 33 additions & 4 deletions test/filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
const fs = require('fs')
const path = require('path')
const chai = require('chai')
const repoUrl = require('../lib/repo-url')
const ncu = require('../lib/index')

const should = chai.should()
chai.should()
process.env.NCU_TESTS = true

describe('filter', () => {

Expand All @@ -15,7 +15,7 @@ describe('filter', () => {
packageData: fs.readFileSync(path.join(__dirname, '/ncu/package2.json'), 'utf-8'),
args: ['lodash.map']
})
upgraded.should.have.property('lodash.map'),
upgraded.should.have.property('lodash.map')
upgraded.should.not.have.property('lodash.filter')
})

Expand All @@ -24,8 +24,37 @@ describe('filter', () => {
packageData: fs.readFileSync(path.join(__dirname, '/ncu/package2.json'), 'utf-8'),
args: ['lodash.map', 'lodash.filter']
})
upgraded.should.have.property('lodash.map'),
upgraded.should.have.property('lodash.map')
upgraded.should.have.property('lodash.filter')
})

it('filter with wildcard', async () => {
const upgraded = await ncu.run({
packageData: JSON.stringify({
dependencies: {
lodash: '2.0.0',
'lodash.map': '2.0.0',
'lodash.filter': '2.0.0'
}
}),
args: ['lodash.*']
})
upgraded.should.have.property('lodash.map')
upgraded.should.have.property('lodash.filter')
})

it('filter with negated wildcard', async () => {
const upgraded = await ncu.run({
packageData: JSON.stringify({
dependencies: {
lodash: '2.0.0',
'lodash.map': '2.0.0',
'lodash.filter': '2.0.0'
}
}),
args: ['!lodash.*']
})
upgraded.should.have.property('lodash')
})

})

0 comments on commit ee27448

Please sign in to comment.