Skip to content

Commit

Permalink
Fix --greatest by actually sorting versions. Fixes #535.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Nov 28, 2019
1 parent 0526054 commit 0e368ba
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/package-managers/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function viewMany(packageName, fields, currentVersion) {
* @returns {Array} An array of versions with the release versions filtered out
*/
function filterOutPrereleaseVersions(versions, pre) {
return _.filter(versions, version => pre || !isPre(version));
return pre ? versions : _.filter(versions, version => !isPre(version));
}

/**
Expand Down Expand Up @@ -245,8 +245,9 @@ module.exports = {
.then(versions =>
_.last(filterOutPrereleaseVersions(
doesSatisfyEnginesNode(versions, options.enginesNode),
options.pre
))
options.pre)
.sort(versionUtil.compareVersions)
)
);
},

Expand Down
7 changes: 7 additions & 0 deletions lib/version-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const semverutils = require('semver-utils');
const _ = require('lodash');
const chalk = require('chalk');
const util = require('util');
const semver = require('semver');

const VERSION_BASE_PARTS = ['major', 'minor', 'patch'];
const VERSION_ADDED_PARTS = ['release', 'build'];
Expand Down Expand Up @@ -213,7 +214,13 @@ function findGreatestByLevel(versions, current, level) {
.value();
}

/** Comparator used to sort semver versions */
function compareVersions(a, b) {
return semver.gt(a, b) ? 1 : a === b ? 0 : -1;
}

module.exports = {
compareVersions,
numParts,
stringify,
precisionAdd,
Expand Down
2 changes: 1 addition & 1 deletion test/individual/test-package-managers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('package-managers', () => {
);

it('greatest', () =>
packageManagers.npm.greatest('express', null, {prefix: testDir}).then(parseInt).should.eventually.be.above(1)
packageManagers.npm.greatest('ncu-test-greatest-not-newest', null, {prefix: testDir}).should.eventually.equal('2.0.0-beta')
);

});
Expand Down

0 comments on commit 0e368ba

Please sign in to comment.