node/deps/npm/lib/utils/find-prefix.js
isaacs 054127112c Upgrade npm to 1.1.3
* Update request to support HTTPS-over-HTTP proxy tunneling
* Throw on undefined envs in config settings
* Update which to 1.0.5
* Fix windows UNC busyloop in findPrefix
* Bundle nested bundleDependencies properly
* Alias adduser to add-user
* Doc updates  (Christian Howe, Henrik Hodne, Andrew Lunny)
* ignore logfd/outfd streams in makeEnv() (Rod Vagg)
* shrinkwrap: Behave properly with url-installed deps
* install: Support --save with url install targets
* Support installing naked tars or single-file modules from urls etc.
* init: Don't add engines section
* Don't run make clean on rebuild
* Added missing unicode replacement (atomizer)
2012-03-02 09:31:46 -08:00

54 lines
1.3 KiB
JavaScript

// try to find the most reasonable prefix to use
module.exports = findPrefix
var fs = require("graceful-fs")
, path = require("path")
, npm = require("../npm.js")
function findPrefix (p, cb_) {
function cb (er, p) {
process.nextTick(function () {
cb_(er, p)
})
}
p = path.resolve(p)
if (npm.config.get("global")) return cb(null, p)
// if there's no node_modules folder, then
// walk up until we hopefully find one.
// if none anywhere, then use cwd.
var walkedUp = false
while (path.basename(p) === "node_modules") {
p = path.dirname(p)
walkedUp = true
}
if (walkedUp) return cb(null, p)
findPrefix_(p, p, cb)
}
function findPrefix_ (p, original, cb) {
if (p === "/"
|| (process.platform === "win32" && p.match(/^[a-zA-Z]:(\\|\/)?$/))) {
return cb(null, original)
}
fs.readdir(p, function (er, files) {
// an error right away is a bad sign.
if (er && p === original) return cb(er)
// walked up too high or something.
if (er) return cb(null, original)
if (files.indexOf("node_modules") !== -1
|| files.indexOf("package.json") !== -1) {
return cb(null, p)
}
var d = path.dirname(p)
if (d === p) return cb(null, original)
return findPrefix_(d, original, cb)
})
}