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 b2eecd0 commit c807026
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
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 c807026

Please sign in to comment.