1 | 'use strict'
|
---|
2 |
|
---|
3 | const path = require('path')
|
---|
4 | const log = require('npmlog')
|
---|
5 |
|
---|
6 | function findNodeDirectory (scriptLocation, processObj) {
|
---|
7 | // set dirname and process if not passed in
|
---|
8 | // this facilitates regression tests
|
---|
9 | if (scriptLocation === undefined) {
|
---|
10 | scriptLocation = __dirname
|
---|
11 | }
|
---|
12 | if (processObj === undefined) {
|
---|
13 | processObj = process
|
---|
14 | }
|
---|
15 |
|
---|
16 | // Have a look to see what is above us, to try and work out where we are
|
---|
17 | var npmParentDirectory = path.join(scriptLocation, '../../../..')
|
---|
18 | log.verbose('node-gyp root', 'npm_parent_directory is ' +
|
---|
19 | path.basename(npmParentDirectory))
|
---|
20 | var nodeRootDir = ''
|
---|
21 |
|
---|
22 | log.verbose('node-gyp root', 'Finding node root directory')
|
---|
23 | if (path.basename(npmParentDirectory) === 'deps') {
|
---|
24 | // We are in a build directory where this script lives in
|
---|
25 | // deps/npm/node_modules/node-gyp/lib
|
---|
26 | nodeRootDir = path.join(npmParentDirectory, '..')
|
---|
27 | log.verbose('node-gyp root', 'in build directory, root = ' +
|
---|
28 | nodeRootDir)
|
---|
29 | } else if (path.basename(npmParentDirectory) === 'node_modules') {
|
---|
30 | // We are in a node install directory where this script lives in
|
---|
31 | // lib/node_modules/npm/node_modules/node-gyp/lib or
|
---|
32 | // node_modules/npm/node_modules/node-gyp/lib depending on the
|
---|
33 | // platform
|
---|
34 | if (processObj.platform === 'win32') {
|
---|
35 | nodeRootDir = path.join(npmParentDirectory, '..')
|
---|
36 | } else {
|
---|
37 | nodeRootDir = path.join(npmParentDirectory, '../..')
|
---|
38 | }
|
---|
39 | log.verbose('node-gyp root', 'in install directory, root = ' +
|
---|
40 | nodeRootDir)
|
---|
41 | } else {
|
---|
42 | // We don't know where we are, try working it out from the location
|
---|
43 | // of the node binary
|
---|
44 | var nodeDir = path.dirname(processObj.execPath)
|
---|
45 | var directoryUp = path.basename(nodeDir)
|
---|
46 | if (directoryUp === 'bin') {
|
---|
47 | nodeRootDir = path.join(nodeDir, '..')
|
---|
48 | } else if (directoryUp === 'Release' || directoryUp === 'Debug') {
|
---|
49 | // If we are a recently built node, and the directory structure
|
---|
50 | // is that of a repository. If we are on Windows then we only need
|
---|
51 | // to go one level up, everything else, two
|
---|
52 | if (processObj.platform === 'win32') {
|
---|
53 | nodeRootDir = path.join(nodeDir, '..')
|
---|
54 | } else {
|
---|
55 | nodeRootDir = path.join(nodeDir, '../..')
|
---|
56 | }
|
---|
57 | }
|
---|
58 | // Else return the default blank, "".
|
---|
59 | }
|
---|
60 | return nodeRootDir
|
---|
61 | }
|
---|
62 |
|
---|
63 | module.exports = findNodeDirectory
|
---|