1 | const {resolve, dirname} = require('path')
|
---|
2 | const isWindows = require('./is-windows.js')
|
---|
3 | // the path here is relative, even though it does not need to be
|
---|
4 | // in order to make the posix tests pass in windows
|
---|
5 | const nodeGypPath = resolve(__dirname, '../lib/node-gyp-bin')
|
---|
6 |
|
---|
7 | // Windows typically calls its PATH environ 'Path', but this is not
|
---|
8 | // guaranteed, nor is it guaranteed to be the only one. Merge them
|
---|
9 | // all together in the order they appear in the object.
|
---|
10 | const setPATH = (projectPath, env) => {
|
---|
11 | // not require('path').delimiter, because we fake this for testing
|
---|
12 | const delimiter = isWindows ? ';' : ':'
|
---|
13 | const PATH = Object.keys(env).filter(p => /^path$/i.test(p) && env[p])
|
---|
14 | .map(p => env[p].split(delimiter))
|
---|
15 | .reduce((set, p) => set.concat(p.filter(p => !set.includes(p))), [])
|
---|
16 | .join(delimiter)
|
---|
17 |
|
---|
18 | const pathArr = []
|
---|
19 | // unshift the ./node_modules/.bin from every folder
|
---|
20 | // walk up until dirname() does nothing, at the root
|
---|
21 | // XXX should we specify a cwd that we don't go above?
|
---|
22 | let p = projectPath
|
---|
23 | let pp
|
---|
24 | do {
|
---|
25 | pathArr.push(resolve(p, 'node_modules', '.bin'))
|
---|
26 | pp = p
|
---|
27 | p = dirname(p)
|
---|
28 | } while (p !== pp)
|
---|
29 | pathArr.push(nodeGypPath, PATH)
|
---|
30 |
|
---|
31 | const pathVal = pathArr.join(delimiter)
|
---|
32 |
|
---|
33 | // XXX include the node-gyp-bin path somehow? Probably better for
|
---|
34 | // npm or arborist or whoever to just provide that by putting it in
|
---|
35 | // the PATH environ, since that's preserved anyway.
|
---|
36 | for (const key of Object.keys(env)) {
|
---|
37 | if (/^path$/i.test(key))
|
---|
38 | env[key] = pathVal
|
---|
39 | }
|
---|
40 |
|
---|
41 | return env
|
---|
42 | }
|
---|
43 |
|
---|
44 | module.exports = setPATH
|
---|