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