├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── fixture ├── baz.js ├── directory-link ├── file-link ├── foo │ └── bar │ │ ├── .gitkeep │ │ └── qux.js └── qux.js ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }} 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | os: 15 | - ubuntu-latest 16 | - macos-latest 17 | - windows-latest 18 | steps: 19 | - uses: actions/checkout@v4 20 | - uses: actions/setup-node@v4 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | - run: npm install 24 | - run: npm test 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /fixture/baz.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/find-up/6076472b32ba30c07dde2a75c45972acd59bac7a/fixture/baz.js -------------------------------------------------------------------------------- /fixture/directory-link: -------------------------------------------------------------------------------- 1 | . -------------------------------------------------------------------------------- /fixture/file-link: -------------------------------------------------------------------------------- 1 | baz.js -------------------------------------------------------------------------------- /fixture/foo/bar/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/find-up/6076472b32ba30c07dde2a75c45972acd59bac7a/fixture/foo/bar/.gitkeep -------------------------------------------------------------------------------- /fixture/foo/bar/qux.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/find-up/6076472b32ba30c07dde2a75c45972acd59bac7a/fixture/foo/bar/qux.js -------------------------------------------------------------------------------- /fixture/qux.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/find-up/6076472b32ba30c07dde2a75c45972acd59bac7a/fixture/qux.js -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import {type Options as LocatePathOptions} from 'locate-path'; 2 | 3 | /** 4 | Return this in a `matcher` function to stop the search and force `findUp` to immediately return `undefined`. 5 | */ 6 | export const findUpStop: unique symbol; 7 | 8 | export type Match = string | typeof findUpStop | undefined; 9 | 10 | export type Options = { 11 | /** 12 | A directory path where the search halts if no matches are found before reaching this point. 13 | 14 | Default: Root directory 15 | */ 16 | readonly stopAt?: string; 17 | } & LocatePathOptions; 18 | 19 | /** 20 | Find a file or directory by walking up parent directories. 21 | 22 | @param name - The name of the file or directory to find. Can be multiple. 23 | @returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found. 24 | 25 | @example 26 | ``` 27 | // / 28 | // └── Users 29 | // └── sindresorhus 30 | // ├── unicorn.png 31 | // └── foo 32 | // └── bar 33 | // ├── baz 34 | // └── example.js 35 | 36 | // example.js 37 | import {findUp} from 'find-up'; 38 | 39 | console.log(await findUp('unicorn.png')); 40 | //=> '/Users/sindresorhus/unicorn.png' 41 | 42 | console.log(await findUp(['rainbow.png', 'unicorn.png'])); 43 | //=> '/Users/sindresorhus/unicorn.png' 44 | ``` 45 | */ 46 | export function findUp(name: string | readonly string[], options?: Options): Promise; 47 | 48 | /** 49 | Find a file or directory by walking up parent directories. 50 | 51 | @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search. 52 | @returns The first path found or `undefined` if none could be found. 53 | 54 | @example 55 | ``` 56 | import path from 'node:path'; 57 | import {findUp, pathExists} from 'find-up'; 58 | 59 | console.log(await findUp(async directory => { 60 | const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png')); 61 | return hasUnicorns && directory; 62 | }, {type: 'directory'})); 63 | //=> '/Users/sindresorhus' 64 | ``` 65 | */ 66 | export function findUp(matcher: (directory: string) => (Match | Promise), options?: Options): Promise; 67 | 68 | /** 69 | Synchronously find a file or directory by walking up parent directories. 70 | 71 | @param name - The name of the file or directory to find. Can be multiple. 72 | @returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found. 73 | 74 | @example 75 | ``` 76 | // / 77 | // └── Users 78 | // └── sindresorhus 79 | // ├── unicorn.png 80 | // └── foo 81 | // └── bar 82 | // ├── baz 83 | // └── example.js 84 | 85 | // example.js 86 | import {findUpSync} from 'find-up'; 87 | 88 | console.log(findUpSync('unicorn.png')); 89 | //=> '/Users/sindresorhus/unicorn.png' 90 | 91 | console.log(findUpSync(['rainbow.png', 'unicorn.png'])); 92 | //=> '/Users/sindresorhus/unicorn.png' 93 | ``` 94 | */ 95 | export function findUpSync(name: string | readonly string[], options?: Options): string | undefined; 96 | 97 | /** 98 | Synchronously find a file or directory by walking up parent directories. 99 | 100 | @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search. 101 | @returns The first path found or `undefined` if none could be found. 102 | 103 | @example 104 | ``` 105 | import path from 'node:path'; 106 | import {findUpSync, pathExistsSync} from 'find-up'; 107 | 108 | console.log(findUpSync(directory => { 109 | const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png')); 110 | return hasUnicorns && directory; 111 | }, {type: 'directory'})); 112 | //=> '/Users/sindresorhus' 113 | ``` 114 | */ 115 | export function findUpSync(matcher: (directory: string) => Match, options?: Options): string | undefined; 116 | 117 | /** 118 | Find files or directories by walking up parent directories. 119 | 120 | @param name - The name of the file or directory to find. Can be multiple. 121 | @returns All paths found (by respecting the order of `name`s) or an empty array if none could be found. 122 | 123 | @example 124 | ``` 125 | // / 126 | // └── Users 127 | // └── sindresorhus 128 | // ├── unicorn.png 129 | // └── foo 130 | // ├── unicorn.png 131 | // └── bar 132 | // ├── baz 133 | // └── example.js 134 | 135 | // example.js 136 | import {findUpMultiple} from 'find-up'; 137 | 138 | console.log(await findUpMultiple('unicorn.png')); 139 | //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png'] 140 | 141 | console.log(await findUpMultiple(['rainbow.png', 'unicorn.png'])); 142 | //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png'] 143 | ``` 144 | */ 145 | export function findUpMultiple(name: string | readonly string[], options?: Options): Promise; 146 | 147 | /** 148 | Find files or directories by walking up parent directories. 149 | 150 | @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search. 151 | @returns All paths found or an empty array if none could be found. 152 | 153 | @example 154 | ``` 155 | import path from 'node:path'; 156 | import {findUpMultiple, pathExists} from 'find-up'; 157 | 158 | console.log(await findUpMultiple(async directory => { 159 | const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png')); 160 | return hasUnicorns && directory; 161 | }, {type: 'directory'})); 162 | //=> ['/Users/sindresorhus/foo', '/Users/sindresorhus'] 163 | ``` 164 | */ 165 | export function findUpMultiple(matcher: (directory: string) => (Match | Promise), options?: Options): Promise; 166 | 167 | /** 168 | Synchronously find files or directories by walking up parent directories. 169 | 170 | @param name - The name of the file or directory to find. Can be multiple. 171 | @returns All paths found (by respecting the order of `name`s) or an empty array if none could be found. 172 | 173 | @example 174 | ``` 175 | // / 176 | // └── Users 177 | // └── sindresorhus 178 | // ├── unicorn.png 179 | // └── foo 180 | // ├── unicorn.png 181 | // └── bar 182 | // ├── baz 183 | // └── example.js 184 | 185 | // example.js 186 | import {findUpMultipleSync} from 'find-up'; 187 | 188 | console.log(findUpMultipleSync('unicorn.png')); 189 | //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png'] 190 | 191 | console.log(findUpMultipleSync(['rainbow.png', 'unicorn.png'])); 192 | //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png'] 193 | ``` 194 | */ 195 | export function findUpMultipleSync(name: string | readonly string[], options?: Options): string[]; 196 | 197 | /** 198 | Synchronously find files or directories by walking up parent directories. 199 | 200 | @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search. 201 | @returns All paths found or an empty array if none could be found. 202 | 203 | @example 204 | ``` 205 | import path from 'node:path'; 206 | import {findUpMultipleSync, pathExistsSync} from 'find-up'; 207 | 208 | console.log(findUpMultipleSync(directory => { 209 | const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png')); 210 | return hasUnicorns && directory; 211 | }, {type: 'directory'})); 212 | //=> ['/Users/sindresorhus/foo', '/Users/sindresorhus'] 213 | ``` 214 | */ 215 | export function findUpMultipleSync(matcher: (directory: string) => Match, options?: Options): string[]; 216 | 217 | /** 218 | Check if a path exists. 219 | 220 | @param path - The path to a file or directory. 221 | @returns Whether the path exists. 222 | 223 | @example 224 | ``` 225 | import {pathExists} from 'find-up'; 226 | 227 | console.log(await pathExists('/Users/sindresorhus/unicorn.png')); 228 | //=> true 229 | ``` 230 | */ 231 | export function pathExists(path: string): Promise; 232 | 233 | /** 234 | Synchronously check if a path exists. 235 | 236 | @param path - Path to the file or directory. 237 | @returns Whether the path exists. 238 | 239 | @example 240 | ``` 241 | import {pathExistsSync} from 'find-up'; 242 | 243 | console.log(pathExistsSync('/Users/sindresorhus/unicorn.png')); 244 | //=> true 245 | ``` 246 | */ 247 | export function pathExistsSync(path: string): boolean; 248 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | import {locatePath, locatePathSync} from 'locate-path'; 3 | import {toPath} from 'unicorn-magic'; 4 | 5 | export const findUpStop = Symbol('findUpStop'); 6 | 7 | export async function findUpMultiple(name, options = {}) { 8 | let directory = path.resolve(toPath(options.cwd) ?? ''); 9 | const {root} = path.parse(directory); 10 | const stopAt = path.resolve(directory, toPath(options.stopAt) ?? root); 11 | const limit = options.limit ?? Number.POSITIVE_INFINITY; 12 | const paths = [name].flat(); 13 | 14 | const runMatcher = async locateOptions => { 15 | if (typeof name !== 'function') { 16 | return locatePath(paths, locateOptions); 17 | } 18 | 19 | const foundPath = await name(locateOptions.cwd); 20 | if (typeof foundPath === 'string') { 21 | return locatePath([foundPath], locateOptions); 22 | } 23 | 24 | return foundPath; 25 | }; 26 | 27 | const matches = []; 28 | // eslint-disable-next-line no-constant-condition 29 | while (true) { 30 | // eslint-disable-next-line no-await-in-loop 31 | const foundPath = await runMatcher({...options, cwd: directory}); 32 | 33 | if (foundPath === findUpStop) { 34 | break; 35 | } 36 | 37 | if (foundPath) { 38 | matches.push(path.resolve(directory, foundPath)); 39 | } 40 | 41 | if (directory === stopAt || matches.length >= limit) { 42 | break; 43 | } 44 | 45 | directory = path.dirname(directory); 46 | } 47 | 48 | return matches; 49 | } 50 | 51 | export function findUpMultipleSync(name, options = {}) { 52 | let directory = path.resolve(toPath(options.cwd) ?? ''); 53 | const {root} = path.parse(directory); 54 | const stopAt = path.resolve(directory, toPath(options.stopAt) ?? root); 55 | const limit = options.limit ?? Number.POSITIVE_INFINITY; 56 | const paths = [name].flat(); 57 | 58 | const runMatcher = locateOptions => { 59 | if (typeof name !== 'function') { 60 | return locatePathSync(paths, locateOptions); 61 | } 62 | 63 | const foundPath = name(locateOptions.cwd); 64 | if (typeof foundPath === 'string') { 65 | return locatePathSync([foundPath], locateOptions); 66 | } 67 | 68 | return foundPath; 69 | }; 70 | 71 | const matches = []; 72 | // eslint-disable-next-line no-constant-condition 73 | while (true) { 74 | const foundPath = runMatcher({...options, cwd: directory}); 75 | 76 | if (foundPath === findUpStop) { 77 | break; 78 | } 79 | 80 | if (foundPath) { 81 | matches.push(path.resolve(directory, foundPath)); 82 | } 83 | 84 | if (directory === stopAt || matches.length >= limit) { 85 | break; 86 | } 87 | 88 | directory = path.dirname(directory); 89 | } 90 | 91 | return matches; 92 | } 93 | 94 | export async function findUp(name, options = {}) { 95 | const matches = await findUpMultiple(name, {...options, limit: 1}); 96 | return matches[0]; 97 | } 98 | 99 | export function findUpSync(name, options = {}) { 100 | const matches = findUpMultipleSync(name, {...options, limit: 1}); 101 | return matches[0]; 102 | } 103 | 104 | export { 105 | pathExists, 106 | pathExistsSync, 107 | } from 'path-exists'; 108 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType, expectError} from 'tsd'; 2 | import { 3 | findUp, 4 | findUpSync, 5 | findUpMultiple, 6 | findUpMultipleSync, 7 | findUpStop, 8 | pathExists, 9 | pathExistsSync, 10 | } from './index.js'; 11 | 12 | expectType>(findUp('unicorn.png')); 13 | expectType>(findUp('unicorn.png', {cwd: ''})); 14 | expectType>(findUp('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); 15 | expectType>(findUp(['rainbow.png', 'unicorn.png'])); 16 | expectType>(findUp(['rainbow.png', 'unicorn.png'], {cwd: ''})); 17 | expectType>(findUp(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); 18 | expectType>(findUp(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); 19 | expectType>(findUp(['rainbow.png', 'unicorn.png'], {type: 'file'})); 20 | expectType>(findUp(['rainbow.png', 'unicorn.png'], {type: 'directory'})); 21 | expectType>(findUp(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'})); 22 | expectError(findUp(['rainbow.png', 'unicorn.png'], {concurrency: 1})); 23 | 24 | expectType>(findUp(() => 'unicorn.png')); 25 | expectType>(findUp(() => 'unicorn.png', {cwd: ''})); 26 | expectType>(findUp(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); 27 | expectType>(findUp(() => 'unicorn.png', {allowSymlinks: true})); 28 | expectType>(findUp(() => 'unicorn.png', {allowSymlinks: false})); 29 | expectType>(findUp(() => 'unicorn.png', {type: 'file'})); 30 | expectType>(findUp(() => 'unicorn.png', {type: 'directory'})); 31 | expectType>(findUp(() => 'unicorn.png', {stopAt: 'foo'})); 32 | expectType>(findUp(() => undefined)); 33 | expectType>(findUp(() => undefined, {cwd: ''})); 34 | expectType>(findUp(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); 35 | expectType>(findUp(() => undefined, {allowSymlinks: true})); 36 | expectType>(findUp(() => undefined, {allowSymlinks: false})); 37 | expectType>(findUp(() => undefined, {type: 'file'})); 38 | expectType>(findUp(() => undefined, {type: 'directory'})); 39 | expectType>(findUp(() => undefined, {stopAt: 'foo'})); 40 | expectType>(findUp((): typeof findUpStop => findUpStop)); 41 | expectType>(findUp((): typeof findUpStop => findUpStop, {cwd: ''})); 42 | expectType>(findUp((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); 43 | expectType>(findUp((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); 44 | expectType>(findUp(async () => 'unicorn.png')); 45 | expectType>(findUp(async () => 'unicorn.png', {cwd: ''})); 46 | expectType>(findUp(async () => 'unicorn.png', {allowSymlinks: true})); 47 | expectType>(findUp(async () => 'unicorn.png', {allowSymlinks: false})); 48 | expectType>(findUp(async () => 'unicorn.png', {type: 'file'})); 49 | expectType>(findUp(async () => 'unicorn.png', {type: 'directory'})); 50 | expectType>(findUp(async () => 'unicorn.png', {stopAt: 'foo'})); 51 | expectType>(findUp(async () => undefined)); 52 | expectType>(findUp(async () => undefined, {cwd: ''})); 53 | expectType>(findUp(async () => undefined, {cwd: new URL('file:///path/to/cwd/')})); 54 | expectType>(findUp(async () => undefined, {allowSymlinks: true})); 55 | expectType>(findUp(async () => undefined, {allowSymlinks: false})); 56 | expectType>(findUp(async () => undefined, {type: 'file'})); 57 | expectType>(findUp(async () => undefined, {type: 'directory'})); 58 | expectType>(findUp(async () => undefined, {stopAt: 'foo'})); 59 | 60 | expectType>(findUp(async (): Promise => findUpStop)); 61 | expectType>(findUp(async (): Promise => findUpStop, {cwd: ''})); 62 | expectType>(findUp(async (): Promise => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); 63 | expectType>(findUp(async (): Promise => findUpStop, {allowSymlinks: true})); 64 | expectType>(findUp(async (): Promise => findUpStop, {allowSymlinks: false})); 65 | expectType>(findUp(async (): Promise => findUpStop, {type: 'file'})); 66 | expectType>(findUp(async (): Promise => findUpStop, {type: 'directory'})); 67 | expectType>(findUp(async (): Promise => findUpStop, {stopAt: 'foo'})); 68 | 69 | expectType>(findUpMultiple('unicorn.png')); 70 | expectType>(findUpMultiple('unicorn.png', {cwd: ''})); 71 | expectType>(findUpMultiple('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); 72 | expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'])); 73 | expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {cwd: ''})); 74 | expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')})); 75 | expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); 76 | expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); 77 | expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {type: 'file'})); 78 | expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {type: 'directory'})); 79 | expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'})); 80 | expectError(findUpMultiple(['rainbow.png', 'unicorn.png'], {concurrency: 1})); 81 | 82 | expectType>(findUpMultiple(() => 'unicorn.png')); 83 | expectType>(findUpMultiple(() => 'unicorn.png', {cwd: ''})); 84 | expectType>(findUpMultiple(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); 85 | expectType>(findUpMultiple(() => 'unicorn.png', {allowSymlinks: true})); 86 | expectType>(findUpMultiple(() => 'unicorn.png', {allowSymlinks: false})); 87 | expectType>(findUpMultiple(() => 'unicorn.png', {type: 'file'})); 88 | expectType>(findUpMultiple(() => 'unicorn.png', {type: 'directory'})); 89 | expectType>(findUpMultiple(() => 'unicorn.png', {stopAt: 'foo'})); 90 | expectType>(findUpMultiple(() => undefined)); 91 | expectType>(findUpMultiple(() => undefined, {cwd: ''})); 92 | expectType>(findUpMultiple(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); 93 | expectType>(findUpMultiple(() => undefined, {allowSymlinks: true})); 94 | expectType>(findUpMultiple(() => undefined, {allowSymlinks: false})); 95 | expectType>(findUpMultiple(() => undefined, {type: 'file'})); 96 | expectType>(findUpMultiple(() => undefined, {type: 'directory'})); 97 | expectType>(findUpMultiple(() => undefined, {stopAt: 'foo'})); 98 | expectType>(findUpMultiple((): typeof findUpStop => findUpStop)); 99 | expectType>(findUpMultiple((): typeof findUpStop => findUpStop, {cwd: ''})); 100 | expectType>(findUpMultiple((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); 101 | expectType>(findUpMultiple((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); 102 | expectType>(findUpMultiple(async () => 'unicorn.png')); 103 | expectType>(findUpMultiple(async () => 'unicorn.png', {cwd: ''})); 104 | expectType>(findUpMultiple(async () => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); 105 | expectType>(findUpMultiple(async () => 'unicorn.png', {allowSymlinks: true})); 106 | expectType>(findUpMultiple(async () => 'unicorn.png', {allowSymlinks: false})); 107 | expectType>(findUpMultiple(async () => 'unicorn.png', {type: 'file'})); 108 | expectType>(findUpMultiple(async () => 'unicorn.png', {type: 'directory'})); 109 | expectType>(findUpMultiple(async () => 'unicorn.png', {stopAt: 'foo'})); 110 | expectType>(findUpMultiple(async () => undefined)); 111 | expectType>(findUpMultiple(async () => undefined, {cwd: ''})); 112 | expectType>(findUpMultiple(async () => undefined, {cwd: new URL('file:///path/to/cwd/')})); 113 | expectType>(findUpMultiple(async () => undefined, {allowSymlinks: true})); 114 | expectType>(findUpMultiple(async () => undefined, {allowSymlinks: false})); 115 | expectType>(findUpMultiple(async () => undefined, {type: 'file'})); 116 | expectType>(findUpMultiple(async () => undefined, {type: 'directory'})); 117 | expectType>(findUpMultiple(async () => undefined, {stopAt: 'foo'})); 118 | 119 | expectType>(findUpMultiple(async (): Promise => findUpStop)); 120 | expectType>(findUpMultiple(async (): Promise => findUpStop, {cwd: ''})); 121 | expectType>(findUpMultiple(async (): Promise => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); 122 | expectType>(findUpMultiple(async (): Promise => findUpStop, {allowSymlinks: true})); 123 | expectType>(findUpMultiple(async (): Promise => findUpStop, {allowSymlinks: false})); 124 | expectType>(findUpMultiple(async (): Promise => findUpStop, {type: 'file'})); 125 | expectType>(findUpMultiple(async (): Promise => findUpStop, {type: 'directory'})); 126 | expectType>(findUpMultiple(async (): Promise => findUpStop, {stopAt: 'foo'})); 127 | 128 | expectType(findUpSync('unicorn.png')); 129 | expectType(findUpSync('unicorn.png', {cwd: ''})); 130 | expectType(findUpSync('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); 131 | expectType(findUpSync(['rainbow.png', 'unicorn.png'])); 132 | expectType(findUpSync(['rainbow.png', 'unicorn.png'], {cwd: ''})); 133 | expectType(findUpSync(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')})); 134 | expectType(findUpSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); 135 | expectType(findUpSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); 136 | expectType(findUpSync(['rainbow.png', 'unicorn.png'], {type: 'file'})); 137 | expectType(findUpSync(['rainbow.png', 'unicorn.png'], {type: 'directory'})); 138 | expectType(findUpSync(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'})); 139 | 140 | expectType(findUpSync(() => 'unicorn.png')); 141 | expectType(findUpSync(() => 'unicorn.png', {cwd: ''})); 142 | expectType(findUpSync(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); 143 | expectType(findUpSync(() => 'unicorn.png', {allowSymlinks: true})); 144 | expectType(findUpSync(() => 'unicorn.png', {allowSymlinks: false})); 145 | expectType(findUpSync(() => 'unicorn.png', {type: 'file'})); 146 | expectType(findUpSync(() => 'unicorn.png', {type: 'directory'})); 147 | expectType(findUpSync(() => 'unicorn.png', {stopAt: 'foo'})); 148 | expectType(findUpSync(() => undefined)); 149 | expectType(findUpSync(() => undefined, {cwd: ''})); 150 | expectType(findUpSync(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); 151 | expectType(findUpSync(() => undefined, {allowSymlinks: true})); 152 | expectType(findUpSync(() => undefined, {allowSymlinks: false})); 153 | expectType(findUpSync(() => undefined, {type: 'file'})); 154 | expectType(findUpSync(() => undefined, {type: 'directory'})); 155 | expectType(findUpSync(() => undefined, {stopAt: 'foo'})); 156 | expectType(findUpSync((): typeof findUpStop => findUpStop)); 157 | expectType(findUpSync((): typeof findUpStop => findUpStop, {cwd: ''})); 158 | expectType(findUpSync((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); 159 | expectType(findUpSync((): typeof findUpStop => findUpStop, {type: 'file'})); 160 | expectType(findUpSync((): typeof findUpStop => findUpStop, {type: 'directory'})); 161 | expectType(findUpSync((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); 162 | 163 | expectType(findUpMultipleSync('unicorn.png')); 164 | expectType(findUpMultipleSync('unicorn.png', {cwd: ''})); 165 | expectType(findUpMultipleSync('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); 166 | expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'])); 167 | expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {cwd: ''})); 168 | expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')})); 169 | expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); 170 | expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); 171 | expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {type: 'file'})); 172 | expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {type: 'directory'})); 173 | expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'})); 174 | expectType(findUpMultipleSync(() => 'unicorn.png')); 175 | expectType(findUpMultipleSync(() => 'unicorn.png', {cwd: ''})); 176 | expectType(findUpMultipleSync(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); 177 | expectType(findUpMultipleSync(() => 'unicorn.png', {allowSymlinks: true})); 178 | expectType(findUpMultipleSync(() => 'unicorn.png', {allowSymlinks: false})); 179 | expectType(findUpMultipleSync(() => 'unicorn.png', {type: 'file'})); 180 | expectType(findUpMultipleSync(() => 'unicorn.png', {type: 'directory'})); 181 | expectType(findUpMultipleSync(() => 'unicorn.png', {stopAt: 'foo'})); 182 | expectType(findUpMultipleSync(() => undefined)); 183 | expectType(findUpMultipleSync(() => undefined, {cwd: ''})); 184 | expectType(findUpMultipleSync(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); 185 | expectType(findUpMultipleSync(() => undefined, {allowSymlinks: true})); 186 | expectType(findUpMultipleSync(() => undefined, {allowSymlinks: false})); 187 | expectType(findUpMultipleSync(() => undefined, {type: 'file'})); 188 | expectType(findUpMultipleSync(() => undefined, {type: 'directory'})); 189 | expectType(findUpMultipleSync(() => undefined, {stopAt: 'foo'})); 190 | expectType(findUpMultipleSync((): typeof findUpStop => findUpStop)); 191 | expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {cwd: ''})); 192 | expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); 193 | expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {type: 'file'})); 194 | expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {type: 'directory'})); 195 | expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); 196 | 197 | expectType>(pathExists('unicorn.png')); 198 | expectType(pathExistsSync('unicorn.png')); 199 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "find-up", 3 | "version": "7.0.0", 4 | "description": "Find a file or directory by walking up parent directories", 5 | "license": "MIT", 6 | "repository": "sindresorhus/find-up", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "find", 31 | "up", 32 | "find-up", 33 | "findup", 34 | "look-up", 35 | "look", 36 | "file", 37 | "search", 38 | "match", 39 | "package", 40 | "resolve", 41 | "parent", 42 | "parents", 43 | "folder", 44 | "directory", 45 | "walk", 46 | "walking", 47 | "path" 48 | ], 49 | "dependencies": { 50 | "locate-path": "^7.2.0", 51 | "path-exists": "^5.0.0", 52 | "unicorn-magic": "^0.1.0" 53 | }, 54 | "devDependencies": { 55 | "ava": "^5.3.1", 56 | "is-path-inside": "^4.0.0", 57 | "tempy": "^3.1.0", 58 | "tsd": "^0.29.0", 59 | "xo": "^0.56.0" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # find-up 2 | 3 | > Find a file or directory by walking up parent directories 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install find-up 9 | ``` 10 | 11 | ## Usage 12 | 13 | ``` 14 | / 15 | └── Users 16 | └── sindresorhus 17 | ├── unicorn.png 18 | └── foo 19 | └── bar 20 | ├── baz 21 | └── example.js 22 | ``` 23 | 24 | `example.js` 25 | 26 | ```js 27 | import path from 'node:path'; 28 | import {findUp, pathExists} from 'find-up'; 29 | 30 | console.log(await findUp('unicorn.png')); 31 | //=> '/Users/sindresorhus/unicorn.png' 32 | 33 | console.log(await findUp(['rainbow.png', 'unicorn.png'])); 34 | //=> '/Users/sindresorhus/unicorn.png' 35 | 36 | console.log(await findUp(async directory => { 37 | const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png')); 38 | return hasUnicorns && directory; 39 | }, {type: 'directory'})); 40 | //=> '/Users/sindresorhus' 41 | ``` 42 | 43 | ## API 44 | 45 | ### findUp(name, options?) 46 | ### findUp(matcher, options?) 47 | 48 | Returns a `Promise` for either the path or `undefined` if it could not be found. 49 | 50 | ### findUp([...name], options?) 51 | 52 | Returns a `Promise` for either the first path found (by respecting the order of the array) or `undefined` if none could be found. 53 | 54 | ### findUpMultiple(name, options?) 55 | ### findUpMultiple(matcher, options?) 56 | 57 | Returns a `Promise` for either an array of paths or an empty array if none could be found. 58 | 59 | ### findUpMultiple([...name], options?) 60 | 61 | 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. 62 | 63 | ### findUpSync(name, options?) 64 | ### findUpSync(matcher, options?) 65 | 66 | Returns a path or `undefined` if it could not be found. 67 | 68 | ### findUpSync([...name], options?) 69 | 70 | Returns the first path found (by respecting the order of the array) or `undefined` if none could be found. 71 | 72 | ### findUpMultipleSync(name, options?) 73 | ### findUpMultipleSync(matcher, options?) 74 | 75 | Returns an array of paths or an empty array if none could be found. 76 | 77 | ### findUpMultipleSync([...name], options?) 78 | 79 | Returns an array of the first paths found (by respecting the order of the array) or an empty array if none could be found. 80 | 81 | #### name 82 | 83 | Type: `string` 84 | 85 | The name of the file or directory to find. 86 | 87 | #### matcher 88 | 89 | Type: `Function` 90 | 91 | 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. 92 | 93 | When using async mode, the `matcher` may optionally be an async or promise-returning function that returns the path. 94 | 95 | #### options 96 | 97 | Type: `object` 98 | 99 | ##### cwd 100 | 101 | Type: `URL | string`\ 102 | Default: `process.cwd()` 103 | 104 | The directory to start from. 105 | 106 | ##### type 107 | 108 | Type: `string`\ 109 | Default: `'file'`\ 110 | Values: `'file' | 'directory'` 111 | 112 | The type of path to match. 113 | 114 | ##### allowSymlinks 115 | 116 | Type: `boolean`\ 117 | Default: `true` 118 | 119 | Allow symbolic links to match if they point to the chosen path type. 120 | 121 | ##### stopAt 122 | 123 | Type: `URL | string`\ 124 | Default: Root directory 125 | 126 | A directory path where the search halts if no matches are found before reaching this point. 127 | 128 | ### pathExists(path) 129 | 130 | Returns a `Promise` of whether the path exists. 131 | 132 | ### pathExistsSync(path) 133 | 134 | Returns a `boolean` of whether the path exists. 135 | 136 | #### path 137 | 138 | Type: `string` 139 | 140 | The path to a file or directory. 141 | 142 | ### findUpStop 143 | 144 | 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. 145 | 146 | ```js 147 | import path from 'node:path'; 148 | import {findUp, findUpStop} from 'find-up'; 149 | 150 | await findUp(directory => { 151 | return path.basename(directory) === 'work' ? findUpStop : 'logo.png'; 152 | }); 153 | ``` 154 | 155 | ## Related 156 | 157 | - [find-up-cli](https://github.com/sindresorhus/find-up-cli) - CLI for this module 158 | - [package-up](https://github.com/sindresorhus/package-up) - Find the closest package.json file 159 | - [pkg-dir](https://github.com/sindresorhus/pkg-dir) - Find the root directory of an npm package 160 | - [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module like `require.resolve()` but from a given path 161 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import {promisify} from 'node:util'; 3 | import fs from 'node:fs'; 4 | import path from 'node:path'; 5 | import {fileURLToPath, pathToFileURL} from 'node:url'; 6 | import test from 'ava'; 7 | import isPathInside from 'is-path-inside'; 8 | import {temporaryDirectory} from 'tempy'; 9 | import { 10 | findUp, 11 | findUpSync, 12 | findUpMultiple, 13 | findUpMultipleSync, 14 | findUpStop, 15 | pathExists, 16 | pathExistsSync, 17 | } from './index.js'; 18 | 19 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); 20 | 21 | const name = { 22 | packageDirectory: 'find-up', 23 | packageJson: 'package.json', 24 | fixtureDirectory: 'fixture', 25 | fooDirectory: 'foo', 26 | barDirectory: 'bar', 27 | modulesDirectory: 'node_modules', 28 | baz: 'baz.js', 29 | qux: 'qux.js', 30 | fileLink: 'file-link', 31 | directoryLink: 'directory-link', 32 | dotDirectory: '.git', 33 | }; 34 | 35 | // These paths are relative to the project root 36 | const relative = { 37 | fixtureDirectory: name.fixtureDirectory, 38 | modulesDirectory: name.modulesDirectory, 39 | }; 40 | relative.baz = path.join(relative.fixtureDirectory, name.baz); 41 | relative.qux = path.join(relative.fixtureDirectory, name.qux); 42 | relative.barDirQux = path.join(relative.fixtureDirectory, name.fooDirectory, name.barDirectory, name.qux); 43 | relative.barDir = path.join(relative.fixtureDirectory, name.fooDirectory, name.barDirectory); 44 | 45 | const absolute = { 46 | packageDirectory: __dirname, 47 | }; 48 | absolute.packageJson = path.join(absolute.packageDirectory, name.packageJson); 49 | absolute.fixtureDirectory = path.join( 50 | absolute.packageDirectory, 51 | name.fixtureDirectory, 52 | ); 53 | absolute.baz = path.join(absolute.fixtureDirectory, name.baz); 54 | absolute.qux = path.join(absolute.fixtureDirectory, name.qux); 55 | absolute.fooDir = path.join(absolute.fixtureDirectory, name.fooDirectory); 56 | absolute.barDir = path.join(absolute.fixtureDirectory, name.fooDirectory, name.barDirectory); 57 | absolute.barDirQux = path.join(absolute.fixtureDirectory, name.fooDirectory, name.barDirectory, name.qux); 58 | absolute.fileLink = path.join(absolute.fixtureDirectory, name.fileLink); 59 | absolute.directoryLink = path.join(absolute.fixtureDirectory, name.directoryLink); 60 | absolute.dotDirectory = path.join(__dirname, name.dotDirectory); 61 | 62 | const url = { 63 | fixtureDirectory: pathToFileURL(absolute.fixtureDirectory), 64 | }; 65 | 66 | // Create a disjoint directory, used for the not-found tests 67 | test.beforeEach(t => { 68 | t.context.disjoint = temporaryDirectory(); 69 | }); 70 | 71 | test.afterEach(t => { 72 | fs.rmdirSync(t.context.disjoint); 73 | }); 74 | 75 | const isWindows = process.platform === 'win32'; 76 | 77 | test('async (child file)', async t => { 78 | const foundPath = await findUp(name.packageJson); 79 | 80 | t.is(foundPath, absolute.packageJson); 81 | }); 82 | 83 | test('sync (child file)', t => { 84 | const foundPath = findUpSync(name.packageJson); 85 | 86 | t.is(foundPath, absolute.packageJson); 87 | }); 88 | 89 | test('async (child directory)', async t => { 90 | const foundPath = await findUp(name.fixtureDirectory, {type: 'directory'}); 91 | 92 | t.is(foundPath, absolute.fixtureDirectory); 93 | }); 94 | 95 | test('sync (child directory)', t => { 96 | const foundPath = findUpSync(name.fixtureDirectory, {type: 'directory'}); 97 | 98 | t.is(foundPath, absolute.fixtureDirectory); 99 | }); 100 | 101 | test('async (explicit type file)', async t => { 102 | t.is(await findUp(name.packageJson, {type: 'file'}), absolute.packageJson); 103 | t.is(await findUp(name.packageJson, {type: 'directory'}), undefined); 104 | }); 105 | 106 | test('sync (explicit type file)', t => { 107 | t.is(findUpSync(name.packageJson, {type: 'file'}), absolute.packageJson); 108 | t.is(findUpSync(name.packageJson, {type: 'directory'}), undefined); 109 | }); 110 | 111 | if (!isWindows) { 112 | test('async (symbolic links)', async t => { 113 | const cwd = absolute.fixtureDirectory; 114 | 115 | t.is(await findUp(name.fileLink, {cwd}), absolute.fileLink); 116 | t.is(await findUp(name.fileLink, {cwd, allowSymlinks: false}), undefined); 117 | 118 | t.is(await findUp(name.directoryLink, {cwd, type: 'directory'}), absolute.directoryLink); 119 | t.is(await findUp(name.directoryLink, {cwd, type: 'directory', allowSymlinks: false}), undefined); 120 | }); 121 | 122 | test('sync (symbolic links)', t => { 123 | const cwd = absolute.fixtureDirectory; 124 | 125 | t.is(findUpSync(name.fileLink, {cwd}), absolute.fileLink); 126 | t.is(findUpSync(name.fileLink, {cwd, allowSymlinks: false}), undefined); 127 | 128 | t.is(findUpSync(name.directoryLink, {cwd, type: 'directory'}), absolute.directoryLink); 129 | t.is(findUpSync(name.directoryLink, {cwd, type: 'directory', allowSymlinks: false}), undefined); 130 | }); 131 | } 132 | 133 | test('async (child file, custom cwd)', async t => { 134 | const foundPath = await findUp(name.baz, { 135 | cwd: relative.fixtureDirectory, 136 | }); 137 | 138 | t.is(foundPath, absolute.baz); 139 | 140 | const foundPath2 = await findUp(name.baz, { 141 | cwd: url.fixtureDirectory, 142 | }); 143 | t.is(foundPath2, foundPath); 144 | }); 145 | 146 | test('sync (child file, custom cwd)', t => { 147 | const foundPath = findUpSync(name.baz, { 148 | cwd: relative.fixtureDirectory, 149 | }); 150 | 151 | t.is(foundPath, absolute.baz); 152 | 153 | const foundPath2 = findUpSync(name.baz, { 154 | cwd: url.fixtureDirectory, 155 | }); 156 | t.is(foundPath2, foundPath); 157 | }); 158 | 159 | test('async (child file, array, custom cwd)', async t => { 160 | const foundPath = await findUp([name.baz], { 161 | cwd: relative.fixtureDirectory, 162 | }); 163 | 164 | t.is(foundPath, absolute.baz); 165 | }); 166 | 167 | test('sync (child file, array, custom cwd)', t => { 168 | const foundPath = findUpSync([name.baz], { 169 | cwd: relative.fixtureDirectory, 170 | }); 171 | 172 | t.is(foundPath, absolute.baz); 173 | }); 174 | 175 | test('async (first child file, array, custom cwd)', async t => { 176 | const foundPath = await findUp([name.qux, name.baz], { 177 | cwd: relative.fixtureDirectory, 178 | }); 179 | 180 | t.is(foundPath, absolute.qux); 181 | }); 182 | 183 | test('sync (first child file, array, custom cwd)', t => { 184 | const foundPath = findUpSync([name.qux, name.baz], { 185 | cwd: relative.fixtureDirectory, 186 | }); 187 | 188 | t.is(foundPath, absolute.qux); 189 | }); 190 | 191 | test('async (second child file, array, custom cwd)', async t => { 192 | const foundPath = await findUp(['fake', name.baz], { 193 | cwd: relative.fixtureDirectory, 194 | }); 195 | 196 | t.is(foundPath, absolute.baz); 197 | }); 198 | 199 | test('sync (second child file, array, custom cwd)', t => { 200 | const foundPath = findUpSync(['fake', name.baz], { 201 | cwd: relative.fixtureDirectory, 202 | }); 203 | 204 | t.is(foundPath, absolute.baz); 205 | }); 206 | 207 | test('async (cwd)', async t => { 208 | const foundPath = await findUp(name.packageDirectory, { 209 | cwd: absolute.packageDirectory, 210 | type: 'directory', 211 | }); 212 | 213 | t.is(foundPath, absolute.packageDirectory); 214 | }); 215 | 216 | test('sync (cwd)', t => { 217 | const foundPath = findUpSync(name.packageDirectory, { 218 | cwd: absolute.packageDirectory, 219 | type: 'directory', 220 | }); 221 | 222 | t.is(foundPath, absolute.packageDirectory); 223 | }); 224 | 225 | test('async (cousin file, custom cwd)', async t => { 226 | const foundPath = await findUp(name.baz, { 227 | cwd: relative.barDir, 228 | }); 229 | 230 | t.is(foundPath, absolute.baz); 231 | }); 232 | 233 | test('sync (cousin file, custom cwd)', t => { 234 | const foundPath = findUpSync(name.baz, { 235 | cwd: relative.barDir, 236 | }); 237 | 238 | t.is(foundPath, absolute.baz); 239 | }); 240 | 241 | test('async (cousin file, custom cwd with stopAt)', async t => { 242 | const foundPath = await findUp(name.baz, { 243 | cwd: relative.barDir, 244 | stopAt: absolute.fooDir, 245 | }); 246 | 247 | t.is(foundPath, undefined); 248 | }); 249 | 250 | test('sync (cousin file, custom cwd with stopAt)', t => { 251 | const foundPath = findUpSync(name.baz, { 252 | cwd: relative.barDir, 253 | stopAt: absolute.fooDir, 254 | }); 255 | 256 | t.is(foundPath, undefined); 257 | }); 258 | 259 | test('async (cousin file, custom cwd, stopAt equal to foundPath)', async t => { 260 | const foundPath = await findUp(name.baz, { 261 | cwd: relative.barDir, 262 | stopAt: absolute.baz, 263 | }); 264 | 265 | t.is(foundPath, absolute.baz); 266 | }); 267 | 268 | test('sync (cousin file, custom cwd, stopAt equal to foundPath)', t => { 269 | const foundPath = findUpSync(name.baz, { 270 | cwd: relative.barDir, 271 | stopAt: absolute.baz, 272 | }); 273 | 274 | t.is(foundPath, absolute.baz); 275 | }); 276 | 277 | test('async (nested descendant file)', async t => { 278 | const foundPath = await findUp(relative.baz); 279 | 280 | t.is(foundPath, absolute.baz); 281 | }); 282 | 283 | test('sync (nested descendant file)', t => { 284 | const foundPath = findUpSync(relative.baz); 285 | 286 | t.is(foundPath, absolute.baz); 287 | }); 288 | 289 | test('async (nested descendant directory)', async t => { 290 | const foundPath = await findUp(relative.barDir, {type: 'directory'}); 291 | 292 | t.is(foundPath, absolute.barDir); 293 | }); 294 | 295 | test('sync (nested descendant directory)', t => { 296 | const foundPath = findUpSync(relative.barDir, {type: 'directory'}); 297 | 298 | t.is(foundPath, absolute.barDir); 299 | }); 300 | 301 | test('async (nested descendant directory, custom cwd)', async t => { 302 | const filePath = await findUp(relative.barDir, { 303 | cwd: relative.modulesDirectory, 304 | type: 'directory', 305 | }); 306 | 307 | t.is(filePath, absolute.barDir); 308 | }); 309 | 310 | test('sync (nested descendant directory, custom cwd)', t => { 311 | const filePath = findUpSync(relative.barDir, { 312 | cwd: relative.modulesDirectory, 313 | type: 'directory', 314 | }); 315 | 316 | t.is(filePath, absolute.barDir); 317 | }); 318 | 319 | test('async (nested cousin directory, custom cwd)', async t => { 320 | const foundPath = await findUp(relative.barDir, { 321 | cwd: relative.fixtureDirectory, 322 | type: 'directory', 323 | }); 324 | 325 | t.is(foundPath, absolute.barDir); 326 | }); 327 | 328 | test('sync (nested cousin directory, custom cwd)', t => { 329 | const foundPath = findUpSync(relative.barDir, { 330 | cwd: relative.fixtureDirectory, 331 | type: 'directory', 332 | }); 333 | 334 | t.is(foundPath, absolute.barDir); 335 | }); 336 | 337 | test('async (ancestor directory, custom cwd)', async t => { 338 | const foundPath = await findUp(name.fixtureDirectory, { 339 | cwd: relative.barDir, 340 | type: 'directory', 341 | }); 342 | 343 | t.is(foundPath, absolute.fixtureDirectory); 344 | }); 345 | 346 | test('sync (ancestor directory, custom cwd)', t => { 347 | const foundPath = findUpSync(name.fixtureDirectory, { 348 | cwd: relative.barDir, 349 | type: 'directory', 350 | }); 351 | 352 | t.is(foundPath, absolute.fixtureDirectory); 353 | }); 354 | 355 | test('async (absolute directory)', async t => { 356 | const filePath = await findUp(absolute.barDir, {type: 'directory'}); 357 | 358 | t.is(filePath, absolute.barDir); 359 | }); 360 | 361 | test('sync (absolute directory)', t => { 362 | const filePath = findUpSync(absolute.barDir, {type: 'directory'}); 363 | 364 | t.is(filePath, absolute.barDir); 365 | }); 366 | 367 | test('async (not found, absolute file)', async t => { 368 | const filePath = await findUp(path.resolve('somenonexistentfile.js')); 369 | 370 | t.is(filePath, undefined); 371 | }); 372 | 373 | test('sync (not found, absolute file)', t => { 374 | const filePath = findUpSync(path.resolve('somenonexistentfile.js')); 375 | 376 | t.is(filePath, undefined); 377 | }); 378 | 379 | test('async (absolute directory, disjoint cwd)', async t => { 380 | const filePath = await findUp(absolute.barDir, { 381 | cwd: t.context.disjoint, 382 | type: 'directory', 383 | }); 384 | 385 | t.is(filePath, absolute.barDir); 386 | }); 387 | 388 | test('sync (absolute directory, disjoint cwd)', t => { 389 | const filePath = findUpSync(absolute.barDir, { 390 | cwd: t.context.disjoint, 391 | type: 'directory', 392 | }); 393 | 394 | t.is(filePath, absolute.barDir); 395 | }); 396 | 397 | test('async (not found)', async t => { 398 | const foundPath = await findUp('somenonexistentfile.js'); 399 | 400 | t.is(foundPath, undefined); 401 | }); 402 | 403 | test('sync (not found)', t => { 404 | const foundPath = findUpSync('somenonexistentfile.js'); 405 | 406 | t.is(foundPath, undefined); 407 | }); 408 | 409 | // Both tests start in a disjoint directory. `package.json` should not be found 410 | // and `undefined` should be returned. 411 | test('async (not found, custom cwd)', async t => { 412 | const foundPath = await findUp(name.packageJson, { 413 | cwd: t.context.disjoint, 414 | }); 415 | 416 | t.is(foundPath, undefined); 417 | }); 418 | 419 | test('sync (not found, custom cwd)', t => { 420 | const foundPath = findUpSync(name.packageJson, { 421 | cwd: t.context.disjoint, 422 | }); 423 | 424 | t.is(foundPath, undefined); 425 | }); 426 | 427 | test('async (dot file)', async t => { 428 | const foundPath = await findUp(name.dotDirectory, {type: 'directory'}); 429 | t.is(foundPath, absolute.dotDirectory); 430 | }); 431 | 432 | test('sync (dot file)', async t => { 433 | const foundPath = await findUp(name.dotDirectory, {type: 'directory'}); 434 | t.is(foundPath, absolute.dotDirectory); 435 | }); 436 | 437 | test('async (matcher function)', async t => { 438 | const cwd = process.cwd(); 439 | 440 | t.is(await findUp(directory => { 441 | t.is(directory, cwd); 442 | return directory; 443 | }, {type: 'directory'}), cwd); 444 | 445 | t.is(await findUp(() => '.', {type: 'directory'}), cwd); 446 | 447 | t.is(await findUp(async () => 'package.json'), path.join(cwd, 'package.json')); 448 | 449 | t.is(await findUp(() => '..', {type: 'directory'}), path.join(cwd, '..')); 450 | 451 | t.is(await findUp(directory => (directory !== cwd) && directory, {type: 'directory'}), path.join(cwd, '..')); 452 | 453 | t.is(await findUp(directory => (directory === cwd) && 'package.json', {cwd: absolute.fixtureDirectory}), absolute.packageJson); 454 | }); 455 | 456 | test('async (not found, matcher function)', async t => { 457 | const cwd = process.cwd(); 458 | const {root} = path.parse(cwd); 459 | const visited = new Set(); 460 | t.is(await findUp(async directory => { 461 | t.is(typeof directory, 'string'); 462 | const stat = await promisify(fs.stat)(directory); 463 | t.true(stat.isDirectory()); 464 | t.true((directory === cwd) || isPathInside(cwd, directory)); 465 | t.false(visited.has(directory)); 466 | visited.add(directory); 467 | }), undefined); 468 | t.true(visited.has(cwd)); 469 | t.true(visited.has(root)); 470 | }); 471 | 472 | test('async (matcher function throws)', async t => { 473 | const cwd = process.cwd(); 474 | const visited = new Set(); 475 | await t.throwsAsync(findUp(directory => { 476 | visited.add(directory); 477 | throw new Error('Some sync throw'); 478 | }), { 479 | message: 'Some sync throw', 480 | }); 481 | t.true(visited.has(cwd)); 482 | t.is(visited.size, 1); 483 | }); 484 | 485 | test('async (matcher function rejects)', async t => { 486 | const cwd = process.cwd(); 487 | const visited = new Set(); 488 | await t.throwsAsync(findUp(async directory => { 489 | visited.add(directory); 490 | throw new Error('Some async rejection'); 491 | }), { 492 | message: 'Some async rejection', 493 | }); 494 | t.true(visited.has(cwd)); 495 | t.is(visited.size, 1); 496 | }); 497 | 498 | test('async (matcher function stops early)', async t => { 499 | const cwd = process.cwd(); 500 | const visited = new Set(); 501 | t.is(await findUp(async directory => { 502 | visited.add(directory); 503 | return findUpStop; 504 | }), undefined); 505 | t.true(visited.has(cwd)); 506 | t.is(visited.size, 1); 507 | }); 508 | 509 | test('sync (matcher function)', t => { 510 | const cwd = process.cwd(); 511 | 512 | t.is(findUpSync(directory => { 513 | t.is(directory, cwd); 514 | return directory; 515 | }, {type: 'directory'}), cwd); 516 | 517 | t.is(findUpSync(() => '.', {type: 'directory'}), cwd); 518 | 519 | t.is(findUpSync(() => 'package.json'), path.join(cwd, 'package.json')); 520 | 521 | t.is(findUpSync(() => '..', {type: 'directory'}), path.join(cwd, '..')); 522 | 523 | t.is(findUpSync(directory => (directory !== cwd) && directory, {type: 'directory'}), path.join(cwd, '..')); 524 | 525 | t.is(findUpSync(directory => (directory === cwd) && 'package.json', {cwd: absolute.fixtureDirectory}), absolute.packageJson); 526 | }); 527 | 528 | test('sync (not found, matcher function)', t => { 529 | const cwd = process.cwd(); 530 | const {root} = path.parse(cwd); 531 | const visited = new Set(); 532 | t.is(findUpSync(directory => { 533 | t.is(typeof directory, 'string'); 534 | const stat = fs.statSync(directory); 535 | t.true(stat.isDirectory()); 536 | t.true((directory === cwd) || isPathInside(cwd, directory)); 537 | t.false(visited.has(directory)); 538 | visited.add(directory); 539 | }), undefined); 540 | t.true(visited.has(cwd)); 541 | t.true(visited.has(root)); 542 | }); 543 | 544 | test('sync (matcher function throws)', t => { 545 | const cwd = process.cwd(); 546 | const visited = new Set(); 547 | t.throws(() => { 548 | findUpSync(directory => { 549 | visited.add(directory); 550 | throw new Error('Some problem'); 551 | }); 552 | }, { 553 | message: 'Some problem', 554 | }); 555 | t.true(visited.has(cwd)); 556 | t.is(visited.size, 1); 557 | }); 558 | 559 | test('sync (matcher function stops early)', t => { 560 | const cwd = process.cwd(); 561 | const visited = new Set(); 562 | t.is(findUpSync(directory => { 563 | visited.add(directory); 564 | return findUpStop; 565 | }), undefined); 566 | t.true(visited.has(cwd)); 567 | t.is(visited.size, 1); 568 | }); 569 | 570 | test('async (check if path exists)', async t => { 571 | if (!isWindows) { 572 | t.true(await pathExists(absolute.directoryLink)); 573 | t.true(await pathExists(absolute.fileLink)); 574 | } 575 | 576 | t.true(await pathExists(absolute.barDir)); 577 | t.true(await pathExists(absolute.packageJson)); 578 | t.false(await pathExists('fake')); 579 | }); 580 | 581 | test('sync (check if path exists)', t => { 582 | if (!isWindows) { 583 | t.true(pathExistsSync(absolute.directoryLink)); 584 | t.true(pathExistsSync(absolute.fileLink)); 585 | } 586 | 587 | t.true(pathExistsSync(absolute.barDir)); 588 | t.true(pathExistsSync(absolute.packageJson)); 589 | t.false(pathExistsSync('fake')); 590 | }); 591 | 592 | test('async multiple (child file)', async t => { 593 | const filePaths = await findUpMultiple(name.qux, {cwd: relative.barDir}); 594 | 595 | t.deepEqual(filePaths, [absolute.barDirQux, absolute.qux]); 596 | }); 597 | 598 | test('sync multiple (child file)', t => { 599 | const filePaths = findUpMultipleSync(name.qux, {cwd: relative.barDir}); 600 | 601 | t.deepEqual(filePaths, [absolute.barDirQux, absolute.qux]); 602 | }); 603 | 604 | test('async multiple (child directory)', async t => { 605 | const foundPath = await findUpMultiple(name.fixtureDirectory, {type: 'directory'}); 606 | 607 | t.deepEqual(foundPath, [absolute.fixtureDirectory]); 608 | }); 609 | 610 | test('sync multiple (child directory)', t => { 611 | const foundPath = findUpMultipleSync(name.fixtureDirectory, {type: 'directory'}); 612 | 613 | t.deepEqual(foundPath, [absolute.fixtureDirectory]); 614 | }); 615 | 616 | test('async multiple (child file, array)', async t => { 617 | const filePaths = await findUpMultiple([name.baz, name.qux], {cwd: relative.barDir}); 618 | 619 | t.deepEqual(filePaths, [absolute.barDirQux, absolute.baz]); 620 | }); 621 | 622 | test('sync multiple (child file, array)', t => { 623 | const filePaths = findUpMultipleSync([name.baz, name.qux], {cwd: relative.barDir}); 624 | 625 | t.deepEqual(filePaths, [absolute.barDirQux, absolute.baz]); 626 | }); 627 | 628 | test('async multiple (child directory, custom cwd, array)', async t => { 629 | const foundPath = await findUpMultiple([name.fixtureDirectory, name.fooDirectory], { 630 | cwd: absolute.barDir, 631 | type: 'directory', 632 | }); 633 | 634 | t.deepEqual(foundPath, [absolute.fooDir, absolute.fixtureDirectory]); 635 | }); 636 | 637 | test('sync multiple (child directory, custom cwd, array)', t => { 638 | const foundPath = findUpMultipleSync([name.fixtureDirectory, name.fooDirectory], { 639 | cwd: absolute.barDir, 640 | type: 'directory', 641 | }); 642 | 643 | t.deepEqual(foundPath, [absolute.fooDir, absolute.fixtureDirectory]); 644 | }); 645 | 646 | test('async multiple (child file with stopAt)', async t => { 647 | const filePaths = await findUpMultiple(name.qux, { 648 | cwd: relative.barDir, 649 | stopAt: absolute.fooDir, 650 | }); 651 | 652 | t.deepEqual(filePaths, [absolute.barDirQux]); 653 | }); 654 | 655 | test('sync multiple (child file with stopAt)', t => { 656 | const filePaths = findUpMultipleSync(name.qux, { 657 | cwd: relative.barDir, 658 | stopAt: absolute.fooDir, 659 | }); 660 | 661 | t.deepEqual(filePaths, [absolute.barDirQux]); 662 | }); 663 | 664 | test('async multiple (not found, child file)', async t => { 665 | const filePaths = await findUpMultiple('somenonexistentfile.js', {cwd: relative.barDir}); 666 | 667 | t.deepEqual(filePaths, []); 668 | }); 669 | 670 | test('sync multiple (not found, child file)', t => { 671 | const filePaths = findUpMultipleSync('somenonexistentfile.js', {cwd: relative.barDir}); 672 | 673 | t.deepEqual(filePaths, []); 674 | }); 675 | --------------------------------------------------------------------------------