You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

find-node-directory.js 2.3KB

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