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.

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const path = require('path');
  3. const Module = require('module');
  4. const resolveFrom = (fromDir, moduleId, silent) => {
  5. if (typeof fromDir !== 'string') {
  6. throw new TypeError(`Expected \`fromDir\` to be of type \`string\`, got \`${typeof fromDir}\``);
  7. }
  8. if (typeof moduleId !== 'string') {
  9. throw new TypeError(`Expected \`moduleId\` to be of type \`string\`, got \`${typeof moduleId}\``);
  10. }
  11. fromDir = path.resolve(fromDir);
  12. const fromFile = path.join(fromDir, 'noop.js');
  13. const resolveFileName = () => Module._resolveFilename(moduleId, {
  14. id: fromFile,
  15. filename: fromFile,
  16. paths: Module._nodeModulePaths(fromDir)
  17. });
  18. if (silent) {
  19. try {
  20. return resolveFileName();
  21. } catch (err) {
  22. return null;
  23. }
  24. }
  25. return resolveFileName();
  26. };
  27. module.exports = (fromDir, moduleId) => resolveFrom(fromDir, moduleId);
  28. module.exports.silent = (fromDir, moduleId) => resolveFrom(fromDir, moduleId, true);