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 |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 10 | * @param {string} [cwd] |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 11 | * @return {string} |
| 12 | */ |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 13 | module.exports = function getModulePath(basedir, lookup, cwd = process.cwd()) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 14 | // 1. Try to resolve from the provided directory |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 15 | // 2. Try to resolve from `cwd` or `process.cwd()` |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 16 | // 3. Try to resolve from global `node_modules` directory |
| 17 | let path = resolveFrom.silent(basedir, lookup); |
| 18 | |
| 19 | if (!path) { |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 20 | path = resolveFrom.silent(cwd, lookup); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | if (!path) { |
| 24 | path = resolveFrom.silent(globalModules, lookup); |
| 25 | } |
| 26 | |
| 27 | if (!path) { |
| 28 | throw configurationError(`Could not find "${lookup}". Do you need a \`configBasedir\`?`); |
| 29 | } |
| 30 | |
| 31 | return path; |
| 32 | }; |