Find Up

Find a file or directory by walking up parent directories.
Table of contents
Install
npm i @teamteanpm2024/ea-consectetur-commodi
APIs
findUp
Find file(s) asynchrously and return a promise.
findUp(name: string, options?: Options) => Promise<string[]>
import findUp from '@teamteanpm2024/ea-consectetur-commodi';
findUp('package.json').then((results: string[]) => {
if(results.length === 0) {
} else {
}
})
const [package] = await findUp('package.json', { stopAtLimit: 1, cwd: 'path/to/start' });
findUp(names: string[], options?: Options) => Promise<string[]>
const [eslintrc] = await findUp(['.eslintrc.yaml', '.eslintrc.json'], { stopAtLimit: 1 })
findUp(matcher: Function, options?: Options) => Promise<string[]>
matcher
is of type (directory: string) => string | undefined
. It'll be called with every directory in the search, and return a string or undefined
. If a string is returned, a file path will be checked. If the string is relative, the file path will be the join of directory
and returned string. If the string is absolute, the file path will be the string.
When in async mode, matcher
could be an async or promise-returning function.
const matcher = (directory: string) => {
if(condition_1) {
return undefined;
} else if (condition_2) {
return '/absolute/path/to/file';
} else
return 'relative/path/to/file';
}
const results = await findUp(matcher);
findUp.gen
Find file(s) asynchrously and return an async generator.
findUp.gen(name: string, options?: Options) => AsyncGenerator<string>
for await (const file of findUp.gen('my.config')) {
}
findUp.gen(names: string[], options?: Options) => AsyncGenerator<string>
for await (const file of findUp.gen(['my.json', 'my.yaml'])) {
}
findUp.gen(matcher: Function, options?: Options) => AsyncGenerator<string>
const matcher = (dir: string) => 'my.config';
for await (const file of findUp.gen(matcher)) {
}
findUp.sync
Find file(s) synchrously.
findUp.sync(name: string, options?: Options) => string[]
const packages = findUp.sync('package.json');
const [package] = findUp.sync('package.json', { stopAtLimit: 1, cwd: 'path/to/start' });
findUp.sync(names: string[], options?: Options) => string[]
const [eslintrc] = findUp.sync(['.eslintrc.yaml', '.eslintrc.json'], { stopAtLimit: 1 })
findUp.sync(matcher: Function, options?: Options) => string[]
const matcher = (dir: string) => 'my.config';
const configs = findUp.sync(matcher);
Options
cwd | string | URL | process.cwd() | The directory to start searching from. |
type | "file" | "directory" | "file" | The type of paths that can match. |
stopAtLimit | number | Number.POSITIVE_INFINITY | Stop the search once a number of results have been found. |
stopAtPath | string | URL | path.parse(cwd).root | The path to the directory to stop the search before reaching root. |
allowSymlinks | boolean | true | Allow symlinks to match if they point to the chosen path type. |
Inspiration
Many thanks to find-up from which this project is inspired.
License
MIT © Zhao DAI daidodo@gmail.com