locate-path
Advanced tools
Comparing version 4.0.0 to 5.0.0
@@ -9,2 +9,16 @@ declare namespace locatePath { | ||
readonly cwd?: string; | ||
/** | ||
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; | ||
} | ||
@@ -11,0 +25,0 @@ |
49
index.js
'use strict'; | ||
const path = require('path'); | ||
const pathExists = require('path-exists'); | ||
const fs = require('fs'); | ||
const {promisify} = require('util'); | ||
const pLocate = require('p-locate'); | ||
module.exports = (paths, options) => { | ||
const fsStat = promisify(fs.stat); | ||
const fsLStat = promisify(fs.lstat); | ||
const typeMappings = { | ||
directory: 'isDirectory', | ||
file: 'isFile' | ||
}; | ||
function checkType({type}) { | ||
if (type in typeMappings) { | ||
return; | ||
} | ||
throw new Error(`Invalid type specified: ${type}`); | ||
} | ||
const matchType = (type, stat) => type === undefined || stat[typeMappings[type]](); | ||
module.exports = async (paths, options) => { | ||
options = { | ||
cwd: process.cwd(), | ||
type: 'file', | ||
allowSymlinks: true, | ||
...options | ||
}; | ||
checkType(options); | ||
const statFn = options.allowSymlinks ? fsStat : fsLStat; | ||
return pLocate(paths, path_ => pathExists(path.resolve(options.cwd, path_)), options); | ||
return pLocate(paths, async path_ => { | ||
try { | ||
const stat = await statFn(path.resolve(options.cwd, path_)); | ||
return matchType(options.type, stat); | ||
} catch (_) { | ||
return false; | ||
} | ||
}, options); | ||
}; | ||
@@ -18,10 +48,19 @@ | ||
cwd: process.cwd(), | ||
allowSymlinks: true, | ||
type: 'file', | ||
...options | ||
}; | ||
checkType(options); | ||
const statFn = options.allowSymlinks ? fs.statSync : fs.lstatSync; | ||
for (const path_ of paths) { | ||
if (pathExists.sync(path.resolve(options.cwd, path_))) { | ||
return path_; | ||
try { | ||
const stat = statFn(path.resolve(options.cwd, path_)); | ||
if (matchType(options.type, stat)) { | ||
return path_; | ||
} | ||
} catch (_) { | ||
} | ||
} | ||
}; |
{ | ||
"name": "locate-path", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Get the first path that exists on disk of multiple paths", | ||
@@ -38,4 +38,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"p-locate": "^4.1.0", | ||
"path-exists": "^4.0.0" | ||
"p-locate": "^4.1.0" | ||
}, | ||
@@ -42,0 +41,0 @@ "devDependencies": { |
@@ -73,2 +73,17 @@ # locate-path [![Build Status](https://travis-ci.org/sindresorhus/locate-path.svg?branch=master)](https://travis-ci.org/sindresorhus/locate-path) | ||
##### type | ||
Type: `string`<br> | ||
Default: `file`<br> | ||
Values: `file` `directory` | ||
The type of paths that can match. | ||
##### allowSymlinks | ||
Type: `boolean`<br> | ||
Default: `true` | ||
Allow symbolic links to match if they point to the chosen path type. | ||
### locatePath.sync(paths, [options]) | ||
@@ -92,3 +107,11 @@ | ||
##### type | ||
Same as above. | ||
##### allowSymlinks | ||
Same as above. | ||
## Related | ||
@@ -95,0 +118,0 @@ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
6584
1
120
123
1
- Removedpath-exists@^4.0.0
- Removedpath-exists@4.0.0(transitive)