1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 | const path = require('path')
|
5 | const which = require('which')
|
6 |
|
7 | const findPrefix = require('./find-prefix')
|
8 |
|
9 | const PATH = getPATHKey()
|
10 | const SEPARATOR = getPATHSeparator()
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function getPath (options, fn) {
|
22 | options.cwd = options.cwd || process.cwd()
|
23 | const env = options.env = options.env || process.env
|
24 | let pathArr = getPathArr(options)
|
25 |
|
26 | whichNpm(options, function (err, npmPath) {
|
27 | if (err) return fn(err)
|
28 | findPrefix(options, function (err, prefixPath) {
|
29 | if (!err && prefixPath) {
|
30 |
|
31 | pathArr.unshift(path.join(prefixPath, 'node_modules', '.bin'))
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 | pathArr.unshift(path.join(path.dirname(npmPath), 'node-gyp-bin'))
|
37 | if (env[PATH]) pathArr = pathArr.concat(env[PATH].split(SEPARATOR))
|
38 |
|
39 |
|
40 | pathArr = Array.from(new Set(pathArr))
|
41 |
|
42 | fn(null, pathArr.join(SEPARATOR))
|
43 | })
|
44 | })
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function getPathAsync (options, fn) {
|
52 |
|
53 | if (options instanceof Function) {
|
54 | fn = options
|
55 | options = {}
|
56 | }
|
57 |
|
58 | if (!(fn instanceof Function)) return getPathSync(options)
|
59 | options = options || {}
|
60 | options.isSync = false
|
61 | return getPath(options, fn)
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function getPathSync (options) {
|
69 | options = options || {}
|
70 | options.isSync = true
|
71 | let thePath = null
|
72 |
|
73 |
|
74 | getPath(options, function (err, foundPath) {
|
75 | if (err) throw err
|
76 | thePath = foundPath
|
77 | })
|
78 | return thePath
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | function setPathAsync (options, fn) {
|
91 |
|
92 | if (options instanceof Function) {
|
93 | fn = options
|
94 | options = {}
|
95 | }
|
96 |
|
97 |
|
98 | if (!(fn instanceof Function)) return setPathSync(options)
|
99 |
|
100 | getPathAsync(options, function (err, newPath) {
|
101 | if (err) return fn(err)
|
102 | fn(null, options.env[PATH] = newPath)
|
103 | })
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | function setPathSync (options) {
|
110 | options = options || {}
|
111 | const newPath = getPathSync(options)
|
112 | options.env[PATH] = newPath
|
113 | return newPath
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | function getPathArr (options) {
|
123 | const wd = options.cwd
|
124 | const pathArr = []
|
125 | const p = wd.split(path.sep + 'node_modules' + path.sep)
|
126 | let acc = path.resolve(p.shift())
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | pathArr.unshift(path.dirname(process.execPath))
|
132 |
|
133 | p.forEach(function (pp) {
|
134 | pathArr.unshift(path.join(acc, 'node_modules', '.bin'))
|
135 | acc = path.join(acc, 'node_modules', pp)
|
136 | })
|
137 | pathArr.unshift(path.join(acc, 'node_modules', '.bin'))
|
138 | return pathArr
|
139 | }
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | function whichNpm (options, fn) {
|
146 | const npmCli = options.npm && path.join(options.npm, 'bin', 'npm-cli.js')
|
147 |
|
148 | if (options.isSync) {
|
149 | fn(null, fs.realpathSync(
|
150 | npmCli || which.sync('npm')
|
151 | ))
|
152 | return
|
153 | }
|
154 |
|
155 | if (options.npm) {
|
156 | process.nextTick(function () {
|
157 | fn(null, npmCli)
|
158 | })
|
159 | return
|
160 | }
|
161 |
|
162 | which('npm', function (err, npmPath) {
|
163 | if (err) return fn(err)
|
164 | fs.realpath(npmPath, fn)
|
165 | })
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | function getPATHKey () {
|
173 | let PATH = 'PATH'
|
174 |
|
175 |
|
176 | if (process.platform === 'win32') {
|
177 | PATH = 'Path'
|
178 | Object.keys(process.env).forEach(function (e) {
|
179 | if (e.match(/^PATH$/i)) {
|
180 | PATH = e
|
181 | }
|
182 | })
|
183 | }
|
184 | return PATH
|
185 | }
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | function getPATHSeparator () {
|
191 | return process.platform === 'win32' ? ';' : ':'
|
192 | }
|
193 |
|
194 | module.exports = setPathAsync
|
195 | module.exports.get = getPathAsync
|
196 | module.exports.get.sync = getPathSync
|
197 | module.exports.getSync = getPathSync
|
198 |
|
199 | module.exports.set = setPathAsync
|
200 | module.exports.set.sync = setPathSync
|
201 | module.exports.setSync = setPathSync
|
202 |
|
203 | module.exports.PATH = PATH
|
204 | module.exports.SEPARATOR = SEPARATOR
|