Skip to content

Commit 1378cf9

Browse files
committed
Simplify findUpSync
1 parent 22663ef commit 1378cf9

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ module.exports = {
5454

5555
* #1304 Replace `fast-levenshtein` by `fastest-levenshtein` (@Kocal)
5656

57+
* #1303 Replace `pkg-up` by an inlined solution (@Kocal)
58+
5759
## [v4.6.1](https://github.com/symfony/webpack-encore/releases/tag/v4.6.1)
5860

5961
* #1256 Re-adding node 18 support (@weaverryan)

lib/utils/package-up.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,43 @@
1111

1212
const fs = require('fs');
1313
const path = require('path');
14-
const {fileURLToPath} = require('url');
14+
const { fileURLToPath } = require('url');
1515

1616
/**
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.
1821
*/
1922
module.exports = function({ cwd }) {
20-
return findUpSync('package.json', {cwd, type: 'file'});
21-
}
22-
23+
return findUpSync('package.json', { cwd, type: 'file' });
24+
};
2325

2426
function toPath(urlOrPath) {
2527
return urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
2628
}
2729

2830
/**
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.
3037
*/
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) {
4143
const filePath = path.isAbsolute(name) ? name : path.join(directory, name);
4244

4345
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()) {
4648
return filePath;
4749
}
48-
} catch {}
50+
} catch (e) {}
4951

5052
directory = path.dirname(directory);
5153
}

test/utils/package-up.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
const {resolve: resolvePath} = require('path');
12+
const { resolve: resolvePath } = require('path');
1313
const expect = require('chai').expect;
1414
const packageUp = require('../../lib/utils/package-up');
1515

@@ -31,7 +31,7 @@ describe('package-up', () => {
3131
cwd: resolvePath(__dirname, '../../fixtures/copy'),
3232
expectedPath: resolvePath(__dirname, '../../package.json'),
3333
},
34-
}
34+
};
3535

3636
Object.entries(test).forEach(([description, { cwd, expectedPath }]) => {
3737
it(description, () => {
@@ -42,4 +42,4 @@ describe('package-up', () => {
4242
expect(path).to.equal(expectedPath);
4343
});
4444
});
45-
})
45+
});

0 commit comments

Comments
 (0)