Permalink
Browse files

Close #817 install won't install invalid deps

  • Loading branch information...
1 parent 5a03f8e commit 3efdebc7348d4e049ec8732975a3437b47ce9cff @isaacs isaacs committed Apr 19, 2011
Showing with 52 additions and 22 deletions.
  1. +52 −22 lib/install.js
View
74 lib/install.js
@@ -110,11 +110,16 @@ install.completion = function (opts, cb) {
registry.get("/-/short", function (er, pkgs) {
if (er) return cb()
if (!opts.partialWord) return cb(null, pkgs)
+
var name = opts.partialWord.split("@").shift()
pkgs = pkgs.filter(function (p) {
return p.indexOf(name) === 0
})
- if (pkgs.length !== 1 && opts.partialWord === name) return cb(null, pkgs)
+
+ if (pkgs.length !== 1 && opts.partialWord === name) {
+ return cb(null, pkgs)
+ }
+
registry.get(pkgs[0], function (er, d) {
if (er) return cb()
return cb(null, Object.keys(d["dist-tags"] || {})
@@ -206,6 +211,7 @@ function installManyTop (what, where, previously, explicit, cb) {
, names = explicit
? what.map(function (w) { return w.split(/@/).shift() })
: []
+
fs.readdir(nm, function (er, pkgs) {
if (er) return installMany(what, where, previously, explicit, cb)
pkgs = pkgs.filter(function (p) {
@@ -220,6 +226,7 @@ function installManyTop (what, where, previously, explicit, cb) {
return cb(null, [[data.name, data.version]])
})
}, function (er, packages) {
+ // add all the existing packages to the previously list.
packages.forEach(function (p) {
previously[p[0]] = p[1]
})
@@ -229,36 +236,56 @@ function installManyTop (what, where, previously, explicit, cb) {
}
function installMany (what, where, previously, explicit, cb) {
- log.verbose(what, "into "+where)
- // what is a list of things.
- // resolve each one.
- asyncMap(what, targetResolver(where, previously, explicit)
- ,function (er, targets) {
- if (er) return cb(er)
- // each target will be a data object corresponding
- // to a package, folder, or whatever that is in the cache now.
- var newPrev = Object.create(previously)
- targets.forEach(function (t) {
- newPrev[t.name] = t.version
- })
- log.silly(targets, "resolved")
- targets.filter(function (t) { return t }).forEach(function (t) {
- log.info(t._id, "into "+where)
+ // 'npm install foo' should install the version of foo
+ // that satisfies the dep in the current folder.
+ // This will typically return immediately, since we already read
+ // this file previously, and it'll be cached.
+ readJson(path.resolve(where, "package.json"), function (er, d) {
+ if (er) d = {}
+ else d = d.dependencies
+
+ log.verbose(what, "into "+where)
+ // what is a list of things.
+ // resolve each one.
+ asyncMap(what, targetResolver(where, previously, explicit, d)
+ ,function (er, targets) {
+ if (er) return cb(er)
+ // each target will be a data object corresponding
+ // to a package, folder, or whatever that is in the cache now.
+ var newPrev = Object.create(previously)
+ targets.forEach(function (t) {
+ newPrev[t.name] = t.version
+ })
+ log.silly(targets, "resolved")
+ targets.filter(function (t) { return t }).forEach(function (t) {
+ log.info(t._id, "into "+where)
+ })
+ asyncMap(targets, function (target, cb) {
+ log(target._id, "installOne")
+ installOne(target, where, newPrev, cb)
+ }, cb)
})
- asyncMap(targets, function (target, cb) {
- log(target._id, "installOne")
- installOne(target, where, newPrev, cb)
- }, cb)
})
}
-function targetResolver (where, previously, explicit) {
+function targetResolver (where, previously, explicit, deps) {
var alreadyInstalledManually = explicit ? [] : null
, nm = path.resolve(where, "node_modules")
if (!explicit) fs.readdir(nm, function (er, inst) {
if (er) return alreadyInstalledManually = []
- alreadyInstalledManually = inst
+ asyncMap(inst, function (pkg, cb) {
+ readJson(path.resolve(nm, pkg, "package.json"), function (er, d) {
+ if (er) return cb(null, [])
+ if (semver.satisfies(d.version, deps[d.name] || "*")) {
+ return cb(null, d.name)
+ }
+ return cb(null, [])
+ })
+ }, function (er, inst) {
+ // this is the list of things that are valid and should be ignored.
+ alreadyInstalledManually = inst
+ })
})
var to = 0
@@ -272,6 +299,9 @@ function targetResolver (where, previously, explicit) {
log.verbose("skipping "+what, "already installed in "+where)
return cb(null, [])
}
+ if (deps[what]) {
+ what = what + "@" + deps[what]
+ }
cache.add(what, function (er, data) {
if (!er && data && previously[data.name] === data.version) {
return cb(null, [])

0 comments on commit 3efdebc

Please sign in to comment.