move version rationalization to it's own class

This commit is contained in:
Spencer Alger
2015-07-02 10:17:19 -07:00
parent cfe5ef44a2
commit f52a59acc6
4 changed files with 76 additions and 53 deletions

56
scripts/Version.js Normal file
View File

@ -0,0 +1,56 @@
var _ = require('lodash');
var package = require('../package.json');
var branches = package.config.supported_es_branches;
var semver = require('semver');
var maxMinorVersion = function (majorV) {
var versions = branches.map(function (v) { return v + '.0'; });
return new Version(semver.maxSatisfying(versions, '^' + majorV));
};
function Version(v) {
this.version = v;
this.major = semver.major(v);
this.minor = semver.minor(v);
this.patch = semver.patch(v);
}
Version.fromBranch = function (branch) {
var m;
// master === the highest version number
if (branch === 'master') return new Version('2.0.0');
// n.m -> n.m.0
if (m = branch.match(/^\d+\.\d+$/)) return new Version(branch + '.0');
// n.x -> n.(maxVersion + 1).0
if (m = branch.match(/^(\d+)\.x$/i)) return maxMinorVersion(m[1]).increment('minor');
throw new Error('unable to convert branch "' + branch + '" to semver');
};
Version.prototype.increment = function (which) {
return new Version(semver.inc(this.version, which));
};
Version.prototype.satisfies = function (range) {
return semver.satisfies(this.version, range);
};
// merge a list of option objects, each of which has a "version" key dictating
// the range of versions those options should be included in. Options are merged
// in the order of the array
Version.prototype.mergeOpts = function (opts, defaults) {
var self = this;
return opts
.filter(function (rule) {
return self.satisfies(rule.version);
})
.reduce(function (overrides, rule) {
return _.merge(overrides, _.omit(rule, 'version'));
}, defaults ? _.clone(defaults) : {});
};
module.exports = Version;