function resolveSourceDir(rootDir, sourceDir) { var srcDir = path.resolve(rootDir, sourceDir); if (fs.existsSync(srcDir)) return srcDir; // Handle module-relative path, e.g. `loopback/common/models` var start = sourceDir.substring(0, 2); if (start !== './' && start !== '..') { // Module.globalPaths is a list of globally configured paths like // [ env.NODE_PATH values, $HOME/.node_modules, etc. ] // Module._nodeModulePaths(rootDir) returns a list of paths like // [ rootDir/node_modules, rootDir/../node_modules, etc. ] var modulePaths = Module.globalPaths .concat(Module._nodeModulePaths(rootDir)); srcDir = modulePaths .map(function(candidateDir) { return path.join(candidateDir, sourceDir); }) .filter(function(candidate) { return fs.existsSync(candidate); }) [0]; if (srcDir) return srcDir; } return undefined; }
function tryResolveAppPath(rootDir, relativePath, resolveOptions) { var fullPath; var start = relativePath.substring(0, 2); /* In order to retain backward compatibility, we need to support * two ways how to treat values that are not relative nor absolute * path (e.g. `relativePath = 'foobar'`) * - `resolveOptions.strict = true` searches in `node_modules` only * - `resolveOptions.strict = false` attempts to resolve the value * as a relative path first before searching `node_modules` */ resolveOptions = resolveOptions || { strict: true }; var isModuleRelative = false; if (relativePath[0] === '/') { fullPath = relativePath; } else if (start === './' || start === '..') { fullPath = path.resolve(rootDir, relativePath); } else if (!resolveOptions.strict) { isModuleRelative = true; fullPath = path.resolve(rootDir, relativePath); } if (fullPath) { // This check is needed to support paths pointing to a directory if (fileExistsSync(fullPath)) { return fullPath; } try { fullPath = require.resolve(fullPath); return fullPath; } catch (err) { if (!isModuleRelative) { debug ('Skipping %s - %s', fullPath, err); return undefined; } } } // Handle module-relative path, e.g. `loopback/common/models` // Module.globalPaths is a list of globally configured paths like // [ env.NODE_PATH values, $HOME/.node_modules, etc. ] // Module._nodeModulePaths(rootDir) returns a list of paths like // [ rootDir/node_modules, rootDir/../node_modules, etc. ] var modulePaths = Module.globalPaths .concat(Module._nodeModulePaths(rootDir)); fullPath = modulePaths .map(function(candidateDir) { var absPath = path.join(candidateDir, relativePath); try { // NOTE(bajtos) We need to create a proper String object here, // otherwise we can't attach additional properties to it /*jshint -W053 */ var filePath = new String(require.resolve(absPath)); filePath.unresolvedPath = absPath; return filePath; } catch (err) { return absPath; } }) .filter(function(candidate) { return fileExistsSync(candidate.toString()); }) [0]; if (fullPath) { if (fullPath.unresolvedPath && resolveOptions.fullResolve === false) return fullPath.unresolvedPath; // Convert String object back to plain string primitive return fullPath.toString(); } debug ('Skipping %s - module not found', fullPath); return undefined; }
function tryResolveAppPath(rootDir, relativePath, resolveOptions) { var fullPath; var start = relativePath.substring(0, 2); /* In order to retain backward compatibility, we need to support * two ways how to treat values that are not relative nor absolute * path (e.g. `relativePath = 'foobar'`) * - `resolveOptions.strict = true` searches in `node_modules` only * - `resolveOptions.strict = false` attempts to resolve the value * as a relative path first before searching `node_modules` */ resolveOptions = resolveOptions || { strict: true }; var isModuleRelative = false; if (relativePath[0] === '/') { fullPath = relativePath; } else if (start === './' || start === '..') { fullPath = path.resolve(rootDir, relativePath); } else if (!resolveOptions.strict) { isModuleRelative = true; fullPath = path.resolve(rootDir, relativePath); } if (fullPath) { // This check is needed to support paths pointing to a directory if (fs.existsSync(fullPath)) { return fullPath; } try { fullPath = require.resolve(fullPath); return fullPath; } catch (err) { if (!isModuleRelative) { debug ('Skipping %s - %s', fullPath, err); return undefined; } } } // Handle module-relative path, e.g. `loopback/common/models` // Module.globalPaths is a list of globally configured paths like // [ env.NODE_PATH values, $HOME/.node_modules, etc. ] // Module._nodeModulePaths(rootDir) returns a list of paths like // [ rootDir/node_modules, rootDir/../node_modules, etc. ] var modulePaths = Module.globalPaths .concat(Module._nodeModulePaths(rootDir)); fullPath = modulePaths .map(function(candidateDir) { try { var filePath = path.join(candidateDir, relativePath); filePath = require.resolve(filePath); return filePath; } catch (err) { return filePath; } }) .filter(function(candidate) { return fs.existsSync(candidate); }) [0]; if (fullPath) return fullPath; debug ('Skipping %s - module not found', fullPath); return undefined; }
//------------------------------------------------------------------------------ // Private //------------------------------------------------------------------------------ const DEFAULT_OPTIONS = { /* * module.paths is an array of paths to search for resolving things relative * to this file. Module.globalPaths contains all of the special Node.js * directories that can also be searched for modules. * * Need to check for existence of module.paths because Jest seems not to * include it. See https://github.com/eslint/eslint/issues/5791. */ lookupPaths: module.paths ? module.paths.concat(Module.globalPaths) : Module.globalPaths.concat() }; /** * Resolves modules based on a set of options. * @param {Object} options The options for resolving modules. * @param {string[]} options.lookupPaths An array of paths to include in the * lookup with the highest priority paths coming first. * @constructor */ function ModuleResolver(options) { options = options || {}; this.options = lodash.assign({}, DEFAULT_OPTIONS, options); }