sindresorhus / find-up

Find a file or directory by walking up parent directories
MIT License
582 stars 40 forks source link

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

Closed brentonhouse closed 5 years ago

brentonhouse commented 5 years ago

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 commented 5 years ago

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 https://github.com/sindresorhus/find-up/pull/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 commented 5 years ago

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

coreyfarrell commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@issuehunt has funded $40.00 to this issue.


sindresorhus commented 5 years ago

https://github.com/sindresorhus/locate-path/releases/tag/v5.0.0

coreyfarrell commented 5 years ago

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

IssueHuntBot commented 5 years ago

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