Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stopAt option #54

Merged
merged 6 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const findUpStop = Symbol('findUpStop');
export async function findUp(name, options = {}) {
let directory = path.resolve(options.cwd || '');
const {root} = path.parse(directory);
const stopAt = options.stopAt || root;
const paths = [name].flat();

const runMatcher = async locateOptions => {
Expand Down Expand Up @@ -34,7 +35,7 @@ export async function findUp(name, options = {}) {
return path.resolve(directory, foundPath);
}

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

Expand All @@ -45,6 +46,7 @@ export async function findUp(name, options = {}) {
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 => {
Expand Down Expand Up @@ -72,7 +74,7 @@ export function findUpSync(name, options = {}) {
return path.resolve(directory, foundPath);
}

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

Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ Default: `true`

Allow symbolic links to match if they point to the chosen path type.

##### stopAt

Type: `string`\
Default: `path.parse(cwd).root`

The absolute path to the directory to stop the search before reaching root if there were no matches before the `stopAt` directory.
Copy link
Owner

@sindresorhus sindresorhus Sep 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it have to be an absolute path? I think you can just path.resolve(cwd, stopAt) the input path you get from the user.


### pathExists(path)

Returns a `Promise<boolean>` of whether the path exists.
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ absolute.fixtureDirectory = path.join(
);
absolute.baz = path.join(absolute.fixtureDirectory, name.baz);
absolute.qux = path.join(absolute.fixtureDirectory, name.qux);
absolute.fooDir = path.join(absolute.fixtureDirectory, 'foo');
absolute.barDir = path.join(absolute.fixtureDirectory, 'foo', 'bar');
absolute.fileLink = path.join(absolute.fixtureDirectory, name.fileLink);
absolute.directoryLink = path.join(absolute.fixtureDirectory, name.directoryLink);
Expand Down Expand Up @@ -210,6 +211,24 @@ test('sync (cousin file, custom cwd)', t => {
t.is(foundPath, absolute.baz);
});

test('async (cousin file, custom cwd with stopAt)', async t => {
const foundPath = await findUp(name.baz, {
cwd: relative.barDir,
stopAt: absolute.fooDir,
});

t.is(foundPath, undefined);
});
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

test('sync (cousin file, custom cwd with stopAt)', t => {
const foundPath = findUpSync(name.baz, {
cwd: relative.barDir,
stopAt: absolute.fooDir,
});

t.is(foundPath, undefined);
});

test('async (nested descendant file)', async t => {
const foundPath = await findUp(relative.baz);

Expand Down