find-node-directory.js 2.3 KB

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