find-up
Advanced tools
Comparing version 6.1.0 to 6.2.0
100
index.d.ts
@@ -119,2 +119,102 @@ /* eslint-disable @typescript-eslint/unified-signatures */ | ||
/** | ||
Find files or directories by walking up parent directories. | ||
@param name - The name of the file or directory to find. Can be multiple. | ||
@returns All paths found (by respecting the order of `name`s) or an empty array if none could be found. | ||
@example | ||
``` | ||
// / | ||
// └── Users | ||
// └── sindresorhus | ||
// ├── unicorn.png | ||
// └── foo | ||
// ├── unicorn.png | ||
// └── bar | ||
// ├── baz | ||
// └── example.js | ||
// example.js | ||
import {findUpMultiple} from 'find-up'; | ||
console.log(await findUpMultiple('unicorn.png')); | ||
//=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png'] | ||
console.log(await findUpMultiple(['rainbow.png', 'unicorn.png'])); | ||
//=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png'] | ||
``` | ||
*/ | ||
export function findUpMultiple(name: string | readonly string[], options?: Options): Promise<string[]>; | ||
/** | ||
Find files or directories by walking up parent directories. | ||
@param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search. | ||
@returns All paths found or an empty array if none could be found. | ||
@example | ||
``` | ||
import path from 'node:path'; | ||
import {findUpMultiple, pathExists} from 'find-up'; | ||
console.log(await findUpMultiple(async directory => { | ||
const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png')); | ||
return hasUnicorns && directory; | ||
}, {type: 'directory'})); | ||
//=> ['/Users/sindresorhus/foo', '/Users/sindresorhus'] | ||
``` | ||
*/ | ||
export function findUpMultiple(matcher: (directory: string) => (Match | Promise<Match>), options?: Options): Promise<string[]>; | ||
/** | ||
Synchronously find files or directories by walking up parent directories. | ||
@param name - The name of the file or directory to find. Can be multiple. | ||
@returns All paths found (by respecting the order of `name`s) or an empty array if none could be found. | ||
@example | ||
``` | ||
// / | ||
// └── Users | ||
// └── sindresorhus | ||
// ├── unicorn.png | ||
// └── foo | ||
// ├── unicorn.png | ||
// └── bar | ||
// ├── baz | ||
// └── example.js | ||
// example.js | ||
import {findUpMultipleSync} from 'find-up'; | ||
console.log(findUpMultipleSync('unicorn.png')); | ||
//=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png'] | ||
console.log(findUpMultipleSync(['rainbow.png', 'unicorn.png'])); | ||
//=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png'] | ||
``` | ||
*/ | ||
export function findUpMultipleSync(name: string | readonly string[], options?: Options): string[]; | ||
/** | ||
Synchronously find files or directories by walking up parent directories. | ||
@param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search. | ||
@returns All paths found or an empty array if none could be found. | ||
@example | ||
``` | ||
import path from 'node:path'; | ||
import {findUpMultipleSync, pathExistsSync} from 'find-up'; | ||
console.log(findUpMultipleSync(directory => { | ||
const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png')); | ||
return hasUnicorns && directory; | ||
}, {type: 'directory'})); | ||
//=> ['/Users/sindresorhus/foo', '/Users/sindresorhus'] | ||
``` | ||
*/ | ||
export function findUpMultipleSync(matcher: (directory: string) => Match, options?: Options): string[]; | ||
/** | ||
Check if a path exists. | ||
@@ -121,0 +221,0 @@ |
38
index.js
@@ -6,6 +6,7 @@ import path from 'node:path'; | ||
export async function findUp(name, options = {}) { | ||
export async function findUpMultiple(name, options = {}) { | ||
let directory = path.resolve(options.cwd || ''); | ||
const {root} = path.parse(directory); | ||
const stopAt = path.resolve(directory, options.stopAt || root); | ||
const limit = options.limit || Number.POSITIVE_INFINITY; | ||
const paths = [name].flat(); | ||
@@ -26,2 +27,3 @@ | ||
const matches = []; | ||
// eslint-disable-next-line no-constant-condition | ||
@@ -33,11 +35,11 @@ while (true) { | ||
if (foundPath === findUpStop) { | ||
return; | ||
break; | ||
} | ||
if (foundPath) { | ||
return path.resolve(directory, foundPath); | ||
matches.push(path.resolve(directory, foundPath)); | ||
} | ||
if (directory === stopAt) { | ||
return; | ||
if (directory === stopAt || matches.length >= limit) { | ||
break; | ||
} | ||
@@ -47,8 +49,11 @@ | ||
} | ||
return matches; | ||
} | ||
export function findUpSync(name, options = {}) { | ||
export function findUpMultipleSync(name, options = {}) { | ||
let directory = path.resolve(options.cwd || ''); | ||
const {root} = path.parse(directory); | ||
const stopAt = options.stopAt || root; | ||
const limit = options.limit || Number.POSITIVE_INFINITY; | ||
const paths = [name].flat(); | ||
@@ -69,2 +74,3 @@ | ||
const matches = []; | ||
// eslint-disable-next-line no-constant-condition | ||
@@ -75,11 +81,11 @@ while (true) { | ||
if (foundPath === findUpStop) { | ||
return; | ||
break; | ||
} | ||
if (foundPath) { | ||
return path.resolve(directory, foundPath); | ||
matches.push(path.resolve(directory, foundPath)); | ||
} | ||
if (directory === stopAt) { | ||
return; | ||
if (directory === stopAt || matches.length >= limit) { | ||
break; | ||
} | ||
@@ -89,4 +95,16 @@ | ||
} | ||
return matches; | ||
} | ||
export async function findUp(name, options = {}) { | ||
const matches = await findUpMultiple(name, {...options, limit: 1}); | ||
return matches[0]; | ||
} | ||
export function findUpSync(name, options = {}) { | ||
const matches = findUpMultipleSync(name, {...options, limit: 1}); | ||
return matches[0]; | ||
} | ||
export { | ||
@@ -93,0 +111,0 @@ pathExists, |
{ | ||
"name": "find-up", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "Find a file or directory by walking up parent directories", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -54,2 +54,11 @@ # find-up | ||
### findUpMultiple(name, options?) | ||
### findUpMultiple(matcher, options?) | ||
Returns a `Promise` for either an array of paths or an empty array if none could be found. | ||
### findUpMultiple([...name], options?) | ||
Returns a `Promise` for either an array of the first paths found (by respecting the order of the array) or an empty array if none could be found. | ||
### findUpSync(name, options?) | ||
@@ -64,2 +73,11 @@ ### findUpSync(matcher, options?) | ||
### findUpMultipleSync(name, options?) | ||
### findUpMultipleSync(matcher, options?) | ||
Returns an array of paths or an empty array if none could be found. | ||
### findUpMultipleSync([...name], options?) | ||
Returns an array of the first paths found (by respecting the order of the array) or an empty array if none could be found. | ||
#### name | ||
@@ -66,0 +84,0 @@ |
16875
278
173