pax_global_header00006660000000000000000000000064142472555660014532gustar00rootroot0000000000000052 comment=4a5368c38e808a62d86010950e6c1ff2d856b05b locate-path-7.1.1/000077500000000000000000000000001424725556600137415ustar00rootroot00000000000000locate-path-7.1.1/.editorconfig000066400000000000000000000002571424725556600164220ustar00rootroot00000000000000root = 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 locate-path-7.1.1/.gitattributes000066400000000000000000000000231424725556600166270ustar00rootroot00000000000000* text=auto eol=lf locate-path-7.1.1/.github/000077500000000000000000000000001424725556600153015ustar00rootroot00000000000000locate-path-7.1.1/.github/funding.yml000066400000000000000000000001651424725556600174600ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/locate-path custom: https://sindresorhus.com/donate locate-path-7.1.1/.github/security.md000066400000000000000000000002631424725556600174730ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. locate-path-7.1.1/.github/workflows/000077500000000000000000000000001424725556600173365ustar00rootroot00000000000000locate-path-7.1.1/.github/workflows/main.yml000066400000000000000000000006261424725556600210110ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 16 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test locate-path-7.1.1/.gitignore000066400000000000000000000000271424725556600157300ustar00rootroot00000000000000node_modules yarn.lock locate-path-7.1.1/.npmrc000066400000000000000000000000231424725556600150540ustar00rootroot00000000000000package-lock=false locate-path-7.1.1/fixture/000077500000000000000000000000001424725556600154275ustar00rootroot00000000000000locate-path-7.1.1/fixture/directory-link000077700000000000000000000000001424725556600203642.ustar00rootroot00000000000000locate-path-7.1.1/fixture/file-link000077700000000000000000000000001424725556600206172unicornustar00rootroot00000000000000locate-path-7.1.1/fixture/unicorn000066400000000000000000000000001424725556600170150ustar00rootroot00000000000000locate-path-7.1.1/index.d.ts000066400000000000000000000032331424725556600156430ustar00rootroot00000000000000export interface Options { /** The current working directory. @default process.cwd() */ readonly cwd?: URL | string; /** The type of path to match. @default 'file' */ readonly type?: 'file' | 'directory'; /** Allow symbolic links to match if they point to the requested path type. @default true */ readonly allowSymlinks?: boolean; } export interface AsyncOptions extends Options { /** The number of concurrently pending promises. Minimum: `1` @default Infinity */ readonly concurrency?: number; /** Preserve `paths` order when searching. Disable this to improve performance if you don't care about the order. @default true */ readonly preserveOrder?: boolean; } /** Get the first path that exists on disk of multiple paths. @param paths - The paths to check. @returns The first path that exists or `undefined` if none exists. @example ``` import {locatePath} from 'locate-path'; const files = [ 'unicorn.png', 'rainbow.png', // Only this one actually exists on disk 'pony.png' ]; console(await locatePath(files)); //=> 'rainbow' ``` */ export function locatePath( paths: Iterable, options?: AsyncOptions ): Promise; /** Synchronously get the first path that exists on disk of multiple paths. @param paths - The paths to check. @returns The first path that exists or `undefined` if none exists. @example ``` import {locatePathSync} from 'locate-path'; const files = [ 'unicorn.png', 'rainbow.png', // Only this one actually exists on disk 'pony.png' ]; console(locatePathSync(files)); //=> 'rainbow' ``` */ export function locatePathSync( paths: Iterable, options?: Options ): string | undefined; locate-path-7.1.1/index.js000066400000000000000000000026471424725556600154170ustar00rootroot00000000000000import process from 'node:process'; import path from 'node:path'; import fs, {promises as fsPromises} from 'node:fs'; import {fileURLToPath} from 'node:url'; import pLocate from 'p-locate'; const typeMappings = { directory: 'isDirectory', file: 'isFile', }; function checkType(type) { if (Object.hasOwnProperty.call(typeMappings, type)) { return; } throw new Error(`Invalid type specified: ${type}`); } const matchType = (type, stat) => stat[typeMappings[type]](); const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; export async function locatePath( paths, { cwd = process.cwd(), type = 'file', allowSymlinks = true, concurrency, preserveOrder, } = {}, ) { checkType(type); cwd = toPath(cwd); const statFunction = allowSymlinks ? fsPromises.stat : fsPromises.lstat; return pLocate(paths, async path_ => { try { const stat = await statFunction(path.resolve(cwd, path_)); return matchType(type, stat); } catch { return false; } }, {concurrency, preserveOrder}); } export function locatePathSync( paths, { cwd = process.cwd(), type = 'file', allowSymlinks = true, } = {}, ) { checkType(type); cwd = toPath(cwd); const statFunction = allowSymlinks ? fs.statSync : fs.lstatSync; for (const path_ of paths) { try { const stat = statFunction(path.resolve(cwd, path_)); if (matchType(type, stat)) { return path_; } } catch {} } } locate-path-7.1.1/index.test-d.ts000066400000000000000000000025611424725556600166230ustar00rootroot00000000000000import {expectType} from 'tsd'; import {locatePath, locatePathSync} from './index.js'; const files = [ 'unicorn.png', 'rainbow.png', 'pony.png', ]; expectType>(locatePath(files)); expectType>(locatePath(files, {concurrency: 2})); expectType>(locatePath(files, {preserveOrder: false})); expectType>(locatePath(files, {cwd: '.'})); expectType>(locatePath(files, {cwd: new URL('file:///path/to/cwd/')})); expectType>(locatePath(files, {type: 'file'})); expectType>(locatePath(files, {type: 'directory'})); expectType>(locatePath(files, {allowSymlinks: true})); expectType>(locatePath(files, {allowSymlinks: false})); expectType(locatePathSync(files)); expectType(locatePathSync(files, {cwd: '.'})); expectType(locatePathSync(files, {cwd: new URL('file:///path/to/cwd/')})); expectType(locatePathSync(files, {type: 'file'})); expectType(locatePathSync(files, {type: 'directory'})); expectType(locatePathSync(files, {allowSymlinks: true})); expectType(locatePathSync(files, {allowSymlinks: false})); locate-path-7.1.1/license000066400000000000000000000021351424725556600153070ustar00rootroot00000000000000MIT 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. locate-path-7.1.1/package.json000066400000000000000000000015341424725556600162320ustar00rootroot00000000000000{ "name": "locate-path", "version": "7.1.1", "description": "Get the first path that exists on disk of multiple paths", "license": "MIT", "repository": "sindresorhus/locate-path", "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": [ "locate", "path", "paths", "file", "files", "exists", "find", "finder", "search", "searcher", "array", "iterable", "iterator" ], "dependencies": { "p-locate": "^6.0.0" }, "devDependencies": { "ava": "^3.15.0", "tsd": "^0.17.0", "xo": "^0.44.0" } } locate-path-7.1.1/readme.md000066400000000000000000000040151424725556600155200ustar00rootroot00000000000000# locate-path > Get the first path that exists on disk of multiple paths ## Install ``` $ npm install locate-path ``` ## Usage Here we find the first file that exists on disk, in array order. ```js import {locatePath} from 'locate-path'; const files = [ 'unicorn.png', 'rainbow.png', // Only this one actually exists on disk 'pony.png' ]; console(await locatePath(files)); //=> 'rainbow' ``` ## API ### locatePath(paths, options?) Returns a `Promise` for the first path that exists or `undefined` if none exists. #### paths Type: `Iterable` The paths to check. #### options Type: `object` ##### concurrency Type: `number`\ Default: `Infinity`\ Minimum: `1` The number of concurrently pending promises. ##### preserveOrder Type: `boolean`\ Default: `true` Preserve `paths` order when searching. Disable this to improve performance if you don't care about the order. ##### cwd Type: `URL | string`\ Default: `process.cwd()` The current working directory. ##### 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. ### locatePathSync(paths, options?) Returns the first path that exists or `undefined` if none exists. #### paths Type: `Iterable` The paths to check. #### options Type: `object` ##### cwd Same as above. ##### type Same as above. ##### allowSymlinks Same as above. ## Related - [path-exists](https://github.com/sindresorhus/path-exists) - Check if a path exists ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
locate-path-7.1.1/test.js000066400000000000000000000063531424725556600152650ustar00rootroot00000000000000import process from 'node:process'; import test from 'ava'; import {locatePath, locatePathSync} from './index.js'; const paths = [ 'noop.foo', 'unicorn.png', 'index.js', 'test.js', ]; test('async', async t => { t.is(await locatePath(paths), 'index.js'); t.is(await locatePath(['nonexistant']), undefined); t.is(await locatePath(['noop', 'unicorn'], {cwd: 'fixture'}), 'unicorn'); t.is(await locatePath(['noop', 'unicorn'], {cwd: new URL('fixture', import.meta.url)}), 'unicorn'); t.is(await locatePath(['index.js'], {type: 'directory'}), undefined); t.is(await locatePath(['fixture'], {type: 'file'}), undefined); t.is(await locatePath(['fixture']), undefined); t.is(await locatePath(['fixture'], {type: 'directory'}), 'fixture'); await t.throwsAsync(locatePath(['fixture'], {type: 'rainbows'}), { instanceOf: Error, message: 'Invalid type specified: rainbows', }); await t.throwsAsync(locatePath(['fixture'], {type: 'toString'}), { instanceOf: Error, message: 'Invalid type specified: toString', }); await t.throwsAsync(locatePath(['fixture'], {type: 1}), { instanceOf: Error, message: 'Invalid type specified: 1', }); if (process.platform !== 'win32') { t.is(await locatePath(['file-link', 'unicorn'], {cwd: 'fixture', type: 'file'}), 'file-link'); t.is(await locatePath(['directory-link', 'unicorn'], {cwd: 'fixture', type: 'file'}), 'unicorn'); t.is(await locatePath(['directory-link', 'unicorn'], {cwd: 'fixture', type: 'directory'}), 'directory-link'); t.is(await locatePath(['file-link', 'unicorn'], {cwd: 'fixture', allowSymlinks: false, type: 'file'}), 'unicorn'); t.is(await locatePath(['directory-link', 'unicorn'], {cwd: 'fixture', allowSymlinks: false, type: 'directory'}), undefined); } }); test('sync', t => { t.is(locatePathSync(paths), 'index.js'); t.is(locatePathSync(['nonexistant']), undefined); t.is(locatePathSync(['noop', 'unicorn'], {cwd: 'fixture'}), 'unicorn'); t.is(locatePathSync(['noop', 'unicorn'], {cwd: new URL('fixture', import.meta.url)}), 'unicorn'); t.is(locatePathSync(['index.js'], {type: 'directory'}), undefined); t.is(locatePathSync(['fixture'], {type: 'file'}), undefined); t.is(locatePathSync(['fixture']), undefined); t.is(locatePathSync(['fixture'], {type: 'directory'}), 'fixture'); t.throws(() => { locatePathSync(['fixture'], {type: 'rainbows'}); }, { instanceOf: Error, message: 'Invalid type specified: rainbows', }); t.throws(() => { locatePathSync(['fixture'], {type: 'toString'}); }, { instanceOf: Error, message: 'Invalid type specified: toString', }); t.throws(() => { locatePathSync(['fixture'], {type: 1}); }, { instanceOf: Error, message: 'Invalid type specified: 1', }); if (process.platform !== 'win32') { t.is(locatePathSync(['file-link', 'unicorn'], {cwd: 'fixture', type: 'file'}), 'file-link'); t.is(locatePathSync(['directory-link', 'unicorn'], {cwd: 'fixture', type: 'file'}), 'unicorn'); t.is(locatePathSync(['directory-link', 'unicorn'], {cwd: 'fixture', type: 'directory'}), 'directory-link'); t.is(locatePathSync(['file-link', 'unicorn'], {cwd: 'fixture', allowSymlinks: false, type: 'file'}), 'unicorn'); t.is(locatePathSync(['directory-link', 'unicorn'], {cwd: 'fixture', allowSymlinks: false, type: 'directory'}), undefined); } });