| 1 | /*!
|
|---|
| 2 | * global-prefix <https://github.com/jonschlinkert/global-prefix>
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (c) 2015-present Jon Schlinkert.
|
|---|
| 5 | * Licensed under the MIT license.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | 'use strict';
|
|---|
| 9 |
|
|---|
| 10 | const fs = require('fs');
|
|---|
| 11 | const os = require('os');
|
|---|
| 12 | const path = require('path');
|
|---|
| 13 | const ini = require('ini');
|
|---|
| 14 | let prefix;
|
|---|
| 15 |
|
|---|
| 16 | const getPrefix = () => {
|
|---|
| 17 | if (process.env.PREFIX) return process.env.PREFIX;
|
|---|
| 18 | if (prefix) return prefix;
|
|---|
| 19 |
|
|---|
| 20 | // Start by checking if the global prefix is set by the user
|
|---|
| 21 | let home = os.homedir();
|
|---|
| 22 |
|
|---|
| 23 | // os.homedir() returns undefined if $HOME is not set; path.resolve requires strings
|
|---|
| 24 | if (home) {
|
|---|
| 25 | prefix = tryConfigPath(path.resolve(home, '.npmrc'));
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | if (prefix) {
|
|---|
| 29 | return prefix;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | // Otherwise find the path of npm
|
|---|
| 33 | let npm = tryNpmPath();
|
|---|
| 34 | if (npm) {
|
|---|
| 35 | // Check the built-in npm config file
|
|---|
| 36 | prefix = tryConfigPath(path.resolve(npm, '..', '..', 'npmrc'));
|
|---|
| 37 |
|
|---|
| 38 | if (prefix) {
|
|---|
| 39 | // Now the global npm config can also be checked.
|
|---|
| 40 | prefix = tryConfigPath(path.resolve(prefix, 'etc', 'npmrc')) || prefix;
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | if (!prefix) {
|
|---|
| 45 | let { APPDATA, DESTDIR, OSTYPE } = process.env;
|
|---|
| 46 |
|
|---|
| 47 | // c:\node\node.exe --> prefix=c:\node\
|
|---|
| 48 | if (process.platform === 'win32' || OSTYPE === 'msys' || OSTYPE === 'cygwin') {
|
|---|
| 49 | prefix = APPDATA ? path.join(APPDATA, 'npm') : path.dirname(process.execPath);
|
|---|
| 50 | return prefix;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | // /usr/local/bin/node --> prefix=/usr/local
|
|---|
| 54 | prefix = path.dirname(path.dirname(process.execPath));
|
|---|
| 55 |
|
|---|
| 56 | // destdir only is respected on Unix
|
|---|
| 57 | if (DESTDIR) {
|
|---|
| 58 | prefix = path.join(DESTDIR, prefix);
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | return prefix;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | function tryNpmPath() {
|
|---|
| 66 | try {
|
|---|
| 67 | return fs.realpathSync(require('which').sync('npm'));
|
|---|
| 68 | } catch (err) { /* do nothing */ }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | function tryConfigPath(configPath) {
|
|---|
| 72 | try {
|
|---|
| 73 | return ini.parse(fs.readFileSync(configPath, 'utf-8')).prefix;
|
|---|
| 74 | } catch (err) { /* do nothing */ }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Expose `prefix`
|
|---|
| 79 | */
|
|---|
| 80 |
|
|---|
| 81 | Reflect.defineProperty(module, 'exports', {
|
|---|
| 82 | get() {
|
|---|
| 83 | return getPrefix();
|
|---|
| 84 | }
|
|---|
| 85 | });
|
|---|