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 #33

Closed
brentonhouse opened this issue Feb 4, 2019 · 11 comments · Fixed by #40
Closed

Add ability to specify if looking for file or directory #33

brentonhouse opened this issue Feb 4, 2019 · 11 comments · Fixed by #40
Labels
enhancement 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted

Comments

@brentonhouse
Copy link

brentonhouse commented Feb 4, 2019

Issuehunt badges

Would be nice to be able to be able to specify in options if you are looking for a file or directory.

coreyfarrell earned $40.00 by resolving this issue!

@sholladay
Copy link
Collaborator

sholladay commented Feb 4, 2019

This should be relatively easy to add and I've wanted it in the past, too. I'm +0.5 on it. Curious to see how Sindre feels about it.

In the meantime, check out my branch that allows you to use a function to inspect filepaths as we walk up the tree: PR #28

Using that branch, you can do something like:

const findUp = require('find-up');
const pathType = require('path-type');

(async () => {
    const distPath = await findUp((dir) => {
        return pathType.dir(path.join('dist')) && 'dist';
    });
})();

... and you can be sure that distPath is a dist directory, not a file named dist.

@sindresorhus
Copy link
Owner

Yes, we need this. I'm thinking: {type: 'directory'} and {type: 'file'} and defaulting to file. Unless someone has a better suggestion?

@coreyfarrell
Copy link
Contributor

Wouldn't this need to be implemented as a locate-path option? Assume someone calls findUp(['name1', 'name2'], {type: 'file', cwd: process.cwd()}), and ./name1 is a directory / name2 is a file. In this case we need to tell locate-path that it should not resolve name1.

One other question, what should happen if a symbolic link matches? Resolve with fs.realpath then retest the result? This could be a problem with using path-type, it might be necessary to check isFile and isSymbolicLink, fs.stat can do this with one promise.

@sindresorhus
Copy link
Owner

Wouldn't this need to be implemented as a locate-path option? Assume someone calls findUp(['name1', 'name2'], {type: 'file', cwd: process.cwd()}), and ./name1 is a directory / name2 is a file.

Yes

One other question, what should happen if a symbolic link matches? Resolve with fs.realpath then retest the result?

Yes, with an option to turn it off (?). (followSymlinks).

@sholladay
Copy link
Collaborator

pathType.isFile(path) and pathType.isDirectory(path) always follow symlinks by nature. I might be misunderstanding the problem you want to solve, but I think as long as path-type considers the given path to match the correct type, then that path should be returned as-is, not its realpath.

@coreyfarrell
Copy link
Contributor

@sholladay my idea is that in some cases callers might not want to ever match symlinks (regardless of what it points to). I've posted sindresorhus/locate-path#4 with a rough implementation plus a couple tests.

@sholladay
Copy link
Collaborator

Okay, thanks, that's helpful. Yeah, it might happen that someone needs it. 99% of the time, though, you can just treat a symlink as the thing that it points to. But I see what you're saying. 👍

@IssueHuntBot
Copy link

@IssueHunt has funded $40.00 to this issue.


@sindresorhus
Copy link
Owner

@coreyfarrell
Copy link
Contributor

I'll submit a PR shortly to update package.json, testing and readme here.

@issuehunt-oss issuehunt-oss bot added 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt and removed 💵 Funded on Issuehunt This issue has been funded on Issuehunt labels May 7, 2019
@IssueHuntBot
Copy link

@sindresorhus has rewarded $36.00 to @coreyfarrell. See it on IssueHunt

  • 💰 Total deposit: $40.00
  • 🎉 Repository reward(0%): $0.00
  • 🔧 Service fee(10%): $4.00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants