-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor maybe* methods to be more consistent
- Loading branch information
Showing
5 changed files
with
125 additions
and
75 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 |
---|---|---|
|
@@ -127,6 +127,7 @@ function read (name, ver, forceBypass, cb) { | |
var jsonFile = path.join(npm.cache, name, ver, "package", "package.json") | ||
function c (er, data) { | ||
if (data) deprCheck(data) | ||
|
||
return cb(er, data) | ||
} | ||
|
||
|
@@ -140,7 +141,9 @@ function read (name, ver, forceBypass, cb) { | |
er = needVersion(er, data) | ||
if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er) | ||
if (er) return addNamed(name, ver, null, c) | ||
|
||
deprCheck(data) | ||
|
||
c(er, data) | ||
}) | ||
} | ||
|
@@ -251,15 +254,27 @@ function add (args, cb) { | |
// in that case, we will not have a protocol now, but if we | ||
// split and check, we will. | ||
if (!name && !p.protocol) { | ||
if (spec.indexOf("/") !== -1 || | ||
process.platform === "win32" && spec.indexOf("\\") !== -1) { | ||
return maybeFile(spec, p, cb) | ||
} else if (spec.indexOf("@") !== -1) { | ||
return maybeAt(spec, cb) | ||
return maybeFile(spec, cb) | ||
} | ||
else { | ||
switch (p.protocol) { | ||
case "http:": | ||
case "https:": | ||
return addRemoteTarball(spec, null, name, "", cb) | ||
|
||
default: | ||
if (isGitUrl(p)) return addRemoteGit(spec, p, false, cb) | ||
|
||
// if we have a name and a spec, then try name@spec | ||
if (name) { | ||
addNamed(name, spec, null, cb) | ||
} | ||
// if not, then try just spec (which may try name@"" if not found) | ||
else { | ||
addLocal(spec, "", cb) | ||
} | ||
} | ||
} | ||
|
||
add_(name, spec, p, cb) | ||
} | ||
|
||
function unpack (pkg, ver, unpackTarget, dMode, fMode, uid, gid, cb) { | ||
|
@@ -284,26 +299,6 @@ function unpack (pkg, ver, unpackTarget, dMode, fMode, uid, gid, cb) { | |
}) | ||
} | ||
|
||
function add_ (name, spec, p, cb) { | ||
switch (p.protocol) { | ||
case "http:": | ||
case "https:": | ||
return addRemoteTarball(spec, null, name, "", cb) | ||
|
||
default: | ||
if (isGitUrl(p)) | ||
return addRemoteGit(spec, p, false, cb) | ||
|
||
// if we have a name and a spec, then try name@spec | ||
// if not, then try just spec (which may try name@"" if not found) | ||
if (name) { | ||
addNamed(name, spec, null, cb) | ||
} else { | ||
addLocal(spec, "", cb) | ||
} | ||
} | ||
} | ||
|
||
function afterAdd (arg, cb) { return function (er, data) { | ||
if (er || !data || !data.name || !data.version) { | ||
return cb(er, data) | ||
|
@@ -319,29 +314,30 @@ function afterAdd (arg, cb) { return function (er, data) { | |
}) | ||
}} | ||
|
||
function maybeFile (spec, p, cb) { | ||
function maybeFile (spec, cb) { | ||
// split [email protected] only if name is a valid package name, | ||
// don't split in case of "./[email protected]/" (local path) | ||
fs.stat(spec, function (er) { | ||
if (!er) { | ||
// definitely a local thing | ||
addLocal(spec, "", cb) | ||
} else if (er && spec.indexOf("@") !== -1) { | ||
// bar@baz/loofa | ||
maybeAt(spec, cb) | ||
} else { | ||
// Already know it's not a url, so must be local | ||
addLocal(spec, "", cb) | ||
return addLocal(spec, "", cb) | ||
} | ||
|
||
maybeAt(spec, cb) | ||
}) | ||
} | ||
|
||
function maybeAt (spec, cb) { | ||
var tmp = spec.split("@") | ||
|
||
// split [email protected] only if name is a valid package name, | ||
// don't split in case of "./[email protected]/" (local path) | ||
var name = tmp.shift() | ||
spec = tmp.join("@") | ||
return add([name, spec], cb) | ||
if (spec.indexOf("@") !== -1) { | ||
var tmp = spec.split("@") | ||
|
||
var name = tmp.shift() | ||
spec = tmp.join("@") | ||
add([name, spec], cb) | ||
} else { | ||
// already know it's not a url, so must be local | ||
addLocal(spec, "", cb) | ||
} | ||
} | ||
|
||
function needName(er, data) { | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,8 @@ var path = require("path") | |
, locker = require("../utils/locker.js") | ||
, lock = locker.lock | ||
, unlock = locker.unlock | ||
, maybeGithub = require("./maybe-github.js") | ||
, addRemoteTarball = require("./add-remote-tarball.js") | ||
, addRemoteGit = require("./add-remote-git.js") | ||
|
||
|
||
module.exports = addNamed | ||
|
@@ -62,7 +62,7 @@ function addNameTag (name, tag, data, cb_) { | |
// might be username/project | ||
// in that case, try it as a github url. | ||
if (er && tag.split("/").length === 2) { | ||
return maybeGithub(tag, name, er, cb_) | ||
return maybeGithub(tag, er, cb_) | ||
} | ||
return cb_(er, data) | ||
} | ||
|
@@ -239,33 +239,6 @@ function addNameRange (name, range, data, cb) { | |
} | ||
} | ||
|
||
function maybeGithub (p, name, er, cb) { | ||
var u = "git://github.com/" + p | ||
, up = url.parse(u) | ||
log.info("maybeGithub", "Attempting %s from %s", p, u) | ||
|
||
return addRemoteGit(u, up, true, function (er2, data) { | ||
if (er2) { | ||
var upriv = "git+ssh://[email protected]:" + p | ||
, uppriv = url.parse(upriv) | ||
|
||
log.info("maybeGithub", "Attempting %s from %s", p, upriv) | ||
|
||
return addRemoteGit(upriv, uppriv, false, function (er3, data) { | ||
if (er3) return cb(er) | ||
success(upriv, data) | ||
}) | ||
} | ||
success(u, data) | ||
}) | ||
|
||
function success (u, data) { | ||
data._from = u | ||
data._fromGithub = true | ||
return cb(null, data) | ||
} | ||
} | ||
|
||
function installTargetsError (requested, data) { | ||
var targets = Object.keys(data["dist-tags"]).filter(function (f) { | ||
return (data.versions || {}).hasOwnProperty(f) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require("../common-tap.js") | ||
var test = require("tap").test | ||
var npm = require("../../lib/npm.js") | ||
|
||
// this is the narrowest way to replace a function in the module cache | ||
var found = true | ||
var remoteGitPath = require.resolve('../../lib/cache/add-remote-git.js') | ||
require("module")._cache[remoteGitPath] = { | ||
id: remoteGitPath, | ||
exports: function stub(_, error, __, cb) { | ||
if (found) { | ||
cb(null, {}) | ||
} | ||
else { | ||
cb(error) | ||
} | ||
} | ||
} | ||
|
||
// only load maybeGithub now, so it gets the stub from cache | ||
var maybeGithub = require("../../lib/cache/maybe-github.js") | ||
|
||
test("should throw with no parameters", function (t) { | ||
t.plan(1) | ||
|
||
t.throws(function () { | ||
maybeGithub(); | ||
}, "throws when called without parameters") | ||
}) | ||
|
||
test("should throw with wrong parameter types", function (t) { | ||
t.plan(3) | ||
|
||
t.throws(function () { | ||
maybeGithub({}, new Error(), function () {}) | ||
}, "expects only a package name") | ||
|
||
t.throws(function () { | ||
maybeGithub("npm/xxx-noexist", null, function () {}) | ||
}, "expects to be called after a previous check already failed") | ||
|
||
t.throws(function () { | ||
maybeGithub("npm/xxx-noexist", new Error(), "ham") | ||
}, "is always async") | ||
}) | ||
|
||
test("should find an existing package on Github", function (t) { | ||
found = true | ||
npm.load({}, function (error) { | ||
t.notOk(error, "bootstrapping succeeds") | ||
t.doesNotThrow(function () { | ||
maybeGithub("npm/npm", new Error("not on filesystem"), function (error, data) { | ||
t.notOk(error, "no issues in looking things up") | ||
t.ok(data, "received metadata from Github") | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
test("shouldn't find a nonexistent package on Github", function (t) { | ||
found = false | ||
npm.load({}, function () { | ||
t.doesNotThrow(function () { | ||
maybeGithub("npm/xxx-noexist", new Error("not on filesystem"), function (error, data) { | ||
t.equal( | ||
error.message, | ||
"not on filesystem", | ||
"passed through original error message" | ||
) | ||
t.notOk(data, "didn't pass any metadata") | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) |