Skip to content

Commit

Permalink
Upgrade prereleases to newer prereleases by default. Fixes #615.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Nov 29, 2019
1 parent 4227b48 commit 1f7312a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/npm-check-updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ function initOptions(options) {
loglevel: options.silent ? 'silent' : options.loglevel,
minimal: options.minimal === undefined ? false : options.minimal,
// default to 0, except when newest or greatest are set
pre: options.pre ? Boolean(Number(options.pre)) : options.newest || options.greatest,
pre: options.pre != null ? Boolean(Number(options.pre)) : options.newest || options.greatest,
// add shortcut for any keys that start with 'json'
json,
// imply upgrade in interactive mode when json is not specified as the output
Expand Down
12 changes: 2 additions & 10 deletions lib/package-managers/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,7 @@ function viewMany(packageName, fields, currentVersion) {
* @returns {Array} An array of versions with the release versions filtered out
*/
function filterOutPrereleaseVersions(versions, pre) {
return pre ? versions : _.filter(versions, version => !isPre(version));
}

/**
* @param {String} version
* @returns {boolean} True if the version is any kind of prerelease: alpha, beta, rc, pre
*/
function isPre(version) {
return versionUtil.getPrecision(version) === 'release';
return pre ? versions : _.filter(versions, version => !versionUtil.isPre(version));
}

/**
Expand Down Expand Up @@ -200,7 +192,7 @@ module.exports = {
// if latest exists and latest is not a prerelease version, return it
// if latest exists and latest is a prerelease version and --pre is specified, return it
// if latest exists and latest not satisfies min version of engines.node
if (latest && (!isPre(latest.version) || options.pre) && doesSatisfyEnginesNode({[latest.version]: latest}, options.enginesNode).length) {
if (latest && (!versionUtil.isPre(latest.version) || options.pre) && doesSatisfyEnginesNode({[latest.version]: latest}, options.enginesNode).length) {
return latest.version;
// if latest is a prerelease version and --pre is not specified, find the next
// version that is not a prerelease
Expand Down
9 changes: 9 additions & 0 deletions lib/version-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ function compareVersions(a, b) {
return semver.gt(a, b) ? 1 : a === b ? 0 : -1;
}

/**
* @param {String} version
* @returns {boolean} True if the version is any kind of prerelease: alpha, beta, rc, pre
*/
function isPre(version) {
return getPrecision(version) === 'release';
}

module.exports = {
compareVersions,
numParts,
Expand All @@ -227,6 +235,7 @@ module.exports = {
getPrecision,
setPrecision,
addWildCard,
isPre,
isWildCard,
isWildPart,
colorizeDiff,
Expand Down
8 changes: 7 additions & 1 deletion lib/versionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,13 @@ function queryVersions(packageMap, options = {}) {
* @returns {Promise}
*/
function getPackageVersionProtected(dep) {
return getPackageVersion(dep, packageMap[dep], options).catch(err => {
return getPackageVersion(dep, packageMap[dep], Object.assign(
options,
// upgrade prereleases to newer prereleases by default
{
pre: options.pre != null ? options.pre : versionUtil.isPre(packageMap[dep])
}
)).catch(err => {
if (err && (err.message || err).toString().match(/E404|ENOTFOUND|404 Not Found/i)) {
return null;
} else {
Expand Down
29 changes: 29 additions & 0 deletions test/test-ncu.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,35 @@ describe('npm-check-updates', function () {
});
});

it('should upgrade prereleases to newer prereleases', () => {

return ncu.run({
packageData: JSON.stringify({
dependencies: {
'ncu-test-alpha-latest': '1.0.0-alpha.1'
}
})
}).then(data => {
return data.should.eql({
'ncu-test-alpha-latest': '1.0.0-alpha.2'
});
});
});

it('should not upgrade prereleases to newer prereleases with --pre 0', () => {

return ncu.run({
pre: false,
packageData: JSON.stringify({
dependencies: {
'ncu-test-alpha-latest': '1.0.0-alpha.1'
}
})
}).then(data => {
return data.should.eql({});
});
});

it('should include -alpha, -beta, -rc with --pre option', () => {

return ncu.run({
Expand Down
14 changes: 14 additions & 0 deletions test/test-version-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,18 @@ describe('version-util', () => {
});
});

describe('isPre', () => {

it('should return false for non-prerelease versions', () => {
versionUtil.isPre('1.0.0').should.be.false;
});

it('should return true for prerelease versions', () => {
versionUtil.isPre('1.0.0-alpha').should.be.true;
versionUtil.isPre('1.0.0-beta').should.be.true;
versionUtil.isPre('1.0.0-rc').should.be.true;
});

});

});

0 comments on commit 1f7312a

Please sign in to comment.