(names: string | string[], from: string)
| 17 | * @returns The path to the first match found, or `null` if no match was found. |
| 18 | */ |
| 19 | export async function findUp(names: string | string[], from: string): Promise<string | null> { |
| 20 | const filenames = Array.isArray(names) ? names : [names]; |
| 21 | |
| 22 | let currentDir = resolve(from); |
| 23 | while (true) { |
| 24 | for (const name of filenames) { |
| 25 | const p = join(currentDir, name); |
| 26 | try { |
| 27 | await stat(p); |
| 28 | |
| 29 | return p; |
| 30 | } catch { |
| 31 | // Ignore errors (e.g. file not found). |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | const parentDir = dirname(currentDir); |
| 36 | if (parentDir === currentDir) { |
| 37 | break; |
| 38 | } |
| 39 | |
| 40 | currentDir = parentDir; |
| 41 | } |
| 42 | |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Synchronously find a file or directory by walking up the directory tree. |
no test coverage detected