mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
package-version-parser package
first version of a package that parses smart package version string
This commit is contained in:
1
packages/package-version-parser/.gitignore
vendored
Normal file
1
packages/package-version-parser/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.build*
|
||||
1
packages/package-version-parser/.npm/package/.gitignore
vendored
Normal file
1
packages/package-version-parser/.npm/package/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules
|
||||
7
packages/package-version-parser/.npm/package/README
Normal file
7
packages/package-version-parser/.npm/package/README
Normal 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.
|
||||
7
packages/package-version-parser/.npm/package/npm-shrinkwrap.json
generated
Normal file
7
packages/package-version-parser/.npm/package/npm-shrinkwrap.json
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"semver": {
|
||||
"version": "2.2.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
39
packages/package-version-parser/package-version-parser.js
Normal file
39
packages/package-version-parser/package-version-parser.js
Normal 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;
|
||||
};
|
||||
|
||||
19
packages/package-version-parser/package.js
Normal file
19
packages/package-version-parser/package.js
Normal 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']);
|
||||
});
|
||||
Reference in New Issue
Block a user