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 ability to use package.json version in comparison #324

Open
wants to merge 3 commits 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 @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(),
Expand All @@ -69,7 +71,8 @@ const cli = meow({
'save-exact',
'color',
'emoji',
'spinner'
'spinner',
'package-version'
],
string: [
'ignore',
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,
packageVersion: cli.flags.packageVersion
};

if (options.debug) {
Expand Down
14 changes: 13 additions & 1 deletion lib/in/create-package-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
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: [],
packageVersion: false,

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