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