Skip to content

Commit

Permalink
Simplify findUpSync
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal committed Aug 24, 2024
1 parent 22663ef commit 1378cf9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ module.exports = {

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

* #1303 Replace `pkg-up` by an inlined solution (@Kocal)

## [v4.6.1](https://github.com/symfony/webpack-encore/releases/tag/v4.6.1)

* #1256 Re-adding node 18 support (@weaverryan)
Expand Down
40 changes: 21 additions & 19 deletions lib/utils/package-up.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,43 @@

const fs = require('fs');
const path = require('path');
const {fileURLToPath} = require('url');
const { fileURLToPath } = require('url');

/**
* Inlined version of the package "package-up" (ESM only), which depends on "find-up-simple" (ESM only too).
* Inlined version of the package "package-up" (ESM only).
*
* @param {string} cwd The directory to start searching from.
* @returns {string|undefined} The path to the nearest package.json file or undefined if not found.
*/
module.exports = function({ cwd }) {
return findUpSync('package.json', {cwd, type: 'file'});
}

return findUpSync('package.json', { cwd, type: 'file' });
};

function toPath(urlOrPath) {
return urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
}

/**
* Inlined version of the package "find-up-simple" (ESM only).
* Inlined and simplified version of the package "find-up-simple" (ESM only).
*
* @param {string} name The name of the file to find
* @param {Object} options
* @param {string} options.cwd The directory to start searching from.
* @returns {string|undefined} The path to the file found or undefined if not found.
*/
function findUpSync(name, {
cwd = process.cwd(),
type = 'file',
stopAt,
} = {}) {
let directory = path.resolve(toPath(cwd) ?? '');
const {root} = path.parse(directory);
stopAt = path.resolve(directory, toPath(stopAt) ?? root);

while (directory && directory !== stopAt && directory !== root) {
function findUpSync(name, { cwd = process.cwd() } = {}) {
let directory = path.resolve(toPath(cwd) || '');
const { root } = path.parse(directory);

while (directory && directory !== root) {
const filePath = path.isAbsolute(name) ? name : path.join(directory, name);

try {
const stats = fs.statSync(filePath, {throwIfNoEntry: false});
if ((type === 'file' && stats?.isFile()) || (type === 'directory' && stats?.isDirectory())) {
const stats = fs.statSync(filePath, { throwIfNoEntry: false });
if (stats && stats.isFile()) {
return filePath;
}
} catch {}
} catch (e) {}

directory = path.dirname(directory);
}
Expand Down
6 changes: 3 additions & 3 deletions test/utils/package-up.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

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

Expand All @@ -31,7 +31,7 @@ describe('package-up', () => {
cwd: resolvePath(__dirname, '../../fixtures/copy'),
expectedPath: resolvePath(__dirname, '../../package.json'),
},
}
};

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

0 comments on commit 1378cf9

Please sign in to comment.