package-version-parser package

first version of a package that parses smart package version string
This commit is contained in:
Slava Kim
2014-03-04 23:08:38 -08:00
parent 53f2b73d6f
commit 468f32bd80
7 changed files with 128 additions and 0 deletions

View File

@@ -0,0 +1 @@
.build*

View File

@@ -0,0 +1 @@
node_modules

View File

@@ -0,0 +1,7 @@
This directory and the files immediately inside it are automatically generated
when you change this package's NPM dependencies. Commit the files in this
directory (npm-shrinkwrap.json, .gitignore, and this README) to source control
so that others run the same versions of sub-dependencies.
You should NOT check in the node_modules directory that Meteor automatically
creates; if you are using git, the .gitignore file tells git to ignore it.

View File

@@ -0,0 +1,7 @@
{
"dependencies": {
"semver": {
"version": "2.2.1"
}
}
}

View File

@@ -0,0 +1,54 @@
var currentTest = null;
var t = function (versionString, expected, descr) {
currentTest.equal(PackageVersion.parse(versionString), expected, descr);
};
var FAIL = function (versionString) {
currentTest.throws(function () {
PackageVersion.parse(versionString);
});
};
Tinytest.add("Smart Package version string parsing - old format", function (test) {
currentTest = test;
t("foo", { name: "foo", version: null, sticky: false });
t("foo-1234", { name: "foo-1234", version: null, sticky: false });
FAIL("my_awesome_InconsitentPackage123");
});
Tinytest.add("Smart Package version string parsing - compatible version", function (test) {
currentTest = test;
t("foo@1.2.3", { name: "foo", version: "1.2.3", sticky: false });
t("foo-1233@1.2.3", { name: "foo-1233", version: "1.2.3", sticky: false });
t("foo-bar@3.2.1", { name: "foo-bar", version: "3.2.1", sticky: false });
t("42@0.2.0", { name: "42", version: "0.2.0", sticky: false });
FAIL("foo@1.2.3.4");
FAIL("foo@1.4");
FAIL("foo@1");
FAIL("foo@");
FAIL("foo@@");
FAIL("foo@x.y.z");
FAIL("foo@<1.2");
FAIL("foo<1.2");
});
Tinytest.add("Smart Package version string parsing - compatible version sticky", function (test) {
currentTest = test;
t("foo@=1.2.3", { name: "foo", version: "1.2.3", sticky: true });
t("foo-bar@=3.2.1", { name: "foo-bar", version: "3.2.1", sticky: true });
t("42@=0.2.0", { name: "42", version: "0.2.0", sticky: true });
FAIL("foo@=1.2.3.4");
FAIL("foo@=1.4");
FAIL("foo@=1");
FAIL("foo@@=");
FAIL("foo@=@");
FAIL("foo@=x.y.z");
FAIL("foo@=<1.2");
FAIL("foo@<=1.2");
FAIL("foo<=1.2");
});

View File

@@ -0,0 +1,39 @@
var semver = Npm.require('semver');
PackageVersion = {};
PackageVersion.parse = function (versionString) {
if (typeof versionString !== "string")
throw new TypeError("versionString must be a string");
var splitted = versionString.split('@');
var versionDesc = { name: "", version: null, sticky: false };
var name = splitted[0];
var version = splitted[1];
if (! /^[a-z0-9-]+$/.test(name) || splitted.length > 2)
throw new Error("Package name must contain lowercase latin letters, digits or dashes");
versionDesc.name = name;
if (splitted.length === 2 && !version)
throw new Error("semver version cannot be empty");
if (version) {
if (version.charAt(0) === '=') {
versionDesc.sticky = true;
version = version.substr(1);
}
// XXX check for a dash in the version in case of foo@1.2.3-rc0
if (! semver.valid(version))
throw new Error(version + " doesn't look like a semver version (e.g. 1.2.0)");
versionDesc.version = version;
}
return versionDesc;
};

View File

@@ -0,0 +1,19 @@
Package.describe({
summary: "Parses Meteor Smart Package version string",
internal: true
});
Npm.depends({
'semver': '2.2.1'
});
Package.on_use(function (api) {
api.export('PackageVersion');
api.add_files([ 'package-version-parser.js' ], ['client', 'server']);
});
Package.on_test(function (api) {
api.use('package-version-parser', ['client', 'server']);
api.use(['tinytest']);
api.add_files('package-version-parser-tests.js', ['client', 'server']);
});