node/deps/npm/lib/config/load-prefix.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-06-06 06:18:15 +08:00
module.exports = loadPrefix
var findPrefix = require("./find-prefix.js")
2014-11-05 07:08:12 +08:00
var path = require("path")
2014-06-06 06:18:15 +08:00
function loadPrefix (cb) {
var cli = this.list[0]
2014-08-01 00:05:30 +08:00
Object.defineProperty(this, "prefix",
{ set : function (prefix) {
var g = this.get("global")
2014-11-05 07:08:12 +08:00
this[g ? "globalPrefix" : "localPrefix"] = prefix
2014-08-01 00:05:30 +08:00
}.bind(this)
, get : function () {
var g = this.get("global")
return g ? this.globalPrefix : this.localPrefix
}.bind(this)
, enumerable : true
})
2014-06-06 06:18:15 +08:00
Object.defineProperty(this, "globalPrefix",
2014-08-01 00:05:30 +08:00
{ set : function (prefix) {
2014-11-05 07:08:12 +08:00
this.set("prefix", prefix)
2014-08-01 00:05:30 +08:00
}.bind(this)
, get : function () {
return path.resolve(this.get("prefix"))
}.bind(this)
2014-06-06 06:18:15 +08:00
, enumerable : true
})
2014-08-01 00:05:30 +08:00
var p
Object.defineProperty(this, "localPrefix",
{ set : function (prefix) { p = prefix },
get : function () { return p }
, enumerable: true })
// try to guess at a good node_modules location.
// If we are *explicitly* given a prefix on the cli, then
// always use that. otherwise, infer local prefix from cwd.
if (Object.prototype.hasOwnProperty.call(cli, "prefix")) {
p = path.resolve(cli.prefix)
process.nextTick(cb)
} else {
findPrefix(process.cwd(), function (er, found) {
p = found
cb(er)
2014-11-05 07:08:12 +08:00
})
2014-06-06 06:18:15 +08:00
}
}