diff options
author | isaacs <i@izs.me> | 2011-05-04 02:12:32 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2011-05-04 02:12:32 +0400 |
commit | 17fcce1d6a9cd0bb02accb7abcb6d8b6f934ab72 (patch) | |
tree | 5f828540846e4a466421e77800d754148a329ba2 | |
parent | b599811a9225b72df322e2112e570aa8b8623c2a (diff) |
Don't crash in ls when given an invalid prefix
Bug reported by trentm
-rw-r--r-- | lib/ls.js | 10 | ||||
-rw-r--r-- | lib/utils/read-installed.js | 25 |
2 files changed, 21 insertions, 14 deletions
@@ -18,11 +18,12 @@ ls.usage = "npm ls" function ls (args, cb) { if (args.length) { - log.warn("ls doesn't take args. Try the 'search' command") + log.warn("ls doesn't take positional args. Try the 'search' command") } var dir = npm.prefix if (npm.config.get("global")) dir = path.resolve(dir, "lib") readInstalled(dir, function (er, data) { + //data.path = dir if (er) return cb(er) var long = npm.config.get("long") var out = makePretty(bfsify(data), long, dir).join("\n") @@ -106,10 +107,11 @@ function makePretty (data, long, dir, prefix, list) { makePretty(depData, long, dir, childPref + c, list) }) if (top && list.length === 1 && !data._id) { - if (!npm.config.get("parseable")) list.push("(empty)") - else if (npm.config.get("long")) list[0] += ":EMPTY" + if (!npm.config.get("parseable")) { + list.push("(empty)") + } else if (npm.config.get("long")) list[0] += ":EMPTY" } - return list.filter(function (l) { return l.trim() }) + return list.filter(function (l) { return l && l.trim() }) } function ugly (data) { diff --git a/lib/utils/read-installed.js b/lib/utils/read-installed.js index 0144e2712..7c829ddee 100644 --- a/lib/utils/read-installed.js +++ b/lib/utils/read-installed.js @@ -120,15 +120,15 @@ function readInstalled_ (folder, parent, name, reqver, depth, maxDepth, cb) { readJson(path.resolve(folder, "package.json"), function (er, data) { obj = data - if (er) { - if (parent) return next(er) - obj = true - } - return next() + if (!parent) obj = obj || true + return next(er) }) fs.lstat(folder, function (er, st) { - if (er) return next(er) + if (er) { + if (!parent) real = true + return next(er) + } fs.realpath(folder, function (er, rp) { //console.error("realpath(%j) = %j", folder, rp) real = rp @@ -138,17 +138,22 @@ function readInstalled_ (folder, parent, name, reqver, depth, maxDepth, cb) { }) var errState = null + , called = false function next (er) { if (errState) return if (er) { - errState = er - return cb(null, []) + if (!parent) obj = true + else { + errState = er + return cb(null, []) + } } //console.error('next', installed, obj && typeof obj, name, real) - if (!installed || !obj || !real) return + if (!installed || !obj || !real || called) return + called = true if (rpSeen[real]) return cb(null, rpSeen[real]) if (obj === true) { - obj = {dependencies:{}} + obj = {dependencies:{}, path:folder} installed.forEach(function (i) { obj.dependencies[i] = "*" }) } if (name && obj.name !== name) obj.invalid = true |