getModulePath.js 824 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. const configurationError = require('./configurationError');
  3. const globalModules = require('global-modules');
  4. const resolveFrom = require('resolve-from');
  5. /**
  6. * @param {string} basedir
  7. * @param {string} lookup
  8. * @param {string} [cwd]
  9. * @return {string}
  10. */
  11. module.exports = function getModulePath(basedir, lookup, cwd = process.cwd()) {
  12. // 1. Try to resolve from the provided directory
  13. // 2. Try to resolve from `cwd` or `process.cwd()`
  14. // 3. Try to resolve from global `node_modules` directory
  15. let path = resolveFrom.silent(basedir, lookup);
  16. if (!path) {
  17. path = resolveFrom.silent(cwd, lookup);
  18. }
  19. if (!path) {
  20. path = resolveFrom.silent(globalModules, lookup);
  21. }
  22. if (!path) {
  23. throw configurationError(`Could not find "${lookup}". Do you need a \`configBasedir\`?`);
  24. }
  25. return path;
  26. };