aboutsummaryrefslogtreecommitdiff
path: root/node_modules/global-prefix/index.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
commitbbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch)
treec58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/global-prefix/index.js
parent003fb34971cf63466184351b4db5f7c67df4f444 (diff)
update packages
Diffstat (limited to 'node_modules/global-prefix/index.js')
-rw-r--r--node_modules/global-prefix/index.js84
1 files changed, 48 insertions, 36 deletions
diff --git a/node_modules/global-prefix/index.js b/node_modules/global-prefix/index.js
index fa19422ff..cc66d952a 100644
--- a/node_modules/global-prefix/index.js
+++ b/node_modules/global-prefix/index.js
@@ -1,46 +1,52 @@
/*!
* global-prefix <https://github.com/jonschlinkert/global-prefix>
*
- * Copyright (c) 2015 Jon Schlinkert.
+ * Copyright (c) 2015-2017 Jon Schlinkert.
* Licensed under the MIT license.
*/
'use strict';
-var homedir = require('homedir-polyfill');
+var fs = require('fs');
var path = require('path');
+var expand = require('expand-tilde');
+var homedir = require('homedir-polyfill');
var ini = require('ini');
-var fs = require('fs')
-
var prefix;
-if (process.env.PREFIX) {
- prefix = process.env.PREFIX;
-} else {
- // Start by checking if the global prefix is set by the user
- var home = homedir();
- if (home) {
- // homedir() returns undefined if $HOME not set; path.resolve requires strings
- var userConfig = path.resolve(home, '.npmrc');
- prefix = readPrefix(userConfig);
- }
+function getPrefix() {
+ if (process.env.PREFIX) {
+ prefix = process.env.PREFIX;
+ } else {
+ // Start by checking if the global prefix is set by the user
+ var home = homedir();
+ if (home) {
+ // homedir() returns undefined if $HOME not set; path.resolve requires strings
+ var userConfig = path.resolve(home, '.npmrc');
+ prefix = tryConfigPath(userConfig);
+ }
- if (!prefix) {
- // Otherwise find the path of npm
- var npm = npmPath();
- if (npm) {
- // Check the built-in npm config file
- var builtinConfig = path.resolve(npm, '..', '..', 'npmrc');
- prefix = readPrefix(builtinConfig);
+ if (!prefix) {
+ // Otherwise find the path of npm
+ var npm = tryNpmPath();
+ if (npm) {
+ // Check the built-in npm config file
+ var builtinConfig = path.resolve(npm, '..', '..', 'npmrc');
+ prefix = tryConfigPath(builtinConfig);
- if (prefix) {
- // Now the global npm config can also be checked.
- var globalConfig = path.resolve(prefix, 'etc', 'npmrc');
- prefix = readPrefix(globalConfig) || prefix;
+ if (prefix) {
+ // Now the global npm config can also be checked.
+ var globalConfig = path.resolve(prefix, 'etc', 'npmrc');
+ prefix = tryConfigPath(globalConfig) || prefix;
+ }
}
+
+ if (!prefix) fallback();
}
+ }
- if (!prefix) fallback();
+ if (prefix) {
+ return expand(prefix);
}
}
@@ -62,23 +68,29 @@ function fallback() {
}
}
-function npmPath() {
+function tryNpmPath() {
try {
return fs.realpathSync(require('which').sync('npm'));
- } catch (ex) {
- }
- return false;
+ } catch (err) {}
+ return null;
}
-function readPrefix(configPath) {
+function tryConfigPath(configPath) {
try {
var data = fs.readFileSync(configPath, 'utf-8');
var config = ini.parse(data);
if (config.prefix) return config.prefix;
- } catch (ex) {
- // file not found
- }
- return false;
+ } catch (err) {}
+ return null;
}
-module.exports = prefix;
+/**
+ * Expose `prefix`
+ */
+
+Object.defineProperty(module, 'exports', {
+ enumerable: true,
+ get: function() {
+ return prefix || (prefix = getPrefix());
+ }
+});