From 3042e1a62f446487f6321aebb1fbebaadd403028 Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 13 May 2019 11:14:11 +0800 Subject: [PATCH] check save-prefix satisfies requested version range --- lib/install/deps.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/install/deps.js b/lib/install/deps.js index 3fe370140adc3..ff6731f938895 100644 --- a/lib/install/deps.js +++ b/lib/install/deps.js @@ -318,7 +318,18 @@ function computeVersionSpec (tree, child) { if (semver.valid(version, true) && semver.gte(version, '0.1.0', true) && !npm.config.get('save-exact')) { - rangeDescriptor = npm.config.get('save-prefix') + if (requested.type === 'range') { + // add save-prefix '^' or '~' only when the future candidates satisfies the + // requested range version spec + var futureVersion = semver.maxSatisfying([semver.inc(version, 'major'), + semver.inc(version, 'minor'), semver.inc(version, 'patch')], + npm.config.get('save-prefix') + version) + if (semver.satisfies(futureVersion, requested.fetchSpec)) { + rangeDescriptor = npm.config.get('save-prefix') + } + } else { + rangeDescriptor = npm.config.get('save-prefix') + } } if (requested.type === 'alias') { rangeDescriptor = `npm:${requested.subSpec.name}@${rangeDescriptor}`