diff --git a/src/helpers/npm.js b/src/helpers/npm.js index e1aff780a634c8e8ee241790ddd33ecdea4871c3..c54b62ac55980f7f0e443d4c705f5b72efe1739c 100644 --- a/src/helpers/npm.js +++ b/src/helpers/npm.js @@ -40,17 +40,17 @@ module.exports = { } allDependencyChecks.push(getDependencyUpgrades(depName, currentVersion) .then(res => { - if (Object.keys(res).length > 0) { + if (res.length > 0) { if (config.verbose) { console.log(`${depName}: Upgrades = ${JSON.stringify(res)}`); } - Object.keys(res).forEach(function(key) { + res.forEach(function(upgrade) { allDependencyUpgrades.push({ - upgradeType: (key === 'pin') ? 'pin' : 'upgrade', depType: depType, depName: depName, currentVersion: currentVersion, - newVersion: res[key], + upgradeType: upgrade.type, + newVersion: upgrade.version, }); }); } else { @@ -78,11 +78,17 @@ function getDependencyUpgrades(depName, currentVersion) { if (!res.body['versions']) { console.log(depName + ' versions is null'); } + const allUpgrades = {}; if (isRange(currentVersion)) { // Pin ranges to their maximum satisfying version - return { 'pin': semver.maxSatisfying(Object.keys(res.body.versions), currentVersion) }; + const maxSatisfying = semver.maxSatisfying(Object.keys(res.body.versions), currentVersion); + allUpgrades['pin'] = { + type: 'pin', + version: maxSatisfying, + }; + currentVersion = maxSatisfying; } - const allUpgrades = {}; + const currentMajor = semver.major(currentVersion); Object.keys(res.body['versions']).forEach(function(version) { if (stable.is(currentVersion) && !stable.is(version)) { // Ignore unstable versions, unless the current version is unstable @@ -91,12 +97,20 @@ function getDependencyUpgrades(depName, currentVersion) { if (semver.gt(version, currentVersion)) { // Group by major versions var thisMajor = semver.major(version); - if (!allUpgrades[thisMajor] || semver.gt(version, allUpgrades[thisMajor])) { - allUpgrades[thisMajor] = version; + if (!allUpgrades[thisMajor] || semver.gt(version, allUpgrades[thisMajor].version)) { + allUpgrades[thisMajor] = { + type: (thisMajor > currentMajor) ? 'major' : 'minor', + version: version, + }; } } }); - return allUpgrades; + if (allUpgrades['pin'] && Object.keys(allUpgrades).length > 1) { + // Remove the pin + delete allUpgrades['pin']; + } + // Return only the values + return Object.keys(allUpgrades).map(key => allUpgrades[key]); }); } diff --git a/src/index.js b/src/index.js index f6fe2bc3dd4a0acf56907e6bce1a1fb13dd87289..df579be4adc5ea4408a5a966cdc9ead361ba628f 100644 --- a/src/index.js +++ b/src/index.js @@ -65,21 +65,22 @@ function processUpgradesSequentially(upgrades) { // 2. Edge case collision of branch name, e.g. dependency also listed as dev dependency return upgrades.reduce((promise, upgrade) => { return promise.then(() => { - return updateDependency(upgrade.upgradeType, upgrade.depType, upgrade.depName, upgrade.currentVersion, upgrade.newVersion); + return updateDependency(upgrade); }); }, Promise.resolve()); } -function updateDependency(upgradeType, depType, depName, currentVersion, newVersion) { +function updateDependency({ upgradeType, depType, depName, currentVersion, newVersion }) { const newVersionMajor = semver.major(newVersion); const branchName = config.templates.branchName({depType, depName, currentVersion, newVersion, newVersionMajor}); let prTitle = ''; if (upgradeType === 'pin') { prTitle = config.templates.prTitlePin({ depType, depName, currentVersion, newVersion, newVersionMajor }); - } else if (newVersionMajor > semver.major(currentVersion)) { - prTitle = config.templates.prTitleMajor({ depType, depName, currentVersion, newVersion, newVersionMajor }); - } else { + } else if (upgradeType === 'minor') { + // Use same title for range or minor prTitle = config.templates.prTitleMinor({ depType, depName, currentVersion, newVersion, newVersionMajor }); + } else { + prTitle = config.templates.prTitleMajor({ depType, depName, currentVersion, newVersion, newVersionMajor }); } const prBody = config.templates.prBody({ depName, currentVersion, newVersion }); const commitMessage = config.templates.commitMessage({ depName, currentVersion, newVersion });