12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 'use strict';
- const fs = require('fs');
- const os = require('os');
- const path = require('path');
- const ini = require('ini');
- let prefix;
- const getPrefix = () => {
- if (process.env.PREFIX) return process.env.PREFIX;
- if (prefix) return prefix;
-
- let home = os.homedir();
-
- if (home) {
- prefix = tryConfigPath(path.resolve(home, '.npmrc'));
- }
- if (prefix) {
- return prefix;
- }
-
- let npm = tryNpmPath();
- if (npm) {
-
- prefix = tryConfigPath(path.resolve(npm, '..', '..', 'npmrc'));
- if (prefix) {
-
- prefix = tryConfigPath(path.resolve(prefix, 'etc', 'npmrc')) || prefix;
- }
- }
- if (!prefix) {
- let { APPDATA, DESTDIR, OSTYPE } = process.env;
-
- if (process.platform === 'win32' || OSTYPE === 'msys' || OSTYPE === 'cygwin') {
- prefix = APPDATA ? path.join(APPDATA, 'npm') : path.dirname(process.execPath);
- return prefix;
- }
-
- prefix = path.dirname(path.dirname(process.execPath));
-
- if (DESTDIR) {
- prefix = path.join(DESTDIR, prefix);
- }
- }
- return prefix;
- }
- function tryNpmPath() {
- try {
- return fs.realpathSync(require('which').sync('npm'));
- } catch (err) { }
- }
- function tryConfigPath(configPath) {
- try {
- return ini.parse(fs.readFileSync(configPath, 'utf-8')).prefix;
- } catch (err) { }
- }
- Reflect.defineProperty(module, 'exports', {
- get() {
- return getPrefix();
- }
- });
|