|
11 | 11 |
|
12 | 12 | const fs = require('fs'); |
13 | 13 | const path = require('path'); |
14 | | -const {fileURLToPath} = require('url'); |
| 14 | +const { fileURLToPath } = require('url'); |
15 | 15 |
|
16 | 16 | /** |
17 | | - * Inlined version of the package "package-up" (ESM only), which depends on "find-up-simple" (ESM only too). |
| 17 | + * Inlined version of the package "package-up" (ESM only). |
| 18 | + * |
| 19 | + * @param {string} cwd The directory to start searching from. |
| 20 | + * @returns {string|undefined} The path to the nearest package.json file or undefined if not found. |
18 | 21 | */ |
19 | 22 | module.exports = function({ cwd }) { |
20 | | - return findUpSync('package.json', {cwd, type: 'file'}); |
21 | | -} |
22 | | - |
| 23 | + return findUpSync('package.json', { cwd, type: 'file' }); |
| 24 | +}; |
23 | 25 |
|
24 | 26 | function toPath(urlOrPath) { |
25 | 27 | return urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; |
26 | 28 | } |
27 | 29 |
|
28 | 30 | /** |
29 | | - * Inlined version of the package "find-up-simple" (ESM only). |
| 31 | + * Inlined and simplified version of the package "find-up-simple" (ESM only). |
| 32 | + * |
| 33 | + * @param {string} name The name of the file to find |
| 34 | + * @param {Object} options |
| 35 | + * @param {string} options.cwd The directory to start searching from. |
| 36 | + * @returns {string|undefined} The path to the file found or undefined if not found. |
30 | 37 | */ |
31 | | -function findUpSync(name, { |
32 | | - cwd = process.cwd(), |
33 | | - type = 'file', |
34 | | - stopAt, |
35 | | -} = {}) { |
36 | | - let directory = path.resolve(toPath(cwd) ?? ''); |
37 | | - const {root} = path.parse(directory); |
38 | | - stopAt = path.resolve(directory, toPath(stopAt) ?? root); |
39 | | - |
40 | | - while (directory && directory !== stopAt && directory !== root) { |
| 38 | +function findUpSync(name, { cwd = process.cwd() } = {}) { |
| 39 | + let directory = path.resolve(toPath(cwd) || ''); |
| 40 | + const { root } = path.parse(directory); |
| 41 | + |
| 42 | + while (directory && directory !== root) { |
41 | 43 | const filePath = path.isAbsolute(name) ? name : path.join(directory, name); |
42 | 44 |
|
43 | 45 | try { |
44 | | - const stats = fs.statSync(filePath, {throwIfNoEntry: false}); |
45 | | - if ((type === 'file' && stats?.isFile()) || (type === 'directory' && stats?.isDirectory())) { |
| 46 | + const stats = fs.statSync(filePath, { throwIfNoEntry: false }); |
| 47 | + if (stats && stats.isFile()) { |
46 | 48 | return filePath; |
47 | 49 | } |
48 | | - } catch {} |
| 50 | + } catch (e) {} |
49 | 51 |
|
50 | 52 | directory = path.dirname(directory); |
51 | 53 | } |
|
0 commit comments