查找起来
通过上行父目录查找文件或目录 .
目录
安装
npm i @dozerg/find-up
APIs 密码
findUp
找出文件并返回承诺 .
findUp(name: string, options?: Options) => Promise<string[]>
import findUp from '@dozerg/find-up';
findUp('package.json').then((results: string[]) => {
if(results.length === 0) {
// No files found.
} else {
// Found 'package.json' files in results.
// The results are in the order they were found,
// i.e. from the deepest to the root.
}
})
// Find the first 'package.json' starting from specific directory.
const [package] = await findUp('package.json', { stopAtLimit: 1, cwd: 'path/to/start' });
findUp(names: string[], options?: Options) => Promise<string[]>
// Find the first supported eslintrc file.
const [eslintrc] = await findUp(['.eslintrc.yaml', '.eslintrc.json'], { stopAtLimit: 1 })
// If both '.eslintrc.yaml' and '.eslintrc.json' are found in a directory,
// '.eslintrc.yaml' will be returned because it's in front of the other in the array.
findUp(matcher: Function, options?: Options) => Promise<string[]>
matcher 类型 (directory: string) => string | undefined。在搜索中,它会和每个目录一起被调用,然后返回一个字符串或 undefined。如果返回字符串,将检查文件路径。 如果字符串是相对的,则文件路径将是 directory 并返回字符串。 如果字符串是绝对的,文件路径将是字符串.
当处于同步模式时, matcher 可能是一种协同或许诺返回的功能.
const matcher = (directory: string) => {
if(condition_1) {
return undefined;
// Nothing to check
} else if (condition_2) {
return '/absolute/path/to/file';
// Will check '/absolute/path/to/file'
} else
return 'relative/path/to/file';
// Will check '${directory}/relative/path/to/file'
}
const results = await findUp(matcher);
findUp.gen
以平稳的方式查找文件并返回一个 Aync 生成器 .
findUp.gen(name: string, options?: Options) => AsyncGenerator<string>
for await (const file of findUp.gen('my.config')) {
// Found 'my.config' file.
}
findUp.gen(names: string[], options?: Options) => AsyncGenerator<string>
for await (const file of findUp.gen(['my.json', 'my.yaml'])) {
// Found either 'my.json' or 'my.yaml' file.
// If both are found in a directory, 'my.json' will be returned
// because it's in front of the other in the array.
}
findUp.gen(matcher: Function, options?: Options) => AsyncGenerator<string>
const matcher = (dir: string) => 'my.config';
for await (const file of findUp.gen(matcher)) {
// Found 'my.config' file.
}
findUp.sync
同步查找文件 .
findUp.sync(name: string, options?: Options) => string[]
// Find all 'package.json' files.
const packages = findUp.sync('package.json');
// Find the first 'package.json' starting from specific directory.
const [package] = findUp.sync('package.json', { stopAtLimit: 1, cwd: 'path/to/start' });
findUp.sync(names: string[], options?: Options) => string[]
// Find the first supported eslintrc file.
const [eslintrc] = findUp.sync(['.eslintrc.yaml', '.eslintrc.json'], { stopAtLimit: 1 })
// If both '.eslintrc.yaml' and '.eslintrc.json' are found in a directory,
// '.eslintrc.yaml' will be returned because it's in front of the other in the array.
findUp.sync(matcher: Function, options?: Options) => string[]
const matcher = (dir: string) => 'my.config';
// Find all 'my.config' files.
const configs = findUp.sync(matcher);
选项
| 名称 | 类型 | 默认 | 说明 |
|---|---|---|---|
| 数据 | string | URL | process.cwd() | 要开始搜索的目录 . |
| 类型 | "file" | "directory" | "file" | 可以匹配的路径类型. |
| 停止链接 | number | Number.POSITIVE_INFINITY | 一旦找到一些结果就停止搜索 . |
| 站点AtPath | string | URL | path.parse(cwd).root | 在到达根之前停止搜索的目录路径 . |
| 允许链接 | boolean | 真实 | 允许符号链接在指向所选路径类型时匹配. |
灵感
非常感谢 寻找 启发了这个项目.
许可证
麻省理工学院 达伊多多@gmail.com