Permalink
Browse files

Don't crash in ls when given an invalid prefix

Bug reported by trentm
  • Loading branch information...
1 parent b599811 commit 17fcce1d6a9cd0bb02accb7abcb6d8b6f934ab72 @isaacs isaacs committed May 3, 2011
Showing with 21 additions and 14 deletions.
  1. +6 −4 lib/ls.js
  2. +15 −10 lib/utils/read-installed.js
View
@@ -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) {
@@ -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

0 comments on commit 17fcce1

Please sign in to comment.