Closed brentonhouse closed 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
.
Yes, we need this. I'm thinking: {type: 'directory'}
and {type: 'file'}
and defaulting to file
. Unless someone has a better suggestion?
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.
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
).
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
.
@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.
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. 👍
@issuehunt has funded $40.00 to this issue.
I'll submit a PR shortly to update package.json, testing and readme here.
@sindresorhus has rewarded $36.00 to @coreyfarrell. See it on IssueHunt
Would be nice to be able to be able to specify in options if you are looking for a file or directory.