-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use semver to define which version to load (fix #108)
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
1 parent
3a35d70
commit 4d7ee30
Showing
3 changed files
with
82 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters