locate-path
Advanced tools
Comparing version 6.0.0 to 7.0.0
137
index.d.ts
@@ -1,83 +0,92 @@ | ||
declare namespace locatePath { | ||
interface Options { | ||
/** | ||
Current working directory. | ||
export interface Options { | ||
/** | ||
The current working directory. | ||
@default process.cwd() | ||
*/ | ||
readonly cwd?: string; | ||
@default process.cwd() | ||
*/ | ||
readonly cwd?: string; | ||
/** | ||
Type of path to match. | ||
/** | ||
The type of path to match. | ||
@default 'file' | ||
*/ | ||
readonly type?: 'file' | 'directory'; | ||
@default 'file' | ||
*/ | ||
readonly type?: 'file' | 'directory'; | ||
/** | ||
Allow symbolic links to match if they point to the requested path type. | ||
/** | ||
Allow symbolic links to match if they point to the requested path type. | ||
@default true | ||
*/ | ||
readonly allowSymlinks?: boolean; | ||
} | ||
@default true | ||
*/ | ||
readonly allowSymlinks?: boolean; | ||
} | ||
interface AsyncOptions extends Options { | ||
/** | ||
Number of concurrently pending promises. Minimum: `1`. | ||
export interface AsyncOptions extends Options { | ||
/** | ||
The number of concurrently pending promises. | ||
@default Infinity | ||
*/ | ||
readonly concurrency?: number; | ||
Minimum: `1` | ||
/** | ||
Preserve `paths` order when searching. | ||
@default Infinity | ||
*/ | ||
readonly concurrency?: number; | ||
Disable this to improve performance if you don't care about the order. | ||
/** | ||
Preserve `paths` order when searching. | ||
@default true | ||
*/ | ||
readonly preserveOrder?: boolean; | ||
} | ||
Disable this to improve performance if you don't care about the order. | ||
@default true | ||
*/ | ||
readonly preserveOrder?: boolean; | ||
} | ||
declare const locatePath: { | ||
/** | ||
Synchronously get the first path that exists on disk of multiple paths. | ||
/** | ||
Get the first path that exists on disk of multiple paths. | ||
@param paths - Paths to check. | ||
@returns The first path that exists or `undefined` if none exists. | ||
*/ | ||
sync: ( | ||
paths: Iterable<string>, | ||
options?: locatePath.Options | ||
) => string | undefined; | ||
@param paths - The paths to check. | ||
@returns The first path that exists or `undefined` if none exists. | ||
/** | ||
Get the first path that exists on disk of multiple paths. | ||
@example | ||
``` | ||
import {locatePath} from 'locate-path'; | ||
@param paths - Paths to check. | ||
@returns The first path that exists or `undefined` if none exists. | ||
const files = [ | ||
'unicorn.png', | ||
'rainbow.png', // Only this one actually exists on disk | ||
'pony.png' | ||
]; | ||
@example | ||
``` | ||
import locatePath = require('locate-path'); | ||
console(await locatePath(files)); | ||
//=> 'rainbow' | ||
``` | ||
*/ | ||
export function locatePath( | ||
paths: Iterable<string>, | ||
options?: AsyncOptions | ||
): Promise<string | undefined>; | ||
const files = [ | ||
'unicorn.png', | ||
'rainbow.png', // Only this one actually exists on disk | ||
'pony.png' | ||
]; | ||
/** | ||
Synchronously get the first path that exists on disk of multiple paths. | ||
(async () => { | ||
console(await locatePath(files)); | ||
//=> 'rainbow' | ||
})(); | ||
``` | ||
*/ | ||
(paths: Iterable<string>, options?: locatePath.AsyncOptions): Promise< | ||
string | undefined | ||
>; | ||
}; | ||
@param paths - The paths to check. | ||
@returns The first path that exists or `undefined` if none exists. | ||
export = locatePath; | ||
@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<string>, | ||
options?: Options | ||
): string | undefined; |
72
index.js
@@ -1,16 +0,12 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const {promisify} = require('util'); | ||
const pLocate = require('p-locate'); | ||
import process from 'node:process'; | ||
import path from 'node:path'; | ||
import fs, {promises as fsPromises} from 'node:fs'; | ||
import pLocate from 'p-locate'; | ||
const fsStat = promisify(fs.stat); | ||
const fsLStat = promisify(fs.lstat); | ||
const typeMappings = { | ||
directory: 'isDirectory', | ||
file: 'isFile' | ||
file: 'isFile', | ||
}; | ||
function checkType({type}) { | ||
function checkType(type) { | ||
if (type in typeMappings) { | ||
@@ -25,41 +21,43 @@ return; | ||
module.exports = async (paths, options) => { | ||
options = { | ||
cwd: process.cwd(), | ||
type: 'file', | ||
allowSymlinks: true, | ||
...options | ||
}; | ||
export async function locatePath( | ||
paths, | ||
{ | ||
cwd = process.cwd(), | ||
type = 'file', | ||
allowSymlinks = true, | ||
concurrency, | ||
preserveOrder, | ||
} = {}, | ||
) { | ||
checkType(type); | ||
checkType(options); | ||
const statFunction = allowSymlinks ? fsPromises.stat : fsPromises.lstat; | ||
const statFn = options.allowSymlinks ? fsStat : fsLStat; | ||
return pLocate(paths, async path_ => { | ||
try { | ||
const stat = await statFn(path.resolve(options.cwd, path_)); | ||
return matchType(options.type, stat); | ||
const stat = await statFunction(path.resolve(cwd, path_)); | ||
return matchType(type, stat); | ||
} catch { | ||
return false; | ||
} | ||
}, options); | ||
}; | ||
}, {concurrency, preserveOrder}); | ||
} | ||
module.exports.sync = (paths, options) => { | ||
options = { | ||
cwd: process.cwd(), | ||
allowSymlinks: true, | ||
type: 'file', | ||
...options | ||
}; | ||
export function locatePathSync( | ||
paths, | ||
{ | ||
cwd = process.cwd(), | ||
type = 'file', | ||
allowSymlinks = true, | ||
} = {}, | ||
) { | ||
checkType(type); | ||
checkType(options); | ||
const statFunction = allowSymlinks ? fs.statSync : fs.lstatSync; | ||
const statFn = options.allowSymlinks ? fs.statSync : fs.lstatSync; | ||
for (const path_ of paths) { | ||
try { | ||
const stat = statFn(path.resolve(options.cwd, path_)); | ||
const stat = statFunction(path.resolve(cwd, path_)); | ||
if (matchType(options.type, stat)) { | ||
if (matchType(type, stat)) { | ||
return path_; | ||
@@ -69,2 +67,2 @@ } | ||
} | ||
}; | ||
} |
{ | ||
"name": "locate-path", | ||
"version": "6.0.0", | ||
"version": "7.0.0", | ||
"description": "Get the first path that exists on disk of multiple paths", | ||
@@ -13,4 +13,6 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -40,9 +42,9 @@ "scripts": { | ||
"dependencies": { | ||
"p-locate": "^5.0.0" | ||
"p-locate": "^6.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"tsd": "^0.13.1", | ||
"xo": "^0.32.1" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# locate-path [![Build Status](https://travis-ci.com/sindresorhus/locate-path.svg?branch=master)](https://travis-ci.com/github/sindresorhus/locate-path) | ||
# locate-path | ||
@@ -16,3 +16,3 @@ > Get the first path that exists on disk of multiple paths | ||
```js | ||
const locatePath = require('locate-path'); | ||
import {locatePath} from 'locate-path'; | ||
@@ -25,6 +25,4 @@ const files = [ | ||
(async () => { | ||
console(await locatePath(files)); | ||
//=> 'rainbow' | ||
})(); | ||
console(await locatePath(files)); | ||
//=> 'rainbow' | ||
``` | ||
@@ -42,3 +40,3 @@ | ||
Paths to check. | ||
The paths to check. | ||
@@ -55,3 +53,3 @@ #### options | ||
Number of concurrently pending promises. | ||
The number of concurrently pending promises. | ||
@@ -72,3 +70,3 @@ ##### preserveOrder | ||
Current working directory. | ||
The current working directory. | ||
@@ -98,3 +96,3 @@ ##### type | ||
Paths to check. | ||
The paths to check. | ||
@@ -101,0 +99,0 @@ #### options |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
126
0
Yes
6987
124
+ Addedp-limit@4.0.0(transitive)
+ Addedp-locate@6.0.0(transitive)
+ Addedyocto-queue@1.0.0(transitive)
- Removedp-limit@3.1.0(transitive)
- Removedp-locate@5.0.0(transitive)
- Removedyocto-queue@0.1.0(transitive)
Updatedp-locate@^6.0.0