diff --git a/README.md b/README.md index 3a5d7fc9..4e6ba6e0 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ Options -d, --dev-only Look at devDependencies only (skip dependencies). -i, --ignore Ignore dependencies based on succeeding glob. -E, --save-exact Save exact version (x.y.z) instead of caret (^x.y.z) in package.json. + -P, --package-version Use the version in package.json for comparison instead of node_modules --specials List of depcheck specials to include in check for unused dependencies. --no-color Force or disable color output. --no-emoji Remove emoji support. No emoji in default in CI environments. @@ -150,6 +151,12 @@ Install packages using `--save-exact`, meaning exact versions will be saved in p Applies to both `dependencies` and `devDependencies`. +#### `-P, --package-version` + +Compare versions using the package.json defined version instead of what version of the package is installed in node_modules. + +This allows you to update the package.json file to the latest version even if the latest version is installed in node_modules (because your package.json range covers the latest version). + #### `--specials` Check special (e.g. config) files when looking for unused dependencies. diff --git a/lib/cli.js b/lib/cli.js index 6523783d..60a2b8ff 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -33,6 +33,7 @@ const cli = meow({ -d, --dev-only Look at devDependencies only (skip dependencies). -i, --ignore Ignore dependencies based on succeeding glob. -E, --save-exact Save exact version (x.y.z) instead of caret (^x.y.z) in package.json. + -P, --package-version Use the version in package.json for comparison instead of node_modules --specials List of depcheck specials to include in check for unused dependencies. --no-color Force or disable color output. --no-emoji Remove emoji support. No emoji in default in CI environments. @@ -52,7 +53,8 @@ const cli = meow({ p: 'production', d: 'dev-only', E: 'save-exact', - i: 'ignore' + i: 'ignore', + P: 'package-version' }, default: { dir: pkgDir.sync() || process.cwd(), @@ -69,7 +71,8 @@ const cli = meow({ 'save-exact', 'color', 'emoji', - 'spinner' + 'spinner', + 'package-version' ], string: [ 'ignore', @@ -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, + packageVersion: cli.flags.packageVersion }; if (options.debug) { diff --git a/lib/in/create-package-summary.js b/lib/in/create-package-summary.js index bd449659..cac072d1 100644 --- a/lib/in/create-package-summary.js +++ b/lib/in/create-package-summary.js @@ -62,7 +62,19 @@ function createPackageSummary(moduleName, currentState) { const versionWanted = semver.maxSatisfying(versions, packageJsonVersion); - const versionToUse = installedVersion || versionWanted; + let versionToUse = installedVersion || versionWanted; + + if (currentState.get('packageVersion')) { + if(!packageJsonVersion) { + throw new Error(` +Trying to update with --package-version enabled but ${moduleName} doesn't exist in your package.json. + +Consider using --skip-unused to solve this issue +`); + } + versionToUse = packageJsonVersion.replace(/^[^~]/, ''); + } + const usingNonSemver = semver.valid(latest) && semver.lt(latest, '1.0.0-pre'); const bump = semver.valid(latest) && diff --git a/lib/state/state.js b/lib/state/state.js index d4bb3d72..a2f63bd1 100644 --- a/lib/state/state.js +++ b/lib/state/state.js @@ -20,6 +20,7 @@ const defaultOptions = { spinner: false, installer: 'npm', ignore: [], + packageVersion: false, globalPackages: {}, cwdPackageJson: {devDependencies: {}, dependencies: {}},