diff options
author | isaacs <i@izs.me> | 2011-02-12 03:16:18 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2011-02-12 03:16:18 +0300 |
commit | d876c4a5827366fdbd39787730abcde36142f7e6 (patch) | |
tree | 58d89c8629773b99f484d954c56ae573128b7760 /lib | |
parent | 5ac308894fb8d55db552748eca3108011edf251e (diff) |
Closes GH-547 Split semver into a separate utility
Diffstat (limited to 'lib')
-rw-r--r-- | lib/activate.js | 2 | ||||
-rw-r--r-- | lib/build.js | 2 | ||||
-rw-r--r-- | lib/deprecate.js | 2 | ||||
-rw-r--r-- | lib/init.js | 2 | ||||
-rw-r--r-- | lib/install.js | 2 | ||||
-rw-r--r-- | lib/link.js | 2 | ||||
-rw-r--r-- | lib/ls.js | 2 | ||||
-rw-r--r-- | lib/outdated.js | 2 | ||||
-rw-r--r-- | lib/rebuild.js | 2 | ||||
-rw-r--r-- | lib/uninstall.js | 2 | ||||
-rw-r--r-- | lib/update-dependents.js | 2 | ||||
-rw-r--r-- | lib/update.js | 1 | ||||
-rw-r--r-- | lib/utils/default-config.js | 4 | ||||
-rw-r--r-- | lib/utils/read-installed.js | 2 | ||||
-rw-r--r-- | lib/utils/read-json.js | 2 | ||||
-rw-r--r-- | lib/utils/registry/unpublish.js | 2 | ||||
-rw-r--r-- | lib/utils/semver.js | 331 | ||||
-rw-r--r-- | lib/version.js | 2 | ||||
-rw-r--r-- | lib/view.js | 2 |
19 files changed, 18 insertions, 350 deletions
diff --git a/lib/activate.js b/lib/activate.js index 275114f0f..feb10e02c 100644 --- a/lib/activate.js +++ b/lib/activate.js @@ -15,7 +15,7 @@ var npm = require("../npm") , link = require("./utils/link") , linkIfExists = link.ifExists , shimIfExists = require("./utils/write-shim").ifExists - , semver = require("./utils/semver") + , semver = require("semver") , asyncMap = require("./utils/async-map") , loadPackageDefaults = require("./utils/load-package-defaults") diff --git a/lib/build.js b/lib/build.js index 4c24bd99f..298b4d172 100644 --- a/lib/build.js +++ b/lib/build.js @@ -14,7 +14,7 @@ var npm = require("../npm") , fetch = require("./utils/fetch") , fs = require("./utils/graceful-fs") , path = require("path") - , semver = require("./utils/semver") + , semver = require("semver") , mkdir = require("./utils/mkdir-p") , lifecycle = require("./utils/lifecycle") , readJson = require("./utils/read-json") diff --git a/lib/deprecate.js b/lib/deprecate.js index bceb95451..3baf77269 100644 --- a/lib/deprecate.js +++ b/lib/deprecate.js @@ -9,7 +9,7 @@ deprecate.completion = function (args, index, cb) { } var registry = require("./utils/registry") - , semver = require("./utils/semver") + , semver = require("semver") , log = require("./utils/log") , asyncMap = require("./utils/async-map") diff --git a/lib/init.js b/lib/init.js index e9d8874f6..6ab1067a6 100644 --- a/lib/init.js +++ b/lib/init.js @@ -9,7 +9,7 @@ var prompt = require("./utils/prompt") , fs = require("./utils/graceful-fs") , promiseChain = require("./utils/promise-chain") , exec = require("./utils/exec") - , semver = require("./utils/semver") + , semver = require("semver") , log = require("./utils/log") , npm = require("../npm") , output = require("./utils/output") diff --git a/lib/install.js b/lib/install.js index cf33a51d3..1392e0a38 100644 --- a/lib/install.js +++ b/lib/install.js @@ -32,7 +32,7 @@ var registry = require("./utils/registry") , npm = require("../npm") , readInstalled = require("./utils/read-installed") , installedPackages - , semver = require("./utils/semver") + , semver = require("semver") , url = require("url") , fetch = require("./utils/fetch") , readJson = require("./utils/read-json") diff --git a/lib/link.js b/lib/link.js index 7c8f4aa21..6a97fcc0a 100644 --- a/lib/link.js +++ b/lib/link.js @@ -10,7 +10,7 @@ var npm = require("../npm") , path = require("path") , crypto , readInstalled = require("./utils/read-installed") - , semver = require("./utils/semver") + , semver = require("semver") , symlink = require("./utils/link") , url = require("url") @@ -6,7 +6,7 @@ module.exports = exports = ls var npm = require("../npm") , readInstalled = require("./utils/read-installed") , registry = require("./utils/registry") - , semver = require("./utils/semver") + , semver = require("semver") , output = require("./utils/output") , log = require("./utils/log") diff --git a/lib/outdated.js b/lib/outdated.js index 89d9a357e..283401742 100644 --- a/lib/outdated.js +++ b/lib/outdated.js @@ -25,7 +25,7 @@ var readInstalled = require("./utils/read-installed") , log = require("./utils/log") , registry = require("./utils/registry") , npm = require("../npm") - , semver = require("./utils/semver") + , semver = require("semver") , lifecycle = require("./utils/lifecycle") , asyncMap = require("./utils/async-map") , output = require("./utils/output") diff --git a/lib/rebuild.js b/lib/rebuild.js index a0d26d95d..774236cf8 100644 --- a/lib/rebuild.js +++ b/lib/rebuild.js @@ -3,7 +3,7 @@ module.exports = rebuild var readInstalled = require("./utils/read-installed") , asyncMap = require("./utils/async-map") - , semver = require("./utils/semver") + , semver = require("semver") , log = require("./utils/log") , path = require("path") , npm = require("../npm") diff --git a/lib/uninstall.js b/lib/uninstall.js index 9e967060f..4834919d2 100644 --- a/lib/uninstall.js +++ b/lib/uninstall.js @@ -21,7 +21,7 @@ var rm = require("./utils/rm-rf") , npm = require("../npm") , chain = require("./utils/chain") , lifecycle = require("./utils/lifecycle") - , semver = require("./utils/semver") + , semver = require("semver") , asyncMap = require("./utils/async-map") , loadPackageDefaults = require("./utils/load-package-defaults") diff --git a/lib/update-dependents.js b/lib/update-dependents.js index a27436d0c..7b14404f2 100644 --- a/lib/update-dependents.js +++ b/lib/update-dependents.js @@ -21,7 +21,7 @@ var readInstalled = require("./utils/read-installed") , path = require("path") , npm = require("../npm") , chain = require("./utils/chain") - , semver = require("./utils/semver") + , semver = require("semver") , link = require("./utils/link") , linkIfExists = link.ifExists , shim = require("./utils/write-shim") diff --git a/lib/update.js b/lib/update.js index f9102416f..5fc7a6b40 100644 --- a/lib/update.js +++ b/lib/update.js @@ -43,7 +43,6 @@ update.completion = function (args, index, cb) { var chain = require("./utils/chain") , log = require("./utils/log") , npm = require("../npm") - , semver = require("./utils/semver") , lifecycle = require("./utils/lifecycle") function update (args, cb) { diff --git a/lib/utils/default-config.js b/lib/utils/default-config.js index 23d5c3698..b2191e080 100644 --- a/lib/utils/default-config.js +++ b/lib/utils/default-config.js @@ -3,7 +3,7 @@ var log = require("./log") , path = require("path") , hasSSL = false , sslWorks = false - , semver = require("./semver") + , semver = require("semver") try { hasSSL = !!(process.binding("crypto") && require("crypto")) @@ -13,7 +13,7 @@ try { // TODO: Change this version when node's SSL client can upload properly. // if (hasSSL) { - sslWorks = require("./semver").gt(process.version, "9999.9999.9999-9999") + sslWorks = semver.gt(process.version, "9999.9999.9999-9999") } if (!process.execPath) { diff --git a/lib/utils/read-installed.js b/lib/utils/read-installed.js index 605c33c5c..d85491c8f 100644 --- a/lib/utils/read-installed.js +++ b/lib/utils/read-installed.js @@ -7,7 +7,7 @@ var npm = require("../../npm") , fs = require("./graceful-fs") , path = require("path") , asyncMap = require("./async-map") - , semver = require("./semver") + , semver = require("semver") module.exports = readInstalled diff --git a/lib/utils/read-json.js b/lib/utils/read-json.js index 2636644d6..96ae19ecb 100644 --- a/lib/utils/read-json.js +++ b/lib/utils/read-json.js @@ -5,7 +5,7 @@ readJson.unParsePeople = unParsePeople readJson.parsePeople = parsePeople var fs = require("./graceful-fs") - , semver = require("./semver") + , semver = require("semver") , path = require("path") , log = require("./log") , npm = require("../../npm") diff --git a/lib/utils/registry/unpublish.js b/lib/utils/registry/unpublish.js index 3250a70ec..9a0363db7 100644 --- a/lib/utils/registry/unpublish.js +++ b/lib/utils/registry/unpublish.js @@ -10,7 +10,7 @@ module.exports = unpublish var request = require("./request") , log = require("../log") , get = require("./get") - , semver = require("../semver") + , semver = require("semver") function unpublish (name, ver, cb) { if (!cb) cb = ver, ver = null diff --git a/lib/utils/semver.js b/lib/utils/semver.js deleted file mode 100644 index 5beb2715d..000000000 --- a/lib/utils/semver.js +++ /dev/null @@ -1,331 +0,0 @@ - -// See http://semver.org/ -// This implementation is a *hair* less strict in that it allows -// v1.2.3 things, and also tags that don't begin with a char. - -var semver = "[v=]*([0-9]+)" // major - + "\\.([0-9]+)" // minor - + "\\.([0-9]+)" // patch - + "(-[0-9]+-?)?" // build - + "([a-zA-Z-][a-zA-Z0-9-\.:]*)?" // tag - , exprComparator = "^((<|>)?=?)("+semver+")$|^$" - , xRange = "((?:<|>)?=?)([0-9]+|x|X)(?:\\.([0-9]+|x|X)(?:\\.([0-9]+|x|X))?)?" - , exprSpermy = "(?:~>?)"+xRange - , expressions = exports.expressions = - { parse : new RegExp("^\\s*"+semver+"\\s*$") - , parsePackage : new RegExp("^\\s*([^\/]+)[-@](" +semver+")\\s*$") - , parseRange : new RegExp( - "^\\s*(" + semver + ")\\s+-\\s+(" + semver + ")\\s*$") - , validComparator : new RegExp("^"+exprComparator+"$") - , parseXRange : new RegExp("^"+xRange+"$") - , parseSpermy : new RegExp("^"+exprSpermy+"$") - } -Object.getOwnPropertyNames(expressions).forEach(function (i) { - exports[i] = function (str) { return (str || "").match(expressions[i]) } -}) - -exports.rangeReplace = ">=$1 <=$7" -exports.clean = clean -exports.compare = compare -exports.satisfies = satisfies -exports.gt = gt -exports.lt = lt -exports.valid = valid -exports.validPackage = validPackage -exports.validRange = validRange -exports.maxSatisfying = maxSatisfying - -function clean (ver) { - v = exports.parse(ver) - if (!v) return v - return [v[1]||'', v[2]||'', v[3]||''].join(".") + (v[4]||'') + (v[5]||'') -} -function valid (version) { - return exports.parse(version) && version.trim().replace(/^[v=]+/, '') -} -function validPackage (version) { - return version.match(expressions.parsePackage) && version.trim() -} - -// range can be one of: -// "1.0.3 - 2.0.0" range, inclusive, like ">=1.0.3 <=2.0.0" -// ">1.0.2" like 1.0.3 - 9999.9999.9999 -// ">=1.0.2" like 1.0.2 - 9999.9999.9999 -// "<2.0.0" like 0.0.0 - 1.9999.9999 -// ">1.0.2 <2.0.0" like 1.0.3 - 1.9999.9999 -var starExpression = /(<|>)?=?\s*\*/g - , starReplace = "" - , compTrimExpression = new RegExp("((<|>)?=?)\\s*("+semver+")", "g") - , compTrimReplace = "$1$3" - -function toComparators (range) { - return range.trim() - .replace(expressions.parseRange, exports.rangeReplace) - .split(/\s+/) - .map(replaceSpermies) - .map(replaceXRanges) - .join(" ") - .replace(compTrimExpression, compTrimReplace) - .replace(starExpression, starReplace) - .split("||") - .map(function (orchunk) { - return orchunk - .trim() - .split(/\s+/) - .filter(function (c) { return c.match(expressions.validComparator) }) - }) - .filter(function (c) { return c.length }) -} - -// "2.x","2.x.x" --> ">=2.0.0 <2.1" -// "2.3.x" --> ">=2.3.0 <2.4.0" -function replaceXRanges (ranges) { - return ranges.split(/\s+/) - .map(replaceXRange) - .join(" ") -} -function replaceXRange (version) { - return version.trim().replace(expressions.parseXRange, - function (v, gtlt, M, m, p) { - var anyX = !M || M.toLowerCase() === "x" - || !m || m.toLowerCase() === "x" - || !p || p.toLowerCase() === "x" - - if (gtlt && anyX) { - // just replace x'es with zeroes - ;(!M || M.toLowerCase() === "x") && (M = 0) - ;(!m || m.toLowerCase() === "x") && (m = 0) - ;(!p || p.toLowerCase() === "x") && (p = 0) - return gtlt + M+"."+m+"."+p - } - - if (!M || M.toLowerCase() === "x") { - return "*" // allow any - } - if (!m || m.toLowerCase() === "x") { - return ">="+M+".0.0 <"+(+M+1)+".0.0" - } - if (!p || p.toLowerCase() === "x") { - return ">="+M+"."+m+".0 <"+M+"."+(+m+1)+".0" - } - return v // impossible? - }) -} - -// ~, ~> --> * (any, kinda silly) -// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0 -// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0 -// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0 -// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0 -// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0 -function replaceSpermies (version) { - return version.trim().replace(expressions.parseSpermy, - function (v, gtlt, M, m, p) { - if (gtlt) throw new Error( - "Using '"+gtlt+"' with ~ makes no sense. Don't do it.") - if (!M || M.toLowerCase() === "x") { - return "*" - } - if (!m || m.toLowerCase() === "x") { - return ">="+M+".0.0 <"+(+M+1)+".0.0" - } - if (!p || p.toLowerCase() === "x") { - return ">="+M+"."+m+".0 <"+M+"."+(+m+1)+".0" - } - return ">="+M+"."+m+"."+p+" <"+M+"."+(+m+1)+".0" - }) -} - -function validRange (range) { - range = range.trim().replace(starExpression, starReplace) - var c = toComparators(range) - return (c.length === 0) - ? null - : c.map(function (c) { return c.join(" ") }).join("||") -} - -// returns the highest satisfying version in the list, or undefined -function maxSatisfying (versions, range) { - return versions - .filter(function (v) { return satisfies(v, range) }) - .sort(compare) - .pop() -} -function satisfies (version, range) { - version = valid(version) - if (!version) return false - range = toComparators(range) - for (var i = 0, l = range.length ; i < l ; i ++) { - var ok = false - for (var j = 0, ll = range[i].length ; j < ll ; j ++) { - var r = range[i][j] - , gtlt = r.charAt(0) === ">" ? gt - : r.charAt(0) === "<" ? lt - : false - , eq = r.charAt(!!gtlt) === "=" - , sub = (!!eq) + (!!gtlt) - if (!gtlt) eq = true - r = r.substr(sub) - r = (r === "") ? r : valid(r) - ok = (r === "") || (eq && r === version) || (gtlt && gtlt(version, r)) - if (!ok) break - } - if (ok) return true - } - return false -} - -// return v1 > v2 ? 1 : -1 -function compare (v1, v2) { - return v1 === v2 ? 0 : gt(v1, v2) ? 1 : -1 -} - -function lt (v1, v2) { return gt(v2, v1) } - -// return v1 > v2 -function num (v) { return parseInt((v||"0").replace(/[^0-9]+/g, ''), 10) } -function gt (v1, v2) { - v1 = exports.parse(v1) - v2 = exports.parse(v2) - if (!v1 || !v2) return false - - for (var i = 1; i < 5; i ++) { - v1[i] = num(v1[i]) - v2[i] = num(v2[i]) - if (v1[i] > v2[i]) return true - else if (v1[i] !== v2[i]) return false - } - // no tag is > than any tag, or use lexicographical order. - var tag1 = v1[5] || "" - , tag2 = v2[5] || "" - return tag2 && (!tag1 || tag1 > tag2) -} - -if (module === require.main) { // tests below -var assert = require("assert") - -; [ ["0.0.0", "0.0.0foo"] - , ["0.0.1", "0.0.0"] - , ["1.0.0", "0.9.9"] - , ["0.10.0", "0.9.0"] - , ["0.99.0", "0.10.0"] - , ["2.0.0", "1.2.3"] - , ["v0.0.0", "0.0.0foo"] - , ["v0.0.1", "0.0.0"] - , ["v1.0.0", "0.9.9"] - , ["v0.10.0", "0.9.0"] - , ["v0.99.0", "0.10.0"] - , ["v2.0.0", "1.2.3"] - , ["0.0.0", "v0.0.0foo"] - , ["0.0.1", "v0.0.0"] - , ["1.0.0", "v0.9.9"] - , ["0.10.0", "v0.9.0"] - , ["0.99.0", "v0.10.0"] - , ["2.0.0", "v1.2.3"] - , ["1.2.3", "1.2.3-asdf"] - , ["1.2.3-4", "1.2.3"] - , ["1.2.3-4-foo", "1.2.3"] - , ["1.2.3-5", "1.2.3-5-foo"] - , ["1.2.3-5", "1.2.3-4"] - ].forEach(function (v) { - assert.ok(gt(v[0], v[1]), "gt('"+v[0]+"', '"+v[1]+"')") - assert.ok(lt(v[1], v[0]), "lt('"+v[1]+"', '"+v[0]+"')") - assert.ok(!gt(v[1], v[0]), "!gt('"+v[1]+"', '"+v[0]+"')") - assert.ok(!lt(v[0], v[1]), "!lt('"+v[0]+"', '"+v[1]+"')") - }) - - -; [ ["1.0.0 - 2.0.0", "1.2.3"] - , ["1.0.0", "1.0.0"] - , [">=*", "0.2.4"] - , ["", "1.0.0"] - , ["*", "1.2.3"] - , ["*", "v1.2.3-foo"] - , [">=1.0.0", "1.0.0"] - , [">=1.0.0", "1.0.1"] - , [">=1.0.0", "1.1.0"] - , [">1.0.0", "1.0.1"] - , [">1.0.0", "1.1.0"] - , ["<=2.0.0", "2.0.0"] - , ["<=2.0.0", "1.9999.9999"] - , ["<=2.0.0", "0.2.9"] - , ["<2.0.0", "1.9999.9999"] - , ["<2.0.0", "0.2.9"] - , [">= 1.0.0", "1.0.0"] - , [">= 1.0.0", "1.0.1"] - , [">= 1.0.0", "1.1.0"] - , ["> 1.0.0", "1.0.1"] - , ["> 1.0.0", "1.1.0"] - , ["<= 2.0.0", "2.0.0"] - , ["<= 2.0.0", "1.9999.9999"] - , ["<= 2.0.0", "0.2.9"] - , ["< 2.0.0", "1.9999.9999"] - , ["<\t2.0.0", "0.2.9"] - , [">=0.1.97", "v0.1.97"] - , [">=0.1.97", "0.1.97"] - , ["0.1.20 || 1.2.4", "1.2.4"] - , [">=0.2.3 || <0.0.1", "0.0.0"] - , [">=0.2.3 || <0.0.1", "0.2.3"] - , [">=0.2.3 || <0.0.1", "0.2.4"] - , ["||", "1.3.4"] - , ["2.x.x", "2.1.3"] - , ["1.2.x", "1.2.3"] - , ["1.2.x || 2.x", "2.1.3"] - , ["1.2.x || 2.x", "1.2.3"] - , ["x", "1.2.3"] - , ["2", "2.1.2"] - , ["2.3", "2.3.1"] - , ["~2.4", "2.4.0"] // >=2.4.0 <2.5.0 - , ["~2.4", "2.4.5"] - , ["~>3.2.1", "3.2.2"] // >=3.2.1 <3.3.0 - , ["~1", "1.2.3"] // >=1.0.0 <2.0.0 - , ["~>1", "1.2.3"] - , ["~1.0", "1.0.2"] // >=1.0.0 <1.1.0 - , ["<1", "1.0.0beta"] - , [">=1", "1.0.0"] - , ["<1.2", "1.1.1"] - ].forEach(function (v) { - assert.ok(satisfies(v[1], v[0]), v[0]+" satisfied by "+v[1]) - }) - - -// negative tests -; [ ["1.0.0 - 2.0.0", "2.2.3"] - , ["1.0.0", "1.0.1"] - , [">=1.0.0", "0.0.0"] - , [">=1.0.0", "0.0.1"] - , [">=1.0.0", "0.1.0"] - , [">1.0.0", "0.0.1"] - , [">1.0.0", "0.1.0"] - , ["<=2.0.0", "3.0.0"] - , ["<=2.0.0", "2.9999.9999"] - , ["<=2.0.0", "2.2.9"] - , ["<2.0.0", "2.9999.9999"] - , ["<2.0.0", "2.2.9"] - , [">=0.1.97", "v0.1.93"] - , [">=0.1.97", "0.1.93"] - , ["0.1.20 || 1.2.4", "1.2.3"] - , [">=0.2.3 || <0.0.1", "0.0.3"] - , [">=0.2.3 || <0.0.1", "0.2.2"] - , ["2.x.x", "1.1.3"] - , ["2.x.x", "3.1.3"] - , ["1.2.x", "1.3.3"] - , ["1.2.x || 2.x", "3.1.3"] - , ["1.2.x || 2.x", "1.1.3"] - , ["2", "1.1.2"] - , ["2.3", "2.4.1"] - , ["~2.4", "2.5.0"] // >=2.4.0 <2.5.0 - , ["~2.4", "2.3.9"] - , ["~>3.2.1", "3.3.2"] // >=3.2.1 <3.3.0 - , ["~>3.2.1", "3.2.0"] // >=3.2.1 <3.3.0 - , ["~1", "0.2.3"] // >=1.0.0 <2.0.0 - , ["~>1", "2.2.3"] - , ["~1.0", "1.1.0"] // >=1.0.0 <1.1.0 - , [">=1", "1.0.0beta"] - , ["<1", "1.0.0"] - , [">=1.2", "1.1.1"] - ].forEach(function (v) { - assert.ok(!satisfies(v[1], v[0]), v[0]+" not satisfied by "+v[1]) - }) - -} diff --git a/lib/version.js b/lib/version.js index a0a088359..c5f9cd024 100644 --- a/lib/version.js +++ b/lib/version.js @@ -4,7 +4,7 @@ module.exports = version var exec = require("./utils/exec") , readJson = require("./utils/read-json") - , semver = require("./utils/semver") + , semver = require("semver") , path = require("path") , fs = require("./utils/graceful-fs") , chain = require("./utils/chain") diff --git a/lib/view.js b/lib/view.js index a1e3de8f1..f435704d3 100644 --- a/lib/view.js +++ b/lib/view.js @@ -38,7 +38,7 @@ var registry = require("./utils/registry") , sys = require("./utils/sys") , output = require("./utils/output") , npm = require("../npm") - , semver = require("./utils/semver") + , semver = require("semver") , readJson = require("./utils/read-json") function view (args, silent, cb) { |