global-dirs
Advanced tools
Comparing version 1.0.0 to 2.0.0
69
index.js
@@ -7,2 +7,4 @@ 'use strict'; | ||
const isWindows = process.platform === 'win32'; | ||
const readRc = filePath => { | ||
@@ -14,8 +16,28 @@ try { | ||
const defaultNpmPrefix = (() => { | ||
if (process.env.PREFIX) { | ||
return process.env.PREFIX; | ||
const getEnvNpmPrefix = () => { | ||
return Object.keys(process.env).reduce((prefix, name) => { | ||
return (/^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'); | ||
} | ||
if (process.platform === 'win32') { | ||
// Homebrew special case: `$(brew --prefix)/lib/node_modules/npm/npmrc` | ||
if (process.execPath.endsWith('/Cellar/node')) { | ||
const homebrewPrefix = path.dirname(path.dirname(process.execPath)); | ||
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) { | ||
// `c:\node\node.exe` → `prefix=c:\node\` | ||
@@ -25,21 +47,12 @@ return path.dirname(process.execPath); | ||
// Homebrew special case; always assume `/usr/local` | ||
if (process.execPath.startsWith('/usr/local/Cellar/node')) { | ||
return '/usr/local'; | ||
} | ||
// `/usr/local/bin/node` → `prefix=/usr/local` | ||
return path.dirname(path.dirname(process.execPath)); | ||
})(); | ||
}; | ||
const getNpmPrefix = () => { | ||
if (process.env.PREFIX) { | ||
return process.env.PREFIX; | ||
const envPrefix = getEnvNpmPrefix(); | ||
if (envPrefix) { | ||
return envPrefix; | ||
} | ||
const envNpmPrefixes = Object.keys(process.env).filter(key => key.toLowerCase() === 'npm_config_prefix'); | ||
if (envNpmPrefixes.length > 0) { | ||
return process.env[envNpmPrefixes.slice(-1)[0]]; | ||
} | ||
const homePrefix = readRc(path.join(os.homedir(), '.npmrc')); | ||
@@ -50,16 +63,12 @@ if (homePrefix) { | ||
const globalConfigPrefix = readRc(path.resolve(defaultNpmPrefix, 'etc', 'npmrc')); | ||
if (globalConfigPrefix) { | ||
return globalConfigPrefix; | ||
if (process.env.PREFIX) { | ||
return process.env.PREFIX; | ||
} | ||
if (process.platform === 'win32' && process.env.APPDATA) { | ||
// Hardcoded contents of `c:\Program Files\nodejs\node_modules\npm\.npmrc` | ||
const prefix = path.join(process.env.APPDATA, 'npm'); | ||
if (fs.existsSync(prefix)) { | ||
return prefix; | ||
} | ||
const globalPrefix = readRc(getGlobalNpmrc()); | ||
if (globalPrefix) { | ||
return globalPrefix; | ||
} | ||
return defaultNpmPrefix; | ||
return getDefaultNpmPrefix(); | ||
}; | ||
@@ -70,3 +79,3 @@ | ||
const getYarnWindowsDirectory = () => { | ||
if (process.platform === 'win32' && process.env.LOCALAPPDATA) { | ||
if (isWindows && process.env.LOCALAPPDATA) { | ||
const dir = path.join(process.env.LOCALAPPDATA, 'Yarn'); | ||
@@ -107,4 +116,4 @@ if (fs.existsSync(dir)) { | ||
exports.npm.prefix = npmPrefix; | ||
exports.npm.packages = path.join(npmPrefix, process.platform === 'win32' ? 'node_modules' : 'lib/node_modules'); | ||
exports.npm.binaries = process.platform === 'win32' ? npmPrefix : path.join(npmPrefix, 'bin'); | ||
exports.npm.packages = path.join(npmPrefix, isWindows ? 'node_modules' : 'lib/node_modules'); | ||
exports.npm.binaries = isWindows ? npmPrefix : path.join(npmPrefix, 'bin'); | ||
@@ -111,0 +120,0 @@ const yarnPrefix = path.resolve(getYarnPrefix()); |
{ | ||
"name": "global-dirs", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Get the directory of globally installed packages and binaries", | ||
@@ -49,8 +49,8 @@ "license": "MIT", | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"execa": "^1.0.0", | ||
"import-fresh": "^3.0.0", | ||
"tsd": "^0.7.2", | ||
"ava": "^2.4.0", | ||
"execa": "^2.0.4", | ||
"import-fresh": "^3.1.0", | ||
"tsd": "^0.7.4", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -67,4 +67,12 @@ # global-dirs [![Build Status](https://travis-ci.org/sindresorhus/global-dirs.svg?branch=master)](https://travis-ci.org/sindresorhus/global-dirs) | ||
## License | ||
--- | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-global-dirs?utm_source=npm-global-dirs&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
8248
141
78
11