Skip to content

Commit

Permalink
Allow glob pattern as arg for packageFile cli option. closes raineors…
Browse files Browse the repository at this point in the history
…hine#522

Implement --deep flag as alias of
`--packageFile '{,*[!node_modules]/**/}package.json'`
  • Loading branch information
mkungla committed Jan 20, 2021
1 parent 5f11300 commit 7fd4290
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
9 changes: 7 additions & 2 deletions lib/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const cliOptions = [
arg: 'path',
description: 'Working directory in which npm will be executed.'
},
{
long: 'deep',
description: `Run recursively in current working directory. Alias of (--packageFile '${deepPattern}').`,
type: 'boolean'
},
{
long: 'dep',
arg: 'value',
Expand Down Expand Up @@ -160,8 +165,8 @@ const cliOptions = [
},
{
long: 'packageFile',
arg: 'path',
description: 'Package file location (default: ./package.json).'
arg: 'path|glob-pattern',
description: 'Package file location or glob pattern (default: ./package.json).'
},
{
long: 'packageManager',
Expand Down
4 changes: 3 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ npm run test
Saving partially upgraded package.json
`

module.exports = { doctorHelpText, supportedVersionTargets }
const deepPattern = '{,*[!node_modules]/**/}package.json'

module.exports = { deepPattern, doctorHelpText, supportedVersionTargets }
36 changes: 29 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const fs = require('fs')
const path = require('path')
const glob = require('glob')
const { promisify } = require('util')
const cint = require('cint')
const findUp = require('find-up')
Expand All @@ -18,7 +19,7 @@ const vm = require('./versionmanager')
const doctor = require('./doctor')
const packageManagers = require('./package-managers')
const { print, printJson, printUpgrades } = require('./logging')
const { doctorHelpText } = require('./constants')
const { deepPattern, doctorHelpText } = require('./constants')
const cliOptions = require('./cli-options')

// maps package managers to package file names
Expand Down Expand Up @@ -238,6 +239,9 @@ function initOptions(options) {
else if (options.filter && options.args.length > 0 && options.filter !== options.args.join(' ')) {
programError(options, chalk.red('Cannot specify a filter using both --filter and args. Did you forget to quote an argument?') + '\nSee: https://github.com/raineorshine/npm-check-updates/issues/759#issuecomment-723587297')
}
else if (options.packageFile && options.deep) {
programError(options, chalk.red(`Cannot specify both --packageFile and --deep. --deep is an alias for --packageFile '${deepPattern}'`))
}

const target = options.newest ? 'newest'
: options.greatest ? 'greatest'
Expand All @@ -250,6 +254,10 @@ function initOptions(options) {
...options.ownerChanged ? ['ownerChanged'] : []
]

if (options.deep) {
options.packageFile = deepPattern
}

return {
...options,
filter: options.args.join(' ') || options.filter,
Expand Down Expand Up @@ -415,17 +423,31 @@ async function run(options = {}) {
}

async function getAnalysis() {
const defaultPackageFilename = getPackageFileName(options)
const pkgs = await glob.sync(defaultPackageFilename)

let analysis
if (options.global) {
const analysis = await analyzeGlobalPackages(options)
clearTimeout(timeout)
return analysis
analysis = await analyzeGlobalPackages(options)
}
else if (pkgs.length > 1) {
// check --deep
pkgs.reduce(async (accumulator, packageFile) => {
await accumulator
if (packageFile.length > 0 && packageFile !== defaultPackageFilename) {
options.packageFile = packageFile // set packageFile only if needed
}
const [pkgData, pkgFile] = await findPackage(options)
await analyzeProjectDependencies(options, pkgData, pkgFile)
return accumulator
}, [])
}
else {
const [pkgData, pkgFile] = await findPackage(options)
const analysis = await analyzeProjectDependencies(options, pkgData, pkgFile)
clearTimeout(timeout)
return analysis
analysis = await analyzeProjectDependencies(options, pkgData, pkgFile)
}
clearTimeout(timeout)
return analysis
}

// doctor mode
Expand Down

0 comments on commit 7fd4290

Please sign in to comment.