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