UNPKG

1.34 kBJavaScriptView Raw
1module.exports = loadPrefix
2
3var findPrefix = require("./find-prefix.js")
4var path = require('path')
5
6function loadPrefix (cb) {
7 var cli = this.list[0]
8
9 Object.defineProperty(this, "prefix",
10 { set : function (prefix) {
11 var g = this.get("global")
12 this[g ? 'globalPrefix' : 'localPrefix'] = prefix
13 }.bind(this)
14 , get : function () {
15 var g = this.get("global")
16 return g ? this.globalPrefix : this.localPrefix
17 }.bind(this)
18 , enumerable : true
19 })
20
21 Object.defineProperty(this, "globalPrefix",
22 { set : function (prefix) {
23 this.set('prefix', prefix)
24 }.bind(this)
25 , get : function () {
26 return path.resolve(this.get("prefix"))
27 }.bind(this)
28 , enumerable : true
29 })
30
31 var p
32 Object.defineProperty(this, "localPrefix",
33 { set : function (prefix) { p = prefix },
34 get : function () { return p }
35 , enumerable: true })
36
37 // try to guess at a good node_modules location.
38 // If we are *explicitly* given a prefix on the cli, then
39 // always use that. otherwise, infer local prefix from cwd.
40 if (Object.prototype.hasOwnProperty.call(cli, "prefix")) {
41 p = path.resolve(cli.prefix)
42 process.nextTick(cb)
43 } else {
44 findPrefix(process.cwd(), function (er, found) {
45 p = found
46 cb(er)
47 }.bind(this))
48 }
49}