pax_global_header00006660000000000000000000000064142004146640014513gustar00rootroot0000000000000052 comment=576b442b12cdec7cefb4ace23b70e2d10b8ace14 find-up-6.3.0/000077500000000000000000000000001420041466400130635ustar00rootroot00000000000000find-up-6.3.0/.editorconfig000066400000000000000000000002571420041466400155440ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 find-up-6.3.0/.gitattributes000066400000000000000000000000231420041466400157510ustar00rootroot00000000000000* text=auto eol=lf find-up-6.3.0/.github/000077500000000000000000000000001420041466400144235ustar00rootroot00000000000000find-up-6.3.0/.github/funding.yml000066400000000000000000000000531420041466400165760ustar00rootroot00000000000000github: sindresorhus tidelift: npm/find-up find-up-6.3.0/.github/security.md000066400000000000000000000002631420041466400166150ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. find-up-6.3.0/.github/workflows/000077500000000000000000000000001420041466400164605ustar00rootroot00000000000000find-up-6.3.0/.github/workflows/main.yml000066400000000000000000000010071420041466400201250ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: node-version: - 16 os: - ubuntu-latest - macos-latest - windows-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test find-up-6.3.0/.gitignore000066400000000000000000000000271420041466400150520ustar00rootroot00000000000000node_modules yarn.lock find-up-6.3.0/.npmrc000066400000000000000000000000231420041466400141760ustar00rootroot00000000000000package-lock=false find-up-6.3.0/fixture/000077500000000000000000000000001420041466400145515ustar00rootroot00000000000000find-up-6.3.0/fixture/baz.js000066400000000000000000000000001420041466400156510ustar00rootroot00000000000000find-up-6.3.0/fixture/directory-link000077700000000000000000000000001420041466400175062.ustar00rootroot00000000000000find-up-6.3.0/fixture/file-link000077700000000000000000000000001420041466400174532baz.jsustar00rootroot00000000000000find-up-6.3.0/fixture/foo/000077500000000000000000000000001420041466400153345ustar00rootroot00000000000000find-up-6.3.0/fixture/foo/bar/000077500000000000000000000000001420041466400161005ustar00rootroot00000000000000find-up-6.3.0/fixture/foo/bar/.gitkeep000066400000000000000000000000001420041466400175170ustar00rootroot00000000000000find-up-6.3.0/fixture/foo/bar/qux.js000066400000000000000000000000001420041466400172410ustar00rootroot00000000000000find-up-6.3.0/fixture/qux.js000066400000000000000000000000001420041466400157120ustar00rootroot00000000000000find-up-6.3.0/index.d.ts000066400000000000000000000165431420041466400147750ustar00rootroot00000000000000/* eslint-disable @typescript-eslint/unified-signatures */ import {Options as LocatePathOptions} from 'locate-path'; /** Return this in a `matcher` function to stop the search and force `findUp` to immediately return `undefined`. */ export const findUpStop: unique symbol; export type Match = string | typeof findUpStop | undefined; export interface Options extends LocatePathOptions { /** The path to the directory to stop the search before reaching root if there were no matches before the `stopAt` directory. @default path.parse(cwd).root */ readonly stopAt?: string; } /** Find a file or directory by walking up parent directories. @param name - The name of the file or directory to find. Can be multiple. @returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found. @example ``` // / // └── Users // └── sindresorhus // ├── unicorn.png // └── foo // └── bar // ├── baz // └── example.js // example.js import {findUp} from 'find-up'; console.log(await findUp('unicorn.png')); //=> '/Users/sindresorhus/unicorn.png' console.log(await findUp(['rainbow.png', 'unicorn.png'])); //=> '/Users/sindresorhus/unicorn.png' ``` */ export function findUp(name: string | readonly string[], options?: Options): Promise; /** Find a file or directory by walking up parent directories. @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search. @returns The first path found or `undefined` if none could be found. @example ``` import path from 'node:path'; import {findUp, pathExists} from 'find-up'; console.log(await findUp(async directory => { const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png')); return hasUnicorns && directory; }, {type: 'directory'})); //=> '/Users/sindresorhus' ``` */ export function findUp(matcher: (directory: string) => (Match | Promise), options?: Options): Promise; /** Synchronously find a file or directory by walking up parent directories. @param name - The name of the file or directory to find. Can be multiple. @returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found. @example ``` // / // └── Users // └── sindresorhus // ├── unicorn.png // └── foo // └── bar // ├── baz // └── example.js // example.js import {findUpSync} from 'find-up'; console.log(findUpSync('unicorn.png')); //=> '/Users/sindresorhus/unicorn.png' console.log(findUpSync(['rainbow.png', 'unicorn.png'])); //=> '/Users/sindresorhus/unicorn.png' ``` */ export function findUpSync(name: string | readonly string[], options?: Options): string | undefined; /** Synchronously find a file or directory by walking up parent directories. @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search. @returns The first path found or `undefined` if none could be found. @example ``` import path from 'node:path'; import {findUpSync, pathExistsSync} from 'find-up'; console.log(findUpSync(directory => { const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png')); return hasUnicorns && directory; }, {type: 'directory'})); //=> '/Users/sindresorhus' ``` */ export function findUpSync(matcher: (directory: string) => Match, options?: Options): string | undefined; /** 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; /** 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), options?: Options): Promise; /** 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. @param path - The path to a file or directory. @returns Whether the path exists. @example ``` import {pathExists} from 'find-up'; console.log(await pathExists('/Users/sindresorhus/unicorn.png')); //=> true ``` */ export function pathExists(path: string): Promise; /** Synchronously check if a path exists. @param path - Path to the file or directory. @returns Whether the path exists. @example ``` import {pathExistsSync} from 'find-up'; console.log(pathExistsSync('/Users/sindresorhus/unicorn.png')); //=> true ``` */ export function pathExistsSync(path: string): boolean; find-up-6.3.0/index.js000066400000000000000000000051671420041466400145410ustar00rootroot00000000000000import path from 'node:path'; import {fileURLToPath} from 'node:url'; import {locatePath, locatePathSync} from 'locate-path'; const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; export const findUpStop = Symbol('findUpStop'); export async function findUpMultiple(name, options = {}) { let directory = path.resolve(toPath(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(); const runMatcher = async locateOptions => { if (typeof name !== 'function') { return locatePath(paths, locateOptions); } const foundPath = await name(locateOptions.cwd); if (typeof foundPath === 'string') { return locatePath([foundPath], locateOptions); } return foundPath; }; const matches = []; // eslint-disable-next-line no-constant-condition while (true) { // eslint-disable-next-line no-await-in-loop const foundPath = await runMatcher({...options, cwd: directory}); if (foundPath === findUpStop) { break; } if (foundPath) { matches.push(path.resolve(directory, foundPath)); } if (directory === stopAt || matches.length >= limit) { break; } directory = path.dirname(directory); } return matches; } export function findUpMultipleSync(name, options = {}) { let directory = path.resolve(toPath(options.cwd) || ''); const {root} = path.parse(directory); const stopAt = options.stopAt || root; const limit = options.limit || Number.POSITIVE_INFINITY; const paths = [name].flat(); const runMatcher = locateOptions => { if (typeof name !== 'function') { return locatePathSync(paths, locateOptions); } const foundPath = name(locateOptions.cwd); if (typeof foundPath === 'string') { return locatePathSync([foundPath], locateOptions); } return foundPath; }; const matches = []; // eslint-disable-next-line no-constant-condition while (true) { const foundPath = runMatcher({...options, cwd: directory}); if (foundPath === findUpStop) { break; } if (foundPath) { matches.push(path.resolve(directory, foundPath)); } if (directory === stopAt || matches.length >= limit) { break; } directory = path.dirname(directory); } 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 { pathExists, pathExistsSync, } from 'path-exists'; find-up-6.3.0/index.test-d.ts000066400000000000000000000400141420041466400157400ustar00rootroot00000000000000import {expectType, expectError} from 'tsd'; import {findUp, findUpSync, findUpMultiple, findUpMultipleSync, findUpStop, pathExists, pathExistsSync} from './index.js'; expectType>(findUp('unicorn.png')); expectType>(findUp('unicorn.png', {cwd: ''})); expectType>(findUp('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(['rainbow.png', 'unicorn.png'])); expectType>(findUp(['rainbow.png', 'unicorn.png'], {cwd: ''})); expectType>(findUp(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); expectType>(findUp(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); expectType>(findUp(['rainbow.png', 'unicorn.png'], {type: 'file'})); expectType>(findUp(['rainbow.png', 'unicorn.png'], {type: 'directory'})); expectType>(findUp(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'})); expectError(findUp(['rainbow.png', 'unicorn.png'], {concurrency: 1})); expectType>(findUp(() => 'unicorn.png')); expectType>(findUp(() => 'unicorn.png', {cwd: ''})); expectType>(findUp(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(() => 'unicorn.png', {allowSymlinks: true})); expectType>(findUp(() => 'unicorn.png', {allowSymlinks: false})); expectType>(findUp(() => 'unicorn.png', {type: 'file'})); expectType>(findUp(() => 'unicorn.png', {type: 'directory'})); expectType>(findUp(() => 'unicorn.png', {stopAt: 'foo'})); expectType>(findUp(() => undefined)); expectType>(findUp(() => undefined, {cwd: ''})); expectType>(findUp(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(() => undefined, {allowSymlinks: true})); expectType>(findUp(() => undefined, {allowSymlinks: false})); expectType>(findUp(() => undefined, {type: 'file'})); expectType>(findUp(() => undefined, {type: 'directory'})); expectType>(findUp(() => undefined, {stopAt: 'foo'})); expectType>(findUp((): typeof findUpStop => findUpStop)); expectType>(findUp((): typeof findUpStop => findUpStop, {cwd: ''})); expectType>(findUp((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); expectType>(findUp(async () => 'unicorn.png')); expectType>(findUp(async () => 'unicorn.png', {cwd: ''})); expectType>(findUp(async () => 'unicorn.png', {allowSymlinks: true})); expectType>(findUp(async () => 'unicorn.png', {allowSymlinks: false})); expectType>(findUp(async () => 'unicorn.png', {type: 'file'})); expectType>(findUp(async () => 'unicorn.png', {type: 'directory'})); expectType>(findUp(async () => 'unicorn.png', {stopAt: 'foo'})); expectType>(findUp(async () => undefined)); expectType>(findUp(async () => undefined, {cwd: ''})); expectType>(findUp(async () => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(async () => undefined, {allowSymlinks: true})); expectType>(findUp(async () => undefined, {allowSymlinks: false})); expectType>(findUp(async () => undefined, {type: 'file'})); expectType>(findUp(async () => undefined, {type: 'directory'})); expectType>(findUp(async () => undefined, {stopAt: 'foo'})); expectType>(findUp(async (): Promise => findUpStop)); expectType>(findUp(async (): Promise => findUpStop, {cwd: ''})); expectType>(findUp(async (): Promise => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(async (): Promise => findUpStop, {allowSymlinks: true})); expectType>(findUp(async (): Promise => findUpStop, {allowSymlinks: false})); expectType>(findUp(async (): Promise => findUpStop, {type: 'file'})); expectType>(findUp(async (): Promise => findUpStop, {type: 'directory'})); expectType>(findUp(async (): Promise => findUpStop, {stopAt: 'foo'})); expectType>(findUpMultiple('unicorn.png')); expectType>(findUpMultiple('unicorn.png', {cwd: ''})); expectType>(findUpMultiple('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'])); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {cwd: ''})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {type: 'file'})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {type: 'directory'})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'})); expectError(findUpMultiple(['rainbow.png', 'unicorn.png'], {concurrency: 1})); expectType>(findUpMultiple(() => 'unicorn.png')); expectType>(findUpMultiple(() => 'unicorn.png', {cwd: ''})); expectType>(findUpMultiple(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(() => 'unicorn.png', {allowSymlinks: true})); expectType>(findUpMultiple(() => 'unicorn.png', {allowSymlinks: false})); expectType>(findUpMultiple(() => 'unicorn.png', {type: 'file'})); expectType>(findUpMultiple(() => 'unicorn.png', {type: 'directory'})); expectType>(findUpMultiple(() => 'unicorn.png', {stopAt: 'foo'})); expectType>(findUpMultiple(() => undefined)); expectType>(findUpMultiple(() => undefined, {cwd: ''})); expectType>(findUpMultiple(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(() => undefined, {allowSymlinks: true})); expectType>(findUpMultiple(() => undefined, {allowSymlinks: false})); expectType>(findUpMultiple(() => undefined, {type: 'file'})); expectType>(findUpMultiple(() => undefined, {type: 'directory'})); expectType>(findUpMultiple(() => undefined, {stopAt: 'foo'})); expectType>(findUpMultiple((): typeof findUpStop => findUpStop)); expectType>(findUpMultiple((): typeof findUpStop => findUpStop, {cwd: ''})); expectType>(findUpMultiple((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); expectType>(findUpMultiple(async () => 'unicorn.png')); expectType>(findUpMultiple(async () => 'unicorn.png', {cwd: ''})); expectType>(findUpMultiple(async () => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(async () => 'unicorn.png', {allowSymlinks: true})); expectType>(findUpMultiple(async () => 'unicorn.png', {allowSymlinks: false})); expectType>(findUpMultiple(async () => 'unicorn.png', {type: 'file'})); expectType>(findUpMultiple(async () => 'unicorn.png', {type: 'directory'})); expectType>(findUpMultiple(async () => 'unicorn.png', {stopAt: 'foo'})); expectType>(findUpMultiple(async () => undefined)); expectType>(findUpMultiple(async () => undefined, {cwd: ''})); expectType>(findUpMultiple(async () => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(async () => undefined, {allowSymlinks: true})); expectType>(findUpMultiple(async () => undefined, {allowSymlinks: false})); expectType>(findUpMultiple(async () => undefined, {type: 'file'})); expectType>(findUpMultiple(async () => undefined, {type: 'directory'})); expectType>(findUpMultiple(async () => undefined, {stopAt: 'foo'})); expectType>(findUpMultiple(async (): Promise => findUpStop)); expectType>(findUpMultiple(async (): Promise => findUpStop, {cwd: ''})); expectType>(findUpMultiple(async (): Promise => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(async (): Promise => findUpStop, {allowSymlinks: true})); expectType>(findUpMultiple(async (): Promise => findUpStop, {allowSymlinks: false})); expectType>(findUpMultiple(async (): Promise => findUpStop, {type: 'file'})); expectType>(findUpMultiple(async (): Promise => findUpStop, {type: 'directory'})); expectType>(findUpMultiple(async (): Promise => findUpStop, {stopAt: 'foo'})); expectType(findUpSync('unicorn.png')); expectType(findUpSync('unicorn.png', {cwd: ''})); expectType(findUpSync('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync(['rainbow.png', 'unicorn.png'])); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {cwd: ''})); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {type: 'file'})); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {type: 'directory'})); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'})); expectType(findUpSync(() => 'unicorn.png')); expectType(findUpSync(() => 'unicorn.png', {cwd: ''})); expectType(findUpSync(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync(() => 'unicorn.png', {allowSymlinks: true})); expectType(findUpSync(() => 'unicorn.png', {allowSymlinks: false})); expectType(findUpSync(() => 'unicorn.png', {type: 'file'})); expectType(findUpSync(() => 'unicorn.png', {type: 'directory'})); expectType(findUpSync(() => 'unicorn.png', {stopAt: 'foo'})); expectType(findUpSync(() => undefined)); expectType(findUpSync(() => undefined, {cwd: ''})); expectType(findUpSync(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync(() => undefined, {allowSymlinks: true})); expectType(findUpSync(() => undefined, {allowSymlinks: false})); expectType(findUpSync(() => undefined, {type: 'file'})); expectType(findUpSync(() => undefined, {type: 'directory'})); expectType(findUpSync(() => undefined, {stopAt: 'foo'})); expectType(findUpSync((): typeof findUpStop => findUpStop)); expectType(findUpSync((): typeof findUpStop => findUpStop, {cwd: ''})); expectType(findUpSync((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync((): typeof findUpStop => findUpStop, {type: 'file'})); expectType(findUpSync((): typeof findUpStop => findUpStop, {type: 'directory'})); expectType(findUpSync((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); expectType(findUpMultipleSync('unicorn.png')); expectType(findUpMultipleSync('unicorn.png', {cwd: ''})); expectType(findUpMultipleSync('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'])); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {cwd: ''})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {type: 'file'})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {type: 'directory'})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'})); expectType(findUpMultipleSync(() => 'unicorn.png')); expectType(findUpMultipleSync(() => 'unicorn.png', {cwd: ''})); expectType(findUpMultipleSync(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync(() => 'unicorn.png', {allowSymlinks: true})); expectType(findUpMultipleSync(() => 'unicorn.png', {allowSymlinks: false})); expectType(findUpMultipleSync(() => 'unicorn.png', {type: 'file'})); expectType(findUpMultipleSync(() => 'unicorn.png', {type: 'directory'})); expectType(findUpMultipleSync(() => 'unicorn.png', {stopAt: 'foo'})); expectType(findUpMultipleSync(() => undefined)); expectType(findUpMultipleSync(() => undefined, {cwd: ''})); expectType(findUpMultipleSync(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync(() => undefined, {allowSymlinks: true})); expectType(findUpMultipleSync(() => undefined, {allowSymlinks: false})); expectType(findUpMultipleSync(() => undefined, {type: 'file'})); expectType(findUpMultipleSync(() => undefined, {type: 'directory'})); expectType(findUpMultipleSync(() => undefined, {stopAt: 'foo'})); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop)); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {cwd: ''})); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {type: 'file'})); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {type: 'directory'})); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); expectType>(pathExists('unicorn.png')); expectType(pathExistsSync('unicorn.png')); find-up-6.3.0/license000066400000000000000000000021351420041466400144310ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. find-up-6.3.0/package.json000066400000000000000000000017371420041466400153610ustar00rootroot00000000000000{ "name": "find-up", "version": "6.3.0", "description": "Find a file or directory by walking up parent directories", "license": "MIT", "repository": "sindresorhus/find-up", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "engines": { "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "find", "up", "find-up", "findup", "look-up", "look", "file", "search", "match", "package", "resolve", "parent", "parents", "folder", "directory", "walk", "walking", "path" ], "dependencies": { "locate-path": "^7.1.0", "path-exists": "^5.0.0" }, "devDependencies": { "ava": "^3.15.0", "is-path-inside": "^4.0.0", "tempy": "^2.0.0", "tsd": "^0.17.0", "xo": "^0.44.0" } } find-up-6.3.0/readme.md000066400000000000000000000111611420041466400146420ustar00rootroot00000000000000# find-up > Find a file or directory by walking up parent directories ## Install ``` $ npm install find-up ``` ## Usage ``` / └── Users └── sindresorhus ├── unicorn.png └── foo └── bar ├── baz └── example.js ``` `example.js` ```js import path from 'node:path'; import {findUp, pathExists} from 'find-up'; console.log(await findUp('unicorn.png')); //=> '/Users/sindresorhus/unicorn.png' console.log(await findUp(['rainbow.png', 'unicorn.png'])); //=> '/Users/sindresorhus/unicorn.png' console.log(await findUp(async directory => { const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png')); return hasUnicorns && directory; }, {type: 'directory'})); //=> '/Users/sindresorhus' ``` ## API ### findUp(name, options?) ### findUp(matcher, options?) Returns a `Promise` for either the path or `undefined` if it couldn't be found. ### findUp([...name], options?) Returns a `Promise` for either the first path found (by respecting the order of the array) or `undefined` if none could be found. ### 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?) ### findUpSync(matcher, options?) Returns a path or `undefined` if it couldn't be found. ### findUpSync([...name], options?) Returns the first path found (by respecting the order of the array) or `undefined` if none could be found. ### 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 Type: `string` The name of the file or directory to find. #### matcher Type: `Function` A function that will be called with each directory until it returns a `string` with the path, which stops the search, or the root directory has been reached and nothing was found. Useful if you want to match files with certain patterns, set of permissions, or other advanced use-cases. When using async mode, the `matcher` may optionally be an async or promise-returning function that returns the path. #### options Type: `object` ##### cwd Type: `URL | string`\ Default: `process.cwd()` The directory to start from. ##### type Type: `string`\ Default: `'file'`\ Values: `'file'` `'directory'` The type of paths that can match. ##### allowSymlinks Type: `boolean`\ Default: `true` Allow symbolic links to match if they point to the chosen path type. ##### stopAt Type: `string`\ Default: `path.parse(cwd).root` The path to the directory to stop the search before reaching root if there were no matches before the `stopAt` directory. ### pathExists(path) Returns a `Promise` of whether the path exists. ### pathExistsSync(path) Returns a `boolean` of whether the path exists. #### path Type: `string` The path to a file or directory. ### findUpStop A [`Symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) that can be returned by a `matcher` function to stop the search and cause `findUp` to immediately return `undefined`. Useful as a performance optimization in case the current working directory is deeply nested in the filesystem. ```js import path from 'node:path'; import {findUp, findUpStop} from 'find-up'; await findUp(directory => { return path.basename(directory) === 'work' ? findUpStop : 'logo.png'; }); ``` ## Related - [find-up-cli](https://github.com/sindresorhus/find-up-cli) - CLI for this module - [pkg-up](https://github.com/sindresorhus/pkg-up) - Find the closest package.json file - [pkg-dir](https://github.com/sindresorhus/pkg-dir) - Find the root directory of an npm package - [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module like `require.resolve()` but from a given path ---
Get professional support for 'find-up' with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
find-up-6.3.0/test.js000066400000000000000000000427701420041466400144120ustar00rootroot00000000000000import process from 'node:process'; import {promisify} from 'node:util'; import fs from 'node:fs'; import path from 'node:path'; import {fileURLToPath, pathToFileURL} from 'node:url'; import test from 'ava'; import isPathInside from 'is-path-inside'; import tempy from 'tempy'; import {findUp, findUpSync, findUpMultiple, findUpMultipleSync, findUpStop, pathExists, pathExistsSync} from './index.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const name = { packageDirectory: 'find-up', packageJson: 'package.json', fixtureDirectory: 'fixture', fooDirectory: 'foo', barDirectory: 'bar', modulesDirectory: 'node_modules', baz: 'baz.js', qux: 'qux.js', fileLink: 'file-link', directoryLink: 'directory-link', }; // These paths are relative to the project root const relative = { fixtureDirectory: name.fixtureDirectory, modulesDirectory: name.modulesDirectory, }; relative.baz = path.join(relative.fixtureDirectory, name.baz); relative.qux = path.join(relative.fixtureDirectory, name.qux); relative.barDirQux = path.join(relative.fixtureDirectory, name.fooDirectory, name.barDirectory, name.qux); relative.barDir = path.join(relative.fixtureDirectory, name.fooDirectory, name.barDirectory); const absolute = { packageDirectory: __dirname, }; absolute.packageJson = path.join(absolute.packageDirectory, name.packageJson); absolute.fixtureDirectory = path.join( absolute.packageDirectory, name.fixtureDirectory, ); absolute.baz = path.join(absolute.fixtureDirectory, name.baz); absolute.qux = path.join(absolute.fixtureDirectory, name.qux); absolute.fooDir = path.join(absolute.fixtureDirectory, name.fooDirectory); absolute.barDir = path.join(absolute.fixtureDirectory, name.fooDirectory, name.barDirectory); absolute.barDirQux = path.join(absolute.fixtureDirectory, name.fooDirectory, name.barDirectory, name.qux); absolute.fileLink = path.join(absolute.fixtureDirectory, name.fileLink); absolute.directoryLink = path.join(absolute.fixtureDirectory, name.directoryLink); const url = { fixtureDirectory: pathToFileURL(absolute.fixtureDirectory), }; // Create a disjoint directory, used for the not-found tests test.beforeEach(t => { const temporaryDirectory = tempy.directory(); t.context.disjoint = temporaryDirectory; }); test.afterEach(t => { fs.rmdirSync(t.context.disjoint); }); const isWindows = process.platform === 'win32'; test('async (child file)', async t => { const foundPath = await findUp(name.packageJson); t.is(foundPath, absolute.packageJson); }); test('sync (child file)', t => { const foundPath = findUpSync(name.packageJson); t.is(foundPath, absolute.packageJson); }); test('async (child directory)', async t => { const foundPath = await findUp(name.fixtureDirectory, {type: 'directory'}); t.is(foundPath, absolute.fixtureDirectory); }); test('sync (child directory)', t => { const foundPath = findUpSync(name.fixtureDirectory, {type: 'directory'}); t.is(foundPath, absolute.fixtureDirectory); }); test('async (explicit type file)', async t => { t.is(await findUp(name.packageJson, {type: 'file'}), absolute.packageJson); t.is(await findUp(name.packageJson, {type: 'directory'}), undefined); }); test('sync (explicit type file)', t => { t.is(findUpSync(name.packageJson, {type: 'file'}), absolute.packageJson); t.is(findUpSync(name.packageJson, {type: 'directory'}), undefined); }); if (!isWindows) { test('async (symbolic links)', async t => { const cwd = absolute.fixtureDirectory; t.is(await findUp(name.fileLink, {cwd}), absolute.fileLink); t.is(await findUp(name.fileLink, {cwd, allowSymlinks: false}), undefined); t.is(await findUp(name.directoryLink, {cwd, type: 'directory'}), absolute.directoryLink); t.is(await findUp(name.directoryLink, {cwd, type: 'directory', allowSymlinks: false}), undefined); }); test('sync (symbolic links)', t => { const cwd = absolute.fixtureDirectory; t.is(findUpSync(name.fileLink, {cwd}), absolute.fileLink); t.is(findUpSync(name.fileLink, {cwd, allowSymlinks: false}), undefined); t.is(findUpSync(name.directoryLink, {cwd, type: 'directory'}), absolute.directoryLink); t.is(findUpSync(name.directoryLink, {cwd, type: 'directory', allowSymlinks: false}), undefined); }); } test('async (child file, custom cwd)', async t => { const foundPath = await findUp(name.baz, { cwd: relative.fixtureDirectory, }); t.is(foundPath, absolute.baz); const foundPath2 = await findUp(name.baz, { cwd: url.fixtureDirectory, }); t.is(foundPath2, foundPath); }); test('sync (child file, custom cwd)', t => { const foundPath = findUpSync(name.baz, { cwd: relative.fixtureDirectory, }); t.is(foundPath, absolute.baz); const foundPath2 = findUpSync(name.baz, { cwd: url.fixtureDirectory, }); t.is(foundPath2, foundPath); }); test('async (child file, array, custom cwd)', async t => { const foundPath = await findUp([name.baz], { cwd: relative.fixtureDirectory, }); t.is(foundPath, absolute.baz); }); test('sync (child file, array, custom cwd)', t => { const foundPath = findUpSync([name.baz], { cwd: relative.fixtureDirectory, }); t.is(foundPath, absolute.baz); }); test('async (first child file, array, custom cwd)', async t => { const foundPath = await findUp([name.qux, name.baz], { cwd: relative.fixtureDirectory, }); t.is(foundPath, absolute.qux); }); test('sync (first child file, array, custom cwd)', t => { const foundPath = findUpSync([name.qux, name.baz], { cwd: relative.fixtureDirectory, }); t.is(foundPath, absolute.qux); }); test('async (second child file, array, custom cwd)', async t => { const foundPath = await findUp(['fake', name.baz], { cwd: relative.fixtureDirectory, }); t.is(foundPath, absolute.baz); }); test('sync (second child file, array, custom cwd)', t => { const foundPath = findUpSync(['fake', name.baz], { cwd: relative.fixtureDirectory, }); t.is(foundPath, absolute.baz); }); test('async (cwd)', async t => { const foundPath = await findUp(name.packageDirectory, { cwd: absolute.packageDirectory, type: 'directory', }); t.is(foundPath, absolute.packageDirectory); }); test('sync (cwd)', t => { const foundPath = findUpSync(name.packageDirectory, { cwd: absolute.packageDirectory, type: 'directory', }); t.is(foundPath, absolute.packageDirectory); }); test('async (cousin file, custom cwd)', async t => { const foundPath = await findUp(name.baz, { cwd: relative.barDir, }); t.is(foundPath, absolute.baz); }); test('sync (cousin file, custom cwd)', t => { const foundPath = findUpSync(name.baz, { cwd: relative.barDir, }); t.is(foundPath, absolute.baz); }); test('async (cousin file, custom cwd with stopAt)', async t => { const foundPath = await findUp(name.baz, { cwd: relative.barDir, stopAt: absolute.fooDir, }); t.is(foundPath, undefined); }); test('sync (cousin file, custom cwd with stopAt)', t => { const foundPath = findUpSync(name.baz, { cwd: relative.barDir, stopAt: absolute.fooDir, }); t.is(foundPath, undefined); }); test('async (cousin file, custom cwd, stopAt equal to foundPath)', async t => { const foundPath = await findUp(name.baz, { cwd: relative.barDir, stopAt: absolute.baz, }); t.is(foundPath, absolute.baz); }); test('sync (cousin file, custom cwd, stopAt equal to foundPath)', t => { const foundPath = findUpSync(name.baz, { cwd: relative.barDir, stopAt: absolute.baz, }); t.is(foundPath, absolute.baz); }); test('async (nested descendant file)', async t => { const foundPath = await findUp(relative.baz); t.is(foundPath, absolute.baz); }); test('sync (nested descendant file)', t => { const foundPath = findUpSync(relative.baz); t.is(foundPath, absolute.baz); }); test('async (nested descendant directory)', async t => { const foundPath = await findUp(relative.barDir, {type: 'directory'}); t.is(foundPath, absolute.barDir); }); test('sync (nested descendant directory)', t => { const foundPath = findUpSync(relative.barDir, {type: 'directory'}); t.is(foundPath, absolute.barDir); }); test('async (nested descendant directory, custom cwd)', async t => { const filePath = await findUp(relative.barDir, { cwd: relative.modulesDirectory, type: 'directory', }); t.is(filePath, absolute.barDir); }); test('sync (nested descendant directory, custom cwd)', t => { const filePath = findUpSync(relative.barDir, { cwd: relative.modulesDirectory, type: 'directory', }); t.is(filePath, absolute.barDir); }); test('async (nested cousin directory, custom cwd)', async t => { const foundPath = await findUp(relative.barDir, { cwd: relative.fixtureDirectory, type: 'directory', }); t.is(foundPath, absolute.barDir); }); test('sync (nested cousin directory, custom cwd)', t => { const foundPath = findUpSync(relative.barDir, { cwd: relative.fixtureDirectory, type: 'directory', }); t.is(foundPath, absolute.barDir); }); test('async (ancestor directory, custom cwd)', async t => { const foundPath = await findUp(name.fixtureDirectory, { cwd: relative.barDir, type: 'directory', }); t.is(foundPath, absolute.fixtureDirectory); }); test('sync (ancestor directory, custom cwd)', t => { const foundPath = findUpSync(name.fixtureDirectory, { cwd: relative.barDir, type: 'directory', }); t.is(foundPath, absolute.fixtureDirectory); }); test('async (absolute directory)', async t => { const filePath = await findUp(absolute.barDir, {type: 'directory'}); t.is(filePath, absolute.barDir); }); test('sync (absolute directory)', t => { const filePath = findUpSync(absolute.barDir, {type: 'directory'}); t.is(filePath, absolute.barDir); }); test('async multiple (child file)', async t => { const filePaths = await findUpMultiple(name.qux, {cwd: relative.barDir}); t.deepEqual(filePaths, [absolute.barDirQux, absolute.qux]); }); test('sync multiple (child file)', t => { const filePaths = findUpMultipleSync(name.qux, {cwd: relative.barDir}); t.deepEqual(filePaths, [absolute.barDirQux, absolute.qux]); }); test('async multiple (child directory)', async t => { const foundPath = await findUpMultiple(name.fixtureDirectory, {type: 'directory'}); t.deepEqual(foundPath, [absolute.fixtureDirectory]); }); test('sync multiple (child directory)', t => { const foundPath = findUpMultipleSync(name.fixtureDirectory, {type: 'directory'}); t.deepEqual(foundPath, [absolute.fixtureDirectory]); }); test('async multiple (child file, array)', async t => { const filePaths = await findUpMultiple([name.baz, name.qux], {cwd: relative.barDir}); t.deepEqual(filePaths, [absolute.barDirQux, absolute.baz]); }); test('sync multiple (child file, array)', t => { const filePaths = findUpMultipleSync([name.baz, name.qux], {cwd: relative.barDir}); t.deepEqual(filePaths, [absolute.barDirQux, absolute.baz]); }); test('async multiple (child directory, custom cwd, array)', async t => { const foundPath = await findUpMultiple([name.fixtureDirectory, name.fooDirectory], { cwd: absolute.barDir, type: 'directory', }); t.deepEqual(foundPath, [absolute.fooDir, absolute.fixtureDirectory]); }); test('sync multiple (child directory, custom cwd, array)', t => { const foundPath = findUpMultipleSync([name.fixtureDirectory, name.fooDirectory], { cwd: absolute.barDir, type: 'directory', }); t.deepEqual(foundPath, [absolute.fooDir, absolute.fixtureDirectory]); }); test('async multiple (child file with stopAt)', async t => { const filePaths = await findUpMultiple(name.qux, { cwd: relative.barDir, stopAt: absolute.fooDir, }); t.deepEqual(filePaths, [absolute.barDirQux]); }); test('sync multiple (child file with stopAt)', t => { const filePaths = findUpMultipleSync(name.qux, { cwd: relative.barDir, stopAt: absolute.fooDir, }); t.deepEqual(filePaths, [absolute.barDirQux]); }); test('async multiple (not found, child file)', async t => { const filePaths = await findUpMultiple('somenonexistentfile.js', {cwd: relative.barDir}); t.deepEqual(filePaths, []); }); test('sync multiple (not found, child file)', t => { const filePaths = findUpMultipleSync('somenonexistentfile.js', {cwd: relative.barDir}); t.deepEqual(filePaths, []); }); test('async (not found, absolute file)', async t => { const filePath = await findUp(path.resolve('somenonexistentfile.js')); t.is(filePath, undefined); }); test('sync (not found, absolute file)', t => { const filePath = findUpSync(path.resolve('somenonexistentfile.js')); t.is(filePath, undefined); }); test('async (absolute directory, disjoint cwd)', async t => { const filePath = await findUp(absolute.barDir, { cwd: t.context.disjoint, type: 'directory', }); t.is(filePath, absolute.barDir); }); test('sync (absolute directory, disjoint cwd)', t => { const filePath = findUpSync(absolute.barDir, { cwd: t.context.disjoint, type: 'directory', }); t.is(filePath, absolute.barDir); }); test('async (not found)', async t => { const foundPath = await findUp('somenonexistentfile.js'); t.is(foundPath, undefined); }); test('sync (not found)', t => { const foundPath = findUpSync('somenonexistentfile.js'); t.is(foundPath, undefined); }); // Both tests start in a disjoint directory. `package.json` should not be found // and `undefined` should be returned. test('async (not found, custom cwd)', async t => { const foundPath = await findUp(name.packageJson, { cwd: t.context.disjoint, }); t.is(foundPath, undefined); }); test('sync (not found, custom cwd)', t => { const foundPath = findUpSync(name.packageJson, { cwd: t.context.disjoint, }); t.is(foundPath, undefined); }); test('async (matcher function)', async t => { const cwd = process.cwd(); t.is(await findUp(directory => { t.is(directory, cwd); return directory; }, {type: 'directory'}), cwd); t.is(await findUp(() => '.', {type: 'directory'}), cwd); t.is(await findUp(async () => 'package.json'), path.join(cwd, 'package.json')); t.is(await findUp(() => '..', {type: 'directory'}), path.join(cwd, '..')); t.is(await findUp(directory => (directory !== cwd) && directory, {type: 'directory'}), path.join(cwd, '..')); t.is(await findUp(directory => (directory === cwd) && 'package.json', {cwd: absolute.fixtureDirectory}), absolute.packageJson); }); test('async (not found, matcher function)', async t => { const cwd = process.cwd(); const {root} = path.parse(cwd); const visited = new Set(); t.is(await findUp(async directory => { t.is(typeof directory, 'string'); const stat = await promisify(fs.stat)(directory); t.true(stat.isDirectory()); t.true((directory === cwd) || isPathInside(cwd, directory)); t.false(visited.has(directory)); visited.add(directory); }), undefined); t.true(visited.has(cwd)); t.true(visited.has(root)); }); test('async (matcher function throws)', async t => { const cwd = process.cwd(); const visited = new Set(); await t.throwsAsync(findUp(directory => { visited.add(directory); 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(findUp(async directory => { visited.add(directory); throw new Error('Some async rejection'); }), { message: 'Some async rejection', }); t.true(visited.has(cwd)); t.is(visited.size, 1); }); test('async (matcher function stops early)', async t => { const cwd = process.cwd(); const visited = new Set(); t.is(await findUp(async directory => { visited.add(directory); return findUpStop; }), undefined); t.true(visited.has(cwd)); t.is(visited.size, 1); }); test('sync (matcher function)', t => { const cwd = process.cwd(); t.is(findUpSync(directory => { t.is(directory, cwd); return directory; }, {type: 'directory'}), cwd); t.is(findUpSync(() => '.', {type: 'directory'}), cwd); t.is(findUpSync(() => 'package.json'), path.join(cwd, 'package.json')); t.is(findUpSync(() => '..', {type: 'directory'}), path.join(cwd, '..')); t.is(findUpSync(directory => (directory !== cwd) && directory, {type: 'directory'}), path.join(cwd, '..')); t.is(findUpSync(directory => (directory === cwd) && 'package.json', {cwd: absolute.fixtureDirectory}), absolute.packageJson); }); test('sync (not found, matcher function)', t => { const cwd = process.cwd(); const {root} = path.parse(cwd); const visited = new Set(); t.is(findUpSync(directory => { t.is(typeof directory, 'string'); const stat = fs.statSync(directory); t.true(stat.isDirectory()); t.true((directory === cwd) || isPathInside(cwd, directory)); t.false(visited.has(directory)); visited.add(directory); }), undefined); 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(() => { findUpSync(directory => { visited.add(directory); throw new Error('Some problem'); }); }, { message: 'Some problem', }); t.true(visited.has(cwd)); t.is(visited.size, 1); }); test('sync (matcher function stops early)', t => { const cwd = process.cwd(); const visited = new Set(); t.is(findUpSync(directory => { visited.add(directory); return findUpStop; }), undefined); t.true(visited.has(cwd)); t.is(visited.size, 1); }); test('async (check if path exists)', async t => { if (!isWindows) { t.true(await pathExists(absolute.directoryLink)); t.true(await pathExists(absolute.fileLink)); } t.true(await pathExists(absolute.barDir)); t.true(await pathExists(absolute.packageJson)); t.false(await pathExists('fake')); }); test('sync (check if path exists)', t => { if (!isWindows) { t.true(pathExistsSync(absolute.directoryLink)); t.true(pathExistsSync(absolute.fileLink)); } t.true(pathExistsSync(absolute.barDir)); t.true(pathExistsSync(absolute.packageJson)); t.false(pathExistsSync('fake')); });