-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
121 lines (96 loc) · 3.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import process from 'node:process';
import path from 'node:path';
import os from 'node:os';
import fs from 'node:fs';
import ini from 'ini';
const isWindows = process.platform === 'win32';
const readRc = filePath => {
try {
return ini.parse(fs.readFileSync(filePath, 'utf8')).prefix;
} catch {}
};
// TODO: Remove the `.reduce` call.
// eslint-disable-next-line unicorn/no-array-reduce
const getEnvNpmPrefix = () => Object.keys(process.env).reduce((prefix, name) => /^npm_config_prefix$/i.test(name) ? process.env[name] : prefix, undefined);
const getGlobalNpmrc = () => {
if (isWindows && process.env.APPDATA) {
// Hardcoded contents of `c:\Program Files\nodejs\node_modules\npm\npmrc`
return path.join(process.env.APPDATA, '/npm/etc/npmrc');
}
// Homebrew special case: `$(brew --prefix)/lib/node_modules/npm/npmrc`
if (process.execPath.includes('/Cellar/node')) {
const homebrewPrefix = process.execPath.slice(0, process.execPath.indexOf('/Cellar/node'));
return path.join(homebrewPrefix, '/lib/node_modules/npm/npmrc');
}
if (process.execPath.endsWith('/bin/node')) {
const installDir = path.dirname(path.dirname(process.execPath));
return path.join(installDir, '/etc/npmrc');
}
};
const getDefaultNpmPrefix = () => {
if (isWindows) {
const {APPDATA} = process.env;
// `c:\node\node.exe` → `prefix=c:\node\`
return APPDATA ? path.join(APPDATA, 'npm') : path.dirname(process.execPath);
}
// `/usr/local/bin/node` → `prefix=/usr/local`
return path.dirname(path.dirname(process.execPath));
};
const getNpmPrefix = () => {
const envPrefix = getEnvNpmPrefix();
if (envPrefix) {
return envPrefix;
}
const homePrefix = readRc(path.join(os.homedir(), '.npmrc'));
if (homePrefix) {
return homePrefix;
}
if (process.env.PREFIX) {
return process.env.PREFIX;
}
const globalPrefix = readRc(getGlobalNpmrc());
if (globalPrefix) {
return globalPrefix;
}
return getDefaultNpmPrefix();
};
const npmPrefix = path.resolve(getNpmPrefix());
const getYarnWindowsDirectory = () => {
if (isWindows && process.env.LOCALAPPDATA) {
const dir = path.join(process.env.LOCALAPPDATA, 'Yarn');
if (fs.existsSync(dir)) {
return dir;
}
}
return false;
};
const getYarnPrefix = () => {
if (process.env.PREFIX) {
return process.env.PREFIX;
}
const windowsPrefix = getYarnWindowsDirectory();
if (windowsPrefix) {
return windowsPrefix;
}
const configPrefix = path.join(os.homedir(), '.config/yarn');
if (fs.existsSync(configPrefix)) {
return configPrefix;
}
const homePrefix = path.join(os.homedir(), '.yarn-config');
if (fs.existsSync(homePrefix)) {
return homePrefix;
}
// Yarn supports the npm conventions but the inverse is not true
return npmPrefix;
};
const globalDirectory = {};
globalDirectory.npm = {};
globalDirectory.npm.prefix = npmPrefix;
globalDirectory.npm.packages = path.join(npmPrefix, isWindows ? 'node_modules' : 'lib/node_modules');
globalDirectory.npm.binaries = isWindows ? npmPrefix : path.join(npmPrefix, 'bin');
const yarnPrefix = path.resolve(getYarnPrefix());
globalDirectory.yarn = {};
globalDirectory.yarn.prefix = yarnPrefix;
globalDirectory.yarn.packages = path.join(yarnPrefix, getYarnWindowsDirectory() ? 'Data/global/node_modules' : 'global/node_modules');
globalDirectory.yarn.binaries = path.join(globalDirectory.yarn.packages, '.bin');
export default globalDirectory;