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 ability to specify if looking for file or directory, only match files by default #40

Merged
merged 4 commits into from May 7, 2019

Conversation

coreyfarrell
Copy link
Contributor

By default directories will no longer match.

Fixes #33

By default directories will no longer match.

Fixes #33
index.d.ts Outdated
@@ -8,6 +8,22 @@ declare namespace findUp {
readonly cwd?: string;
}

interface FullOptions extends Options {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know TypeScript that well, could this be imported from locate-path (and if it can should it)?

Copy link
Owner

Choose a reason for hiding this comment

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

You can import it at line 1:

import {Options as LocatePathOptions} from 'locate-path';

And then extend it on line 2:

interface Options extends LocatePathOptions {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually Options is for using a matcher function where only cwd is accepted.

For the normal mode technically locatePath.AsyncOptions is supported, though I'm not sure we want TS to allow concurrency and preserveOrder to be used? For now my patch is using locatePath.AsyncOptions but can easily be switched to locatePath.Options.

Also instead of extending the locate-path options I just directly used them since this module does not add any new options.

readme.md Show resolved Hide resolved
@sindresorhus sindresorhus changed the title Add ability to specify if looking for file or directory Add ability to specify if looking for file or directory, only match files by default May 7, 2019
@sindresorhus sindresorhus merged commit da17a22 into sindresorhus:master May 7, 2019
@sindresorhus
Copy link
Owner

Hmm, just started thinking; when using findUp(matcher, [options]), should we support the type option then too? I don't really see any use-case of not knowing whether it's a directory or file up front. It would simplify existence checking as then you only need to check whether it exists, not also that it's the correct type.

// @sholladay

@coreyfarrell coreyfarrell deleted the issue-33 branch May 7, 2019 09:24
@coreyfarrell
Copy link
Contributor Author

I was assuming that the matcher would deal with restricting the type, possibly by wrapping locatePath. Adapted from the test added in #28:

function matchUnicornParentPath(options = {}) {
  return async (cwd) => {
    const hasUnicorns = await locatePath(['unicorns.png'], { ...options, cwd});
    return hasUnicorns && cwd;
  };
}

console.log(await findUp(matchUnicornParentPath({allowSymlinks: false}));

@sindresorhus
Copy link
Owner

But is there any point in that flexibility? I can think of no reason to want either file or directory. Everything would be a lot simpler if the user could just define that upfront in the options like the normal findUp() method.

@coreyfarrell
Copy link
Contributor Author

The normal findUp is adequate for my needs so I can't speak to the need for flexibility but the example matcher I gave is only capable of returning a directory (or undefined). I think it would be convoluted if this matcher failed to work without giving {type: 'directory'} to findUp. So my suggestion is not about flexibility, it's about what is responsible for the type check.

@sholladay
Copy link
Collaborator

sholladay commented May 7, 2019

But is there any point in that flexibility?

Personally, I am fine with it either way. I have never needed the flexibility mentioned above, however my thinking has been that the matcher function is responsible for the implementation details of how to match, hence the name, and in that case, findUp is merely responsible for walking the filesystem. This is how I've been thinking about the separation of responsibility.

That said, I also cannot think of a use case where it would be a problem for type to apply to the result. So I guess let's do it, for the sake of predictability, if nothing else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to specify if looking for file or directory
3 participants