findup-lit
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -6,10 +6,16 @@ import path from 'path'; | ||
//////////////////////////////////////////////////////////////////////////////// | ||
const fsStat = promisify(fs.stat); | ||
const fsLStat = promisify(fs.lstat); //////////////////////////////////////////////////////////////////////////////// | ||
const fsLStat = promisify(fs.lstat); | ||
//////////////////////////////////////////////////////////////////////////////// | ||
const typeMappings = { | ||
directory: 'isDirectory', | ||
file: 'isFile' | ||
}; //////////////////////////////////////////////////////////////////////////////// | ||
}; | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -20,8 +26,9 @@ * checkType tests if a given `type` is valid. | ||
*/ | ||
function checkType(type) { | ||
if (type in typeMappings) return; | ||
throw new Error(`Invalid type specified: ${type}`); | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -33,8 +40,7 @@ * matchType tests if a given `type` matches a file or directory type. | ||
*/ | ||
function matchType(type, stat) { | ||
return type === undefined || stat[typeMappings[type]](); | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} | ||
//////////////////////////////////////////////////////////////////////////////// | ||
@@ -46,4 +52,5 @@ class EndError extends Error { | ||
} | ||
} | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
@@ -62,4 +69,2 @@ /** | ||
*/ | ||
async function locatePath(paths, options) { | ||
@@ -76,4 +81,5 @@ options = { | ||
const statFn = options.allowSymlinks ? fsStat : fsLStat; | ||
const limit = pLimit(options.concurrency); // NOTE(joel): Start all the promises concurrently with optional limit. | ||
const limit = pLimit(options.concurrency); | ||
// NOTE(joel): Start all the promises concurrently with optional limit. | ||
const items = [...paths].map(element => [element, limit(async () => { | ||
@@ -86,15 +92,14 @@ try { | ||
} | ||
})]); // NOTE(joel): Check the promises either serially or concurrently. | ||
})]); | ||
// NOTE(joel): Check the promises either serially or concurrently. | ||
const checkLimit = pLimit(options.preserveOrder ? 1 : Infinity); | ||
try { | ||
await Promise.all(items.map(element => checkLimit(async () => { | ||
const values = await Promise.all(element); // NOTE(joel): When we found a file, we throw an `EndError` to | ||
const values = await Promise.all(element); | ||
// NOTE(joel): When we found a file, we throw an `EndError` to | ||
// indicate that we're done. | ||
if (values[1] === true) { | ||
throw new EndError(values[0]); | ||
} | ||
return false; | ||
@@ -106,7 +111,8 @@ }))); | ||
} | ||
throw error; | ||
} | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -121,3 +127,2 @@ * locatePathSync gets the first path that is either a directory or file. | ||
*/ | ||
function locatePathSync(paths, options) { | ||
@@ -132,7 +137,5 @@ options = { | ||
const statFn = options.allowSymlinks ? fs.statSync : fs.lstatSync; | ||
for (const _path of paths) { | ||
try { | ||
const stat = statFn(path.resolve(options.cwd, _path)); | ||
if (matchType(options.type, stat)) { | ||
@@ -151,3 +154,2 @@ return _path; | ||
*/ | ||
async function pathExists(path) { | ||
@@ -161,2 +163,3 @@ try { | ||
} | ||
/** | ||
@@ -168,3 +171,2 @@ * pathExistsSync synchronously checks if a given `path` (file or directory) | ||
*/ | ||
function pathExistsSync(path) { | ||
@@ -179,2 +181,4 @@ try { | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -185,4 +189,4 @@ * findUpStop can be returned by a matcher function to stop the search and | ||
*/ | ||
const findUpStop = Symbol('findUp.stop'); | ||
const findUpStop = Symbol('findUp.stop'); | ||
/** | ||
@@ -193,4 +197,4 @@ * findUpExists asynchronously checks if a given `path` (file or directory) | ||
*/ | ||
const findUpExists = pathExists; | ||
const findUpExists = pathExists; | ||
/** | ||
@@ -201,4 +205,5 @@ * findUpExistsSync synchronously checks if a given `path` (file or directory) | ||
*/ | ||
const findUpExistsSync = pathExistsSync; | ||
const findUpExistsSync = pathExistsSync; //////////////////////////////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
@@ -211,3 +216,2 @@ /** | ||
*/ | ||
async function findUp(name, options = {}) { | ||
@@ -219,3 +223,2 @@ let directory = path.resolve(options.cwd || ''); | ||
const paths = [].concat(name); | ||
const runMatcher = async locateOptions => { | ||
@@ -225,34 +228,30 @@ if (typeof name !== 'function') { | ||
} | ||
const foundPath = await name(locateOptions.cwd); | ||
if (typeof foundPath === 'string') { | ||
return locatePath([foundPath], locateOptions); | ||
} | ||
return foundPath; | ||
}; // eslint-disable-next-line no-constant-condition | ||
}; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const foundPath = await runMatcher({ ...options, | ||
const foundPath = await runMatcher({ | ||
...options, | ||
cwd: directory | ||
}); | ||
if (foundPath === findUpStop) { | ||
return; | ||
} | ||
if (foundPath) { | ||
return path.resolve(directory, foundPath); | ||
} | ||
if (directory === root) { | ||
return; | ||
} | ||
directory = path.dirname(directory); | ||
} | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -264,3 +263,2 @@ * findUpSync walks the directory tree up until it finds the given `name`. | ||
*/ | ||
function findUpSync(name, options = {}) { | ||
@@ -272,3 +270,2 @@ let directory = path.resolve(options.cwd || ''); | ||
const paths = [].concat(name); | ||
const runMatcher = locateOptions => { | ||
@@ -278,30 +275,24 @@ if (typeof name !== 'function') { | ||
} | ||
const foundPath = name(locateOptions.cwd); | ||
if (typeof foundPath === 'string') { | ||
return locatePathSync([foundPath], locateOptions); | ||
} | ||
return foundPath; | ||
}; // eslint-disable-next-line no-constant-condition | ||
}; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const foundPath = runMatcher({ ...options, | ||
const foundPath = runMatcher({ | ||
...options, | ||
cwd: directory | ||
}); | ||
if (foundPath === findUpStop) { | ||
return; | ||
} | ||
if (foundPath) { | ||
return path.resolve(directory, foundPath); | ||
} | ||
if (directory === root) { | ||
return; | ||
} | ||
directory = path.dirname(directory); | ||
@@ -308,0 +299,0 @@ } |
@@ -6,10 +6,16 @@ import path from 'path'; | ||
//////////////////////////////////////////////////////////////////////////////// | ||
const fsStat = promisify(fs.stat); | ||
const fsLStat = promisify(fs.lstat); //////////////////////////////////////////////////////////////////////////////// | ||
const fsLStat = promisify(fs.lstat); | ||
//////////////////////////////////////////////////////////////////////////////// | ||
const typeMappings = { | ||
directory: 'isDirectory', | ||
file: 'isFile' | ||
}; //////////////////////////////////////////////////////////////////////////////// | ||
}; | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -20,8 +26,9 @@ * checkType tests if a given `type` is valid. | ||
*/ | ||
function checkType(type) { | ||
if (type in typeMappings) return; | ||
throw new Error(`Invalid type specified: ${type}`); | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -33,8 +40,7 @@ * matchType tests if a given `type` matches a file or directory type. | ||
*/ | ||
function matchType(type, stat) { | ||
return type === undefined || stat[typeMappings[type]](); | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} | ||
//////////////////////////////////////////////////////////////////////////////// | ||
@@ -46,4 +52,5 @@ class EndError extends Error { | ||
} | ||
} | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
@@ -62,4 +69,2 @@ /** | ||
*/ | ||
async function locatePath(paths, options) { | ||
@@ -76,4 +81,5 @@ options = { | ||
const statFn = options.allowSymlinks ? fsStat : fsLStat; | ||
const limit = pLimit(options.concurrency); // NOTE(joel): Start all the promises concurrently with optional limit. | ||
const limit = pLimit(options.concurrency); | ||
// NOTE(joel): Start all the promises concurrently with optional limit. | ||
const items = [...paths].map(element => [element, limit(async () => { | ||
@@ -86,15 +92,14 @@ try { | ||
} | ||
})]); // NOTE(joel): Check the promises either serially or concurrently. | ||
})]); | ||
// NOTE(joel): Check the promises either serially or concurrently. | ||
const checkLimit = pLimit(options.preserveOrder ? 1 : Infinity); | ||
try { | ||
await Promise.all(items.map(element => checkLimit(async () => { | ||
const values = await Promise.all(element); // NOTE(joel): When we found a file, we throw an `EndError` to | ||
const values = await Promise.all(element); | ||
// NOTE(joel): When we found a file, we throw an `EndError` to | ||
// indicate that we're done. | ||
if (values[1] === true) { | ||
throw new EndError(values[0]); | ||
} | ||
return false; | ||
@@ -106,7 +111,8 @@ }))); | ||
} | ||
throw error; | ||
} | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -121,3 +127,2 @@ * locatePathSync gets the first path that is either a directory or file. | ||
*/ | ||
function locatePathSync(paths, options) { | ||
@@ -132,7 +137,5 @@ options = { | ||
const statFn = options.allowSymlinks ? fs.statSync : fs.lstatSync; | ||
for (const _path of paths) { | ||
try { | ||
const stat = statFn(path.resolve(options.cwd, _path)); | ||
if (matchType(options.type, stat)) { | ||
@@ -151,3 +154,2 @@ return _path; | ||
*/ | ||
async function pathExists(path) { | ||
@@ -161,2 +163,3 @@ try { | ||
} | ||
/** | ||
@@ -168,3 +171,2 @@ * pathExistsSync synchronously checks if a given `path` (file or directory) | ||
*/ | ||
function pathExistsSync(path) { | ||
@@ -179,2 +181,4 @@ try { | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -185,4 +189,4 @@ * findUpStop can be returned by a matcher function to stop the search and | ||
*/ | ||
const findUpStop = Symbol('findUp.stop'); | ||
const findUpStop = Symbol('findUp.stop'); | ||
/** | ||
@@ -193,4 +197,4 @@ * findUpExists asynchronously checks if a given `path` (file or directory) | ||
*/ | ||
const findUpExists = pathExists; | ||
const findUpExists = pathExists; | ||
/** | ||
@@ -201,4 +205,5 @@ * findUpExistsSync synchronously checks if a given `path` (file or directory) | ||
*/ | ||
const findUpExistsSync = pathExistsSync; | ||
const findUpExistsSync = pathExistsSync; //////////////////////////////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
@@ -211,3 +216,2 @@ /** | ||
*/ | ||
async function findUp(name, options = {}) { | ||
@@ -219,3 +223,2 @@ let directory = path.resolve(options.cwd || ''); | ||
const paths = [].concat(name); | ||
const runMatcher = async locateOptions => { | ||
@@ -225,34 +228,30 @@ if (typeof name !== 'function') { | ||
} | ||
const foundPath = await name(locateOptions.cwd); | ||
if (typeof foundPath === 'string') { | ||
return locatePath([foundPath], locateOptions); | ||
} | ||
return foundPath; | ||
}; // eslint-disable-next-line no-constant-condition | ||
}; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const foundPath = await runMatcher({ ...options, | ||
const foundPath = await runMatcher({ | ||
...options, | ||
cwd: directory | ||
}); | ||
if (foundPath === findUpStop) { | ||
return; | ||
} | ||
if (foundPath) { | ||
return path.resolve(directory, foundPath); | ||
} | ||
if (directory === root) { | ||
return; | ||
} | ||
directory = path.dirname(directory); | ||
} | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/** | ||
@@ -264,3 +263,2 @@ * findUpSync walks the directory tree up until it finds the given `name`. | ||
*/ | ||
function findUpSync(name, options = {}) { | ||
@@ -272,3 +270,2 @@ let directory = path.resolve(options.cwd || ''); | ||
const paths = [].concat(name); | ||
const runMatcher = locateOptions => { | ||
@@ -278,30 +275,24 @@ if (typeof name !== 'function') { | ||
} | ||
const foundPath = name(locateOptions.cwd); | ||
if (typeof foundPath === 'string') { | ||
return locatePathSync([foundPath], locateOptions); | ||
} | ||
return foundPath; | ||
}; // eslint-disable-next-line no-constant-condition | ||
}; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const foundPath = runMatcher({ ...options, | ||
const foundPath = runMatcher({ | ||
...options, | ||
cwd: directory | ||
}); | ||
if (foundPath === findUpStop) { | ||
return; | ||
} | ||
if (foundPath) { | ||
return path.resolve(directory, foundPath); | ||
} | ||
if (directory === root) { | ||
return; | ||
} | ||
directory = path.dirname(directory); | ||
@@ -308,0 +299,0 @@ } |
{ | ||
"name": "findup-lit", | ||
"description": "This package finds a file or directory by walking up parent directories.", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"author": "Joel Voss <mail@joelvoss.com>", | ||
@@ -34,6 +34,6 @@ "license": "MIT", | ||
"dependencies": { | ||
"plimit-lit": "^1.4.0" | ||
"plimit-lit": "^1.4.1" | ||
}, | ||
"devDependencies": { | ||
"@jvdx/core": "^2.19.0", | ||
"@jvdx/core": "^3.0.0", | ||
"fixturez": "^1.1.0" | ||
@@ -40,0 +40,0 @@ }, |
Sorry, the diff of this file is not supported yet
754
27734
6
Updatedplimit-lit@^1.4.1