resolve.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. // Dependencies
  3. var path = require('path');
  4. // Load global paths
  5. var globalPaths = require('module').globalPaths;
  6. // Guess at NPM's global install dir
  7. var npmGlobalPrefix;
  8. if ('win32' === process.platform) {
  9. npmGlobalPrefix = path.dirname(process.execPath);
  10. } else {
  11. npmGlobalPrefix = path.dirname(path.dirname(process.execPath));
  12. }
  13. var npmGlobalModuleDir = path.resolve(npmGlobalPrefix, 'lib', 'node_modules');
  14. // Save OS-specific path separator
  15. var sep = path.sep;
  16. // If we're in webpack, force it to use the original require() method
  17. var requireFunction = ("function" === typeof __webpack_require__ || "function" === typeof __non_webpack_require__)
  18. ? __non_webpack_require__
  19. : require;
  20. // Resolver
  21. module.exports = function resolve(dirname) {
  22. // Check for environmental variable
  23. if (process.env.APP_ROOT_PATH) {
  24. return path.resolve(process.env.APP_ROOT_PATH);
  25. }
  26. // Defer to Yarn Plug'n'Play if enabled
  27. if (process.versions.pnp) {
  28. try {
  29. var pnp = requireFunction('pnpapi');
  30. return pnp.getPackageInformation(pnp.topLevel).packageLocation;
  31. } catch (e) {}
  32. }
  33. // Defer to main process in electron renderer
  34. if ('undefined' !== typeof window && window.process && 'renderer' === window.process.type) {
  35. try {
  36. var remote = requireFunction('electron').remote;
  37. return remote.require('app-root-path').path;
  38. } catch (e) {}
  39. }
  40. // Defer to AWS Lambda when executing there
  41. if (process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV) {
  42. return process.env.LAMBDA_TASK_ROOT;
  43. }
  44. var resolved = path.resolve(dirname);
  45. var alternateMethod = false;
  46. var appRootPath = null;
  47. // Make sure that we're not loaded from a global include path
  48. // Eg. $HOME/.node_modules
  49. // $HOME/.node_libraries
  50. // $PREFIX/lib/node
  51. globalPaths.forEach(function(globalPath) {
  52. if (!alternateMethod && 0 === resolved.indexOf(globalPath)) {
  53. alternateMethod = true;
  54. }
  55. });
  56. // If the app-root-path library isn't loaded globally,
  57. // and node_modules exists in the path, just split __dirname
  58. var nodeModulesDir = sep + 'node_modules';
  59. if (!alternateMethod && -1 !== resolved.indexOf(nodeModulesDir)) {
  60. var parts = resolved.split(nodeModulesDir);
  61. if (parts.length) {
  62. appRootPath = parts[0];
  63. parts = null;
  64. }
  65. }
  66. // If the above didn't work, or this module is loaded globally, then
  67. // resort to require.main.filename (See http://nodejs.org/api/modules.html)
  68. if (alternateMethod || null == appRootPath) {
  69. appRootPath = path.dirname(require.main.filename);
  70. }
  71. // Handle global bin/ directory edge-case
  72. if (alternateMethod && -1 !== appRootPath.indexOf(npmGlobalModuleDir) && (appRootPath.length - 4) === appRootPath.indexOf(sep + 'bin')) {
  73. appRootPath = appRootPath.slice(0, -4);
  74. }
  75. // Return
  76. return appRootPath;
  77. };