Skip to content

Commit

Permalink
Use semver to define which version to load (fix #108)
Browse files Browse the repository at this point in the history
This also:

- dynamically builds the available versions by listing
  the versions directories.
- introduces module.latest, that point to the latest version
  id available

Using "semver" module, this will also allow much advanced loading
syntax (like ranges, carret, star or x…).
  • Loading branch information
yohanboniface committed Sep 14, 2015
1 parent 3a35d70 commit 4d7ee30
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
45 changes: 27 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
var fs = require('fs'),
path = require('path'),
existsSync = require('fs').existsSync || require('path').existsSync;
semver = require('semver'),
existsSync = require('fs').existsSync || path.existsSync;

var versions = [
'2.0.0',
'2.0.1',
'2.0.2',
'2.1.0',
'2.1.1',
'2.2.0',
'2.3.0',
'3.0.0',
'3.0.3',
'3.0.4'
];

module.exports.versions = versions;
var getVersions = function () {
var names = fs.readdirSync('./'), versions = [];
for (var i = 0; i < names.length; i++) {
if(names[i].match(/^\d{1,2}\.\d{1,2}\.\d{1,2}$/)) versions.push(names[i]);
};
return versions;
};
var versions = getVersions();

var getSatisfyingVersion = function (wanted) {
var version = semver.maxSatisfying(versions, wanted), parsed;
if (!version) {
parsed = semver(wanted);
parsed.patch = 'x';
version = semver.maxSatisfying(versions, parsed.format());
}
return version;
};

module.exports.load = function(version) {
if (versions.indexOf(version) <= -1) {
throw new Error("Unknown mapnik-reference version: '" + version + "'");
module.exports.versions = versions;
module.exports.latest = versions[versions.length - 1];
module.exports.load = function(wanted) {
var version = getSatisfyingVersion(wanted);
if (!version) {
throw new Error("Unknown mapnik-reference version: '" + wanted + "'");
}
var ref = require(path.join(__dirname, version, 'reference.json'));
var ds_path = path.join(__dirname, version, 'datasources.json');
if (existsSync(ds_path)) {
ref.datasources = require(ds_path).datasources;
ref.datasources = require(ds_path).datasources;
}
return ref;
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
"url": "git://github.com/mapnik/mapnik-reference.git"
},
"scripts": {
"test": "mocha -R spec --timeout 50000"
"test": "mocha -R spec --timeout 50000"
},
"devDependencies": {
"mocha": "1.x",
"glob":"4.x"
"glob": "4.x"
},
"dependencies": {
"semver": "^5.0.3"
}
}
50 changes: 50 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,53 @@ describe('versions', function() {
});



describe('load', function() {
it('should load requested version if exists', function() {
var spec = mapnikref.load('2.0.2');
assert.equal(spec.version, '2.0.2');
});

it('should load closer patch version if requested patch does not exist', function() {
var spec = mapnikref.load('2.0.12');
assert.equal(spec.version, '2.0.2');
});

it('should support x as patch', function() {
var spec = mapnikref.load('2.0.x');
assert.equal(spec.version, '2.0.2');
});

it('should support x as minor', function() {
var spec = mapnikref.load('2.x.x');
assert.equal(spec.version, '2.3.0');
});

it('should support missing patch', function() {
var spec = mapnikref.load('2.3');
assert.equal(spec.version, '2.3.0');
});

it('should support major only', function() {
var spec = mapnikref.load('2');
assert.equal(spec.version, '2.3.0');
});

it('should support range', function() {
var spec = mapnikref.load('2.x < 3');
assert.equal(spec.version, '2.3.0');
});

it('should support *', function() {
var spec = mapnikref.load('*');
assert.equal(spec.version, mapnikref.latest);
});

it('should throw if requested minor does not exist', function() {
assert.throws(function () {
mapnikref.load('2.12.0');
}, /Unknown mapnik-reference version/);
});
});


0 comments on commit 4d7ee30

Please sign in to comment.