Skip to content
Snippets Groups Projects
Commit 32a2f22f authored by Rhys Arkins's avatar Rhys Arkins
Browse files

Support ranges

Closes #7
parent 2189313d
No related branches found
No related tags found
No related merge requests found
...@@ -40,17 +40,17 @@ module.exports = { ...@@ -40,17 +40,17 @@ module.exports = {
} }
allDependencyChecks.push(getDependencyUpgrades(depName, currentVersion) allDependencyChecks.push(getDependencyUpgrades(depName, currentVersion)
.then(res => { .then(res => {
if (Object.keys(res).length > 0) { if (res.length > 0) {
if (config.verbose) { if (config.verbose) {
console.log(`${depName}: Upgrades = ${JSON.stringify(res)}`); console.log(`${depName}: Upgrades = ${JSON.stringify(res)}`);
} }
Object.keys(res).forEach(function(key) { res.forEach(function(upgrade) {
allDependencyUpgrades.push({ allDependencyUpgrades.push({
upgradeType: (key === 'pin') ? 'pin' : 'upgrade',
depType: depType, depType: depType,
depName: depName, depName: depName,
currentVersion: currentVersion, currentVersion: currentVersion,
newVersion: res[key], upgradeType: upgrade.type,
newVersion: upgrade.version,
}); });
}); });
} else { } else {
...@@ -78,11 +78,17 @@ function getDependencyUpgrades(depName, currentVersion) { ...@@ -78,11 +78,17 @@ function getDependencyUpgrades(depName, currentVersion) {
if (!res.body['versions']) { if (!res.body['versions']) {
console.log(depName + ' versions is null'); console.log(depName + ' versions is null');
} }
const allUpgrades = {};
if (isRange(currentVersion)) { if (isRange(currentVersion)) {
// Pin ranges to their maximum satisfying version // 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) { Object.keys(res.body['versions']).forEach(function(version) {
if (stable.is(currentVersion) && !stable.is(version)) { if (stable.is(currentVersion) && !stable.is(version)) {
// Ignore unstable versions, unless the current version is unstable // Ignore unstable versions, unless the current version is unstable
...@@ -91,12 +97,20 @@ function getDependencyUpgrades(depName, currentVersion) { ...@@ -91,12 +97,20 @@ function getDependencyUpgrades(depName, currentVersion) {
if (semver.gt(version, currentVersion)) { if (semver.gt(version, currentVersion)) {
// Group by major versions // Group by major versions
var thisMajor = semver.major(version); var thisMajor = semver.major(version);
if (!allUpgrades[thisMajor] || semver.gt(version, allUpgrades[thisMajor])) { if (!allUpgrades[thisMajor] || semver.gt(version, allUpgrades[thisMajor].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]);
}); });
} }
......
...@@ -65,21 +65,22 @@ function processUpgradesSequentially(upgrades) { ...@@ -65,21 +65,22 @@ function processUpgradesSequentially(upgrades) {
// 2. Edge case collision of branch name, e.g. dependency also listed as dev dependency // 2. Edge case collision of branch name, e.g. dependency also listed as dev dependency
return upgrades.reduce((promise, upgrade) => { return upgrades.reduce((promise, upgrade) => {
return promise.then(() => { return promise.then(() => {
return updateDependency(upgrade.upgradeType, upgrade.depType, upgrade.depName, upgrade.currentVersion, upgrade.newVersion); return updateDependency(upgrade);
}); });
}, Promise.resolve()); }, Promise.resolve());
} }
function updateDependency(upgradeType, depType, depName, currentVersion, newVersion) { function updateDependency({ upgradeType, depType, depName, currentVersion, newVersion }) {
const newVersionMajor = semver.major(newVersion); const newVersionMajor = semver.major(newVersion);
const branchName = config.templates.branchName({depType, depName, currentVersion, newVersion, newVersionMajor}); const branchName = config.templates.branchName({depType, depName, currentVersion, newVersion, newVersionMajor});
let prTitle = ''; let prTitle = '';
if (upgradeType === 'pin') { if (upgradeType === 'pin') {
prTitle = config.templates.prTitlePin({ depType, depName, currentVersion, newVersion, newVersionMajor }); prTitle = config.templates.prTitlePin({ depType, depName, currentVersion, newVersion, newVersionMajor });
} else if (newVersionMajor > semver.major(currentVersion)) { } else if (upgradeType === 'minor') {
prTitle = config.templates.prTitleMajor({ depType, depName, currentVersion, newVersion, newVersionMajor }); // Use same title for range or minor
} else {
prTitle = config.templates.prTitleMinor({ depType, depName, currentVersion, newVersion, newVersionMajor }); 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 prBody = config.templates.prBody({ depName, currentVersion, newVersion });
const commitMessage = config.templates.commitMessage({ depName, currentVersion, newVersion }); const commitMessage = config.templates.commitMessage({ depName, currentVersion, newVersion });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment