Security update to Drupal 8.4.6
[yaffs-website] / node_modules / global-prefix / index.js
1 /*!
2  * global-prefix <https://github.com/jonschlinkert/global-prefix>
3  *
4  * Copyright (c) 2015 Jon Schlinkert.
5  * Licensed under the MIT license.
6  */
7
8 'use strict';
9
10 var homedir = require('homedir-polyfill');
11 var path = require('path');
12 var ini = require('ini');
13 var fs = require('fs')
14
15 var prefix;
16
17 if (process.env.PREFIX) {
18   prefix = process.env.PREFIX;
19 } else {
20   // Start by checking if the global prefix is set by the user
21   var home = homedir();
22   if (home) {
23     // homedir() returns undefined if $HOME not set; path.resolve requires strings
24     var userConfig = path.resolve(home, '.npmrc');
25     prefix = readPrefix(userConfig);
26   }
27
28   if (!prefix) {
29     // Otherwise find the path of npm
30     var npm = npmPath();
31     if (npm) {
32       // Check the built-in npm config file
33       var builtinConfig = path.resolve(npm, '..', '..', 'npmrc');
34       prefix = readPrefix(builtinConfig);
35
36       if (prefix) {
37         // Now the global npm config can also be checked.
38         var globalConfig = path.resolve(prefix, 'etc', 'npmrc');
39         prefix = readPrefix(globalConfig) || prefix;
40       }
41     }
42
43     if (!prefix) fallback();
44   }
45 }
46
47 function fallback() {
48   var isWindows = require('is-windows');
49   if (isWindows()) {
50     // c:\node\node.exe --> prefix=c:\node\
51     prefix = process.env.APPDATA
52       ? path.join(process.env.APPDATA, 'npm')
53       : path.dirname(process.execPath);
54   } else {
55     // /usr/local/bin/node --> prefix=/usr/local
56     prefix = path.dirname(path.dirname(process.execPath));
57
58     // destdir only is respected on Unix
59     if (process.env.DESTDIR) {
60       prefix = path.join(process.env.DESTDIR, prefix);
61     }
62   }
63 }
64
65 function npmPath() {
66   try {
67     return fs.realpathSync(require('which').sync('npm'));
68   } catch (ex) {
69   }
70   return false;
71 }
72
73 function readPrefix(configPath) {
74   try {
75     var data = fs.readFileSync(configPath, 'utf-8');
76     var config = ini.parse(data);
77     if (config.prefix) return config.prefix;
78   } catch (ex) {
79     // file not found
80   }
81   return false;
82 }
83
84 module.exports = prefix;