Skip to content

Commit

Permalink
address review issue: reduce duplication in findUp and findUpSync
Browse files Browse the repository at this point in the history
  • Loading branch information
vast committed Oct 5, 2021
1 parent 1e4754e commit 1134d6d
Showing 1 changed file with 16 additions and 81 deletions.
97 changes: 16 additions & 81 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,11 @@ import {locatePath, locatePathSync} from 'locate-path';

export const findUpStop = Symbol('findUpStop');

export async function findUp(name, options = {}) {
let directory = path.resolve(options.cwd || '');
const {root} = path.parse(directory);
const stopAt = path.resolve(directory, options.stopAt || root);
const paths = [name].flat();

const runMatcher = async locateOptions => {
if (typeof name !== 'function') {
return locatePath(paths, locateOptions);
}

const foundPath = await name(locateOptions.cwd);
if (typeof foundPath === 'string') {
return locatePath([foundPath], locateOptions);
}

return foundPath;
};

// eslint-disable-next-line no-constant-condition
while (true) {
// eslint-disable-next-line no-await-in-loop
const foundPath = await runMatcher({...options, cwd: directory});

if (foundPath === findUpStop) {
return;
}

if (foundPath) {
return path.resolve(directory, foundPath);
}

if (directory === stopAt) {
return;
}

directory = path.dirname(directory);
}
}

export async function findUpMultiple(name, options = {}) {
let directory = path.resolve(options.cwd || '');
const {root} = path.parse(directory);
const stopAt = path.resolve(directory, options.stopAt || root);
const limit = options.limit || Number.POSITIVE_INFINITY;
const paths = [name].flat();

const runMatcher = async locateOptions => {
Expand Down Expand Up @@ -76,7 +37,7 @@ export async function findUpMultiple(name, options = {}) {
matches.push(path.resolve(directory, foundPath));
}

if (directory === stopAt) {
if (directory === stopAt || matches.length >= limit) {
break;
}

Expand All @@ -86,49 +47,11 @@ export async function findUpMultiple(name, options = {}) {
return matches;
}

export function findUpSync(name, options = {}) {
let directory = path.resolve(options.cwd || '');
const {root} = path.parse(directory);
const stopAt = options.stopAt || root;
const paths = [name].flat();

const runMatcher = locateOptions => {
if (typeof name !== 'function') {
return locatePathSync(paths, locateOptions);
}

const foundPath = name(locateOptions.cwd);
if (typeof foundPath === 'string') {
return locatePathSync([foundPath], locateOptions);
}

return foundPath;
};

// eslint-disable-next-line no-constant-condition
while (true) {
const foundPath = runMatcher({...options, cwd: directory});

if (foundPath === findUpStop) {
return;
}

if (foundPath) {
return path.resolve(directory, foundPath);
}

if (directory === stopAt) {
return;
}

directory = path.dirname(directory);
}
}

export function findUpMultipleSync(name, options = {}) {
let directory = path.resolve(options.cwd || '');
const {root} = path.parse(directory);
const stopAt = options.stopAt || root;
const limit = options.limit || Number.POSITIVE_INFINITY;
const paths = [name].flat();

const runMatcher = locateOptions => {
Expand Down Expand Up @@ -157,7 +80,7 @@ export function findUpMultipleSync(name, options = {}) {
matches.push(path.resolve(directory, foundPath));
}

if (directory === stopAt) {
if (directory === stopAt || matches.length >= limit) {
break;
}

Expand All @@ -167,6 +90,18 @@ export function findUpMultipleSync(name, options = {}) {
return matches;
}

export async function findUp(name, options = {}) {
const matches = await findUpMultiple(name, {...options, limit: 1});

return matches[0];
}

export function findUpSync(name, options = {}) {
const matches = findUpMultipleSync(name, {...options, limit: 1});

return matches[0];
}

export {
pathExists,
pathExistsSync,
Expand Down

0 comments on commit 1134d6d

Please sign in to comment.