node/deps/npm/lib/config/load-prefix.js
Kat Marchán d3b1c971bc
deps: upgrade npm to 5.6.0
PR-URL: https://github.com/nodejs/node/pull/17777
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
2018-01-19 11:32:08 -05:00

52 lines
1.3 KiB
JavaScript

module.exports = loadPrefix
var findPrefix = require('find-npm-prefix')
var path = require('path')
function loadPrefix (cb) {
var cli = this.list[0]
Object.defineProperty(this, 'prefix',
{
set: function (prefix) {
var g = this.get('global')
this[g ? 'globalPrefix' : 'localPrefix'] = prefix
}.bind(this),
get: function () {
var g = this.get('global')
return g ? this.globalPrefix : this.localPrefix
}.bind(this),
enumerable: true
})
Object.defineProperty(this, 'globalPrefix',
{
set: function (prefix) {
this.set('prefix', prefix)
}.bind(this),
get: function () {
return path.resolve(this.get('prefix'))
}.bind(this),
enumerable: true
})
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()).then((found) => {
p = found
cb()
}, cb)
}
}