-
-
Notifications
You must be signed in to change notification settings - Fork 41
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 support for path matcher function #28
Merged
Merged
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
95d4390
Add support for filename matcher function
sholladay 8b80cf1
Improve documentation
sholladay e8072bb
Seperate not found tests
sholladay 44e67bf
Bubble up errors correctly
sholladay 102b688
Fix indentation
sholladay 7a83413
Merge branch 'master' of github.com:sindresorhus/find-up
sholladay 6ad0dd8
Fix lint errors
sholladay ca48c76
Add Symbol for stopping early
sholladay f858bdd
Merge branch 'master' of github.com:sindresorhus/find-up
sholladay eff8479
Use util.promisify instead of pify
sholladay cba6f26
Add documentation and TypeScript types
sholladay af9d361
Tweak documentation
sindresorhus b10d5b2
Simplify examples
sholladay eb97a2f
Merge branch 'master' of github.com:sholladay/find-up
sholladay ae4d8bc
Use unique symbol
sindresorhus bb10d62
Ensure symbol is readonly
sholladay 94fafd1
Add test for finding nested dir at path.basename(cwd) (#1)
coreyfarrell 41b8bdb
More strict TypeScript types
sholladay cf3157c
Update index.d.ts
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import isPathInside from 'is-path-inside'; | ||
import pify from 'pify'; | ||
import tempy from 'tempy'; | ||
import m from '.'; | ||
|
||
|
@@ -244,3 +246,134 @@ test('sync (not found, custom cwd)', t => { | |
|
||
t.is(filePath, null); | ||
}); | ||
|
||
test('async (matcher function)', async t => { | ||
const cwd = process.cwd(); | ||
|
||
t.is(await m(dir => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
t.is(dir, cwd); | ||
return dir; | ||
}), cwd); | ||
|
||
t.is(await m(() => { | ||
return '.'; | ||
}), cwd); | ||
|
||
t.is(await m(async () => { | ||
return 'foo.txt'; | ||
}), path.join(cwd, 'foo.txt')); | ||
sholladay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
t.is(await m(() => { | ||
return '..'; | ||
}), path.join(cwd, '..')); | ||
|
||
t.is(await m(dir => { | ||
return (dir !== cwd) && dir; | ||
}), path.join(cwd, '..')); | ||
|
||
t.is(await m(dir => { | ||
return (dir !== cwd) && 'foo.txt'; | ||
}), path.join(cwd, '..', 'foo.txt')); | ||
}); | ||
|
||
test('async (not found, matcher function)', async t => { | ||
const cwd = process.cwd(); | ||
const {root} = path.parse(cwd); | ||
const visited = new Set(); | ||
t.is(await m(async dir => { | ||
t.is(typeof dir, 'string'); | ||
const stat = await pify(fs.stat)(dir); | ||
t.true(stat.isDirectory()); | ||
t.true((dir === cwd) || isPathInside(cwd, dir)); | ||
t.false(visited.has(dir)); | ||
visited.add(dir); | ||
}), null); | ||
t.true(visited.has(cwd)); | ||
t.true(visited.has(root)); | ||
sindresorhus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
test('async (matcher function throws)', async t => { | ||
const cwd = process.cwd(); | ||
const visited = new Set(); | ||
await t.throwsAsync(m(dir => { | ||
visited.add(dir); | ||
throw new Error('Some sync throw'); | ||
}), { | ||
message: 'Some sync throw' | ||
}); | ||
t.true(visited.has(cwd)); | ||
t.is(visited.size, 1); | ||
}); | ||
|
||
test('async (matcher function rejects)', async t => { | ||
const cwd = process.cwd(); | ||
const visited = new Set(); | ||
await t.throwsAsync(m(async dir => { | ||
visited.add(dir); | ||
throw new Error('Some async rejection'); | ||
}), { | ||
message: 'Some async rejection' | ||
}); | ||
t.true(visited.has(cwd)); | ||
t.is(visited.size, 1); | ||
}); | ||
|
||
test('sync (matcher function)', t => { | ||
const cwd = process.cwd(); | ||
|
||
t.is(m.sync(dir => { | ||
t.is(dir, cwd); | ||
return dir; | ||
}), cwd); | ||
|
||
t.is(m.sync(() => { | ||
return '.'; | ||
}), cwd); | ||
|
||
t.is(m.sync(() => { | ||
return 'foo.txt'; | ||
}), path.join(cwd, 'foo.txt')); | ||
|
||
t.is(m.sync(() => { | ||
return '..'; | ||
}), path.join(cwd, '..')); | ||
|
||
t.is(m.sync(dir => { | ||
return (dir !== cwd) && dir; | ||
}), path.join(cwd, '..')); | ||
|
||
t.is(m.sync(dir => { | ||
return (dir !== cwd) && 'foo.txt'; | ||
}), path.join(cwd, '..', 'foo.txt')); | ||
}); | ||
|
||
test('sync (not found, matcher function)', t => { | ||
const cwd = process.cwd(); | ||
const {root} = path.parse(cwd); | ||
const visited = new Set(); | ||
t.is(m.sync(dir => { | ||
t.is(typeof dir, 'string'); | ||
const stat = fs.statSync(dir); | ||
t.true(stat.isDirectory()); | ||
t.true((dir === cwd) || isPathInside(cwd, dir)); | ||
t.false(visited.has(dir)); | ||
visited.add(dir); | ||
}), null); | ||
t.true(visited.has(cwd)); | ||
t.true(visited.has(root)); | ||
}); | ||
|
||
test('sync (matcher function throws)', t => { | ||
const cwd = process.cwd(); | ||
const visited = new Set(); | ||
t.throws(() => { | ||
m.sync(dir => { | ||
visited.add(dir); | ||
throw new Error('Some problem'); | ||
}); | ||
}, { | ||
message: 'Some problem' | ||
}); | ||
t.true(visited.has(cwd)); | ||
t.is(visited.size, 1); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't mix async and sync. Here, it should have used an async method to check whether it exists. The problem is that
fs.exists
is deprecated, and it's also not async/await friendly. This is why I want to expose some utility methods. To make common operation simple.I'm thinking:
findUp.exists()
findUp.isFile()
findUp.isDirectory()
findUp.sync.exists()
findUp.sync.isFile()
findUp.sync.isDirectory()
I know this seems like a lot, but I want
find-up
to just work for people with minimal mental overhead or boilerplate.Maybe we don't need the
exists
methods as I can't really think of a scenario where you wouldn't care about the type. Can you?The methods could alternatively be passed into the callback as a sort of
context
or something. I think I prefer them to be on thefindUp
object though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe we should have a
type
option that accepts eitherfile
ordirectory
, but defaults tofile
? Hmm, we should probably have that regardless, as currentlyfind-up
could match both directories and files, while most would only want to match file. Usually it's not a problem as people use extensions, but I can imagine a scenario where a user wants to find an extension-less file and instead gets a directory of the same name somewhere else in the hierarchy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I should've used
findUp.sync()
. I think I was trying to avoid that and then I realized:fs.exists()
is deprecated.fs
method.But now
fs.promises
is stable and it makes the examples simpler. How about something like this?That could replace the example below as well.
As for the utility methods, I agree they are useful and I'd also prefer for them to be properties on the
findUp
function. Let's do that. I think we should merge this PR first and then build the utilities on top of this functionality in a separate PR(s), since this is still useful with or without them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that if we implement #33, we don't need the file/directory checks, so we can simply implement two utility methods => #37