pax_global_header00006660000000000000000000000064135016300560014511gustar00rootroot0000000000000052 comment=6c32f0caed1684ef778053b6f79b13a772e22ba4 find-up-4.1.0/000077500000000000000000000000001350163005600130555ustar00rootroot00000000000000find-up-4.1.0/.editorconfig000066400000000000000000000002571350163005600155360ustar00rootroot00000000000000root = 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-4.1.0/.gitattributes000066400000000000000000000000231350163005600157430ustar00rootroot00000000000000* text=auto eol=lf find-up-4.1.0/.github/000077500000000000000000000000001350163005600144155ustar00rootroot00000000000000find-up-4.1.0/.github/funding.yml000066400000000000000000000000531350163005600165700ustar00rootroot00000000000000github: sindresorhus tidelift: npm/find-up find-up-4.1.0/.github/security.md000066400000000000000000000002631350163005600166070ustar00rootroot00000000000000# 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-4.1.0/.gitignore000066400000000000000000000000271350163005600150440ustar00rootroot00000000000000node_modules yarn.lock find-up-4.1.0/.npmrc000066400000000000000000000000231350163005600141700ustar00rootroot00000000000000package-lock=false find-up-4.1.0/.travis.yml000066400000000000000000000001271350163005600151660ustar00rootroot00000000000000os: - linux - osx - windows language: node_js node_js: - '12' - '10' - '8' find-up-4.1.0/fixture/000077500000000000000000000000001350163005600145435ustar00rootroot00000000000000find-up-4.1.0/fixture/baz.js000066400000000000000000000000001350163005600156430ustar00rootroot00000000000000find-up-4.1.0/fixture/directory-link000077700000000000000000000000001350163005600175002.ustar00rootroot00000000000000find-up-4.1.0/fixture/file-link000077700000000000000000000000001350163005600174452baz.jsustar00rootroot00000000000000find-up-4.1.0/fixture/foo/000077500000000000000000000000001350163005600153265ustar00rootroot00000000000000find-up-4.1.0/fixture/foo/bar/000077500000000000000000000000001350163005600160725ustar00rootroot00000000000000find-up-4.1.0/fixture/foo/bar/.gitkeep000066400000000000000000000000001350163005600175110ustar00rootroot00000000000000find-up-4.1.0/fixture/qux.js000066400000000000000000000000001350163005600157040ustar00rootroot00000000000000find-up-4.1.0/index.d.ts000066400000000000000000000071341350163005600147630ustar00rootroot00000000000000import {Options as LocatePathOptions} from 'locate-path'; declare const stop: unique symbol; declare namespace findUp { interface Options extends LocatePathOptions {} type StopSymbol = typeof stop; type Match = string | StopSymbol | undefined; } declare const findUp: { /** Find a file or directory by walking up parent directories. @param name - 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 = require('find-up'); (async () => { console.log(await findUp('unicorn.png')); //=> '/Users/sindresorhus/unicorn.png' console.log(await findUp(['rainbow.png', 'unicorn.png'])); //=> '/Users/sindresorhus/unicorn.png' })(); ``` */ (name: string | string[], options?: findUp.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 `findUp.stop` to stop the search. @returns The first path found or `undefined` if none could be found. @example ``` import path = require('path'); import findUp = require('find-up'); (async () => { console.log(await findUp(async directory => { const hasUnicorns = await findUp.exists(path.join(directory, 'unicorn.png')); return hasUnicorns && directory; }, {type: 'directory'})); //=> '/Users/sindresorhus' })(); ``` */ (matcher: (directory: string) => (findUp.Match | Promise), options?: findUp.Options): Promise; sync: { /** Synchronously find a file or directory by walking up parent directories. @param name - 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. */ (name: string | string[], options?: findUp.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 `findUp.stop` to stop the search. @returns The first path found or `undefined` if none could be found. @example ``` import path = require('path'); import findUp = require('find-up'); console.log(findUp.sync(directory => { const hasUnicorns = findUp.sync.exists(path.join(directory, 'unicorn.png')); return hasUnicorns && directory; }, {type: 'directory'})); //=> '/Users/sindresorhus' ``` */ (matcher: (directory: string) => findUp.Match, options?: findUp.Options): string | undefined; /** Synchronously check if a path exists. @param path - Path to the file or directory. @returns Whether the path exists. @example ``` import findUp = require('find-up'); console.log(findUp.sync.exists('/Users/sindresorhus/unicorn.png')); //=> true ``` */ exists(path: string): boolean; } /** Check if a path exists. @param path - Path to a file or directory. @returns Whether the path exists. @example ``` import findUp = require('find-up'); (async () => { console.log(await findUp.exists('/Users/sindresorhus/unicorn.png')); //=> true })(); ``` */ exists(path: string): Promise; /** Return this in a `matcher` function to stop the search and force `findUp` to immediately return `undefined`. */ readonly stop: findUp.StopSymbol; }; export = findUp; find-up-4.1.0/index.js000066400000000000000000000036241350163005600145270ustar00rootroot00000000000000'use strict'; const path = require('path'); const locatePath = require('locate-path'); const pathExists = require('path-exists'); const stop = Symbol('findUp.stop'); module.exports = async (name, options = {}) => { let directory = path.resolve(options.cwd || ''); const {root} = path.parse(directory); const paths = [].concat(name); 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; }; // 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 === stop) { return; } if (foundPath) { return path.resolve(directory, foundPath); } if (directory === root) { return; } directory = path.dirname(directory); } }; module.exports.sync = (name, options = {}) => { let directory = path.resolve(options.cwd || ''); const {root} = path.parse(directory); const paths = [].concat(name); const runMatcher = locateOptions => { if (typeof name !== 'function') { return locatePath.sync(paths, locateOptions); } const foundPath = name(locateOptions.cwd); if (typeof foundPath === 'string') { return locatePath.sync([foundPath], locateOptions); } return foundPath; }; // eslint-disable-next-line no-constant-condition while (true) { const foundPath = runMatcher({...options, cwd: directory}); if (foundPath === stop) { return; } if (foundPath) { return path.resolve(directory, foundPath); } if (directory === root) { return; } directory = path.dirname(directory); } }; module.exports.exists = pathExists; module.exports.sync.exists = pathExists.sync; module.exports.stop = stop; find-up-4.1.0/index.test-d.ts000066400000000000000000000136071350163005600157420ustar00rootroot00000000000000import {expectType, expectError} from 'tsd'; import findUp = require('.'); expectType>(findUp('unicorn.png')); expectType>(findUp('unicorn.png', {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'})); expectError(findUp(['rainbow.png', 'unicorn.png'], {concurrency: 1})) expectType>(findUp(() => 'unicorn.png')); expectType>(findUp(() => 'unicorn.png', {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(() => undefined)); expectType>(findUp(() => undefined, {cwd: ''})); expectType>(findUp(() => undefined, {allowSymlinks: true})); expectType>(findUp(() => undefined, {allowSymlinks: false})); expectType>(findUp(() => undefined, {type: 'file'})); expectType>(findUp(() => undefined, {type: 'directory'})); expectType>(findUp((): findUp.StopSymbol => findUp.stop)); expectType>(findUp((): findUp.StopSymbol => findUp.stop, {cwd: ''})); 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 () => undefined)); expectType>(findUp(async () => undefined, {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 (): Promise => findUp.stop)); expectType>(findUp(async (): Promise => findUp.stop, {cwd: ''})); expectType>(findUp(async (): Promise => findUp.stop, {allowSymlinks: true})); expectType>(findUp(async (): Promise => findUp.stop, {allowSymlinks: false})); expectType>(findUp(async (): Promise => findUp.stop, {type: 'file'})); expectType>(findUp(async (): Promise => findUp.stop, {type: 'directory'})); expectType(findUp.sync('unicorn.png')); expectType(findUp.sync('unicorn.png', {cwd: ''})); expectType(findUp.sync(['rainbow.png', 'unicorn.png'])); expectType(findUp.sync(['rainbow.png', 'unicorn.png'], {cwd: ''})); expectType(findUp.sync(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); expectType(findUp.sync(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); expectType(findUp.sync(['rainbow.png', 'unicorn.png'], {type: 'file'})); expectType(findUp.sync(['rainbow.png', 'unicorn.png'], {type: 'directory'})); expectType(findUp.sync(() => 'unicorn.png')); expectType(findUp.sync(() => 'unicorn.png', {cwd: ''})); expectType(findUp.sync(() => 'unicorn.png', {allowSymlinks: true})); expectType(findUp.sync(() => 'unicorn.png', {allowSymlinks: false})); expectType(findUp.sync(() => 'unicorn.png', {type: 'file'})); expectType(findUp.sync(() => 'unicorn.png', {type: 'directory'})); expectType(findUp.sync(() => undefined)); expectType(findUp.sync(() => undefined, {cwd: ''})); expectType(findUp.sync(() => undefined, {allowSymlinks: true})); expectType(findUp.sync(() => undefined, {allowSymlinks: false})); expectType(findUp.sync(() => undefined, {type: 'file'})); expectType(findUp.sync(() => undefined, {type: 'directory'})); expectType(findUp.sync((): findUp.StopSymbol => findUp.stop)); expectType(findUp.sync((): findUp.StopSymbol => findUp.stop, {cwd: ''})); expectType(findUp.sync((): findUp.StopSymbol => findUp.stop, {type: 'file'})); expectType(findUp.sync((): findUp.StopSymbol => findUp.stop, {type: 'directory'})); expectType>(findUp.exists('unicorn.png')); expectType(findUp.sync.exists('unicorn.png')); expectType(findUp.stop); find-up-4.1.0/license000066400000000000000000000021251350163005600144220ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (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-4.1.0/package.json000066400000000000000000000015231350163005600153440ustar00rootroot00000000000000{ "name": "find-up", "version": "4.1.0", "description": "Find a file or directory by walking up parent directories", "license": "MIT", "repository": "sindresorhus/find-up", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "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": "^5.0.0", "path-exists": "^4.0.0" }, "devDependencies": { "ava": "^2.1.0", "is-path-inside": "^2.1.0", "tempy": "^0.3.0", "tsd": "^0.7.3", "xo": "^0.24.0" } } find-up-4.1.0/readme.md000066400000000000000000000077031350163005600146430ustar00rootroot00000000000000# find-up [![Build Status](https://travis-ci.org/sindresorhus/find-up.svg?branch=master)](https://travis-ci.org/sindresorhus/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 const path = require('path'); const findUp = require('find-up'); (async () => { 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 findUp.exists(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. ### findUp.sync(name, options?) ### findUp.sync(matcher, options?) Returns a path or `undefined` if it couldn't be found. ### findUp.sync([...name], options?) Returns the first path found (by respecting the order of the array) or `undefined` if none could be found. #### name Type: `string` 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: `string`
Default: `process.cwd()` 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. ### findUp.exists(path) Returns a `Promise` of whether the path exists. ### findUp.sync.exists(path) Returns a `boolean` of whether the path exists. #### path Type: `string` Path to a file or directory. ### findUp.stop 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 const path = require('path'); const findUp = require('find-up'); (async () => { await findUp(directory => { return path.basename(directory) === 'work' ? findUp.stop : '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-4.1.0/test.js000066400000000000000000000327311350163005600144000ustar00rootroot00000000000000import fs from 'fs'; import path from 'path'; import {promisify} from 'util'; import test from 'ava'; import isPathInside from 'is-path-inside'; import tempy from 'tempy'; import findUp from '.'; const name = { packageDirectory: 'find-up', packageJson: 'package.json', fixtureDirectory: 'fixture', 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.barDir = path.join(relative.fixtureDirectory, 'foo', 'bar'); 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.barDir = path.join(absolute.fixtureDirectory, 'foo', 'bar'); absolute.fileLink = path.join(absolute.fixtureDirectory, name.fileLink); absolute.directoryLink = path.join(absolute.fixtureDirectory, name.directoryLink); // Create a disjoint directory, used for the not-found tests test.beforeEach(t => { const tmpDir = tempy.directory(); t.context.disjoint = tmpDir; }); 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 = findUp.sync(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 = findUp.sync(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(findUp.sync(name.packageJson, {type: 'file'}), absolute.packageJson); t.is(findUp.sync(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(findUp.sync(name.fileLink, {cwd}), absolute.fileLink); t.is(findUp.sync(name.fileLink, {cwd, allowSymlinks: false}), undefined); t.is(findUp.sync(name.directoryLink, {cwd, type: 'directory'}), absolute.directoryLink); t.is(findUp.sync(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); }); test('sync (child file, custom cwd)', t => { const foundPath = findUp.sync(name.baz, { cwd: relative.fixtureDirectory }); t.is(foundPath, absolute.baz); }); 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 = findUp.sync([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 = findUp.sync([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 = findUp.sync(['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 = findUp.sync(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 = findUp.sync(name.baz, { cwd: relative.barDir }); 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 = findUp.sync(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 = findUp.sync(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 = findUp.sync(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 = findUp.sync(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 = findUp.sync(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 = findUp.sync(absolute.barDir, {type: 'directory'}); t.is(filePath, absolute.barDir); }); 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 = findUp.sync(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 = findUp.sync(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 = findUp.sync('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 = findUp.sync(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(() => { return '.'; }, {type: 'directory'}), cwd); t.is(await findUp(async () => { return 'package.json'; }), path.join(cwd, 'package.json')); t.is(await findUp(() => { return '..'; }, {type: 'directory'}), path.join(cwd, '..')); t.is(await findUp(directory => { return (directory !== cwd) && directory; }, {type: 'directory'}), path.join(cwd, '..')); t.is(await findUp(directory => { return (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 findUp.stop; }), undefined); t.true(visited.has(cwd)); t.is(visited.size, 1); }); test('sync (matcher function)', t => { const cwd = process.cwd(); t.is(findUp.sync(directory => { t.is(directory, cwd); return directory; }, {type: 'directory'}), cwd); t.is(findUp.sync(() => { return '.'; }, {type: 'directory'}), cwd); t.is(findUp.sync(() => { return 'package.json'; }), path.join(cwd, 'package.json')); t.is(findUp.sync(() => { return '..'; }, {type: 'directory'}), path.join(cwd, '..')); t.is(findUp.sync(directory => { return (directory !== cwd) && directory; }, {type: 'directory'}), path.join(cwd, '..')); t.is(findUp.sync(directory => { return (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(findUp.sync(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(() => { findUp.sync(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(findUp.sync(directory => { visited.add(directory); return findUp.stop; }), undefined); t.true(visited.has(cwd)); t.is(visited.size, 1); }); test('async (check if path exists)', async t => { if (!isWindows) { t.true(await findUp.exists(absolute.directoryLink)); t.true(await findUp.exists(absolute.fileLink)); } t.true(await findUp.exists(absolute.barDir)); t.true(await findUp.exists(absolute.packageJson)); t.false(await findUp.exists('fake')); }); test('sync (check if path exists)', t => { if (!isWindows) { t.true(findUp.sync.exists(absolute.directoryLink)); t.true(findUp.sync.exists(absolute.fileLink)); } t.true(findUp.sync.exists(absolute.barDir)); t.true(findUp.sync.exists(absolute.packageJson)); t.false(findUp.sync.exists('fake')); });