Comparing version 2.0.3 to 2.1.0
@@ -9,2 +9,3 @@ declare module "fdir" { | ||
isExcludedDir?: (dirPath: string) => boolean; | ||
ignoreErrors?: boolean; | ||
}; | ||
@@ -17,3 +18,3 @@ | ||
*/ | ||
function sync(directoryPath: String, options: Options): Array<String>; | ||
function sync(directoryPath: String, options?: Options): Array<String>; | ||
@@ -27,3 +28,3 @@ /** | ||
directoryPath: String, | ||
options: Options | ||
options?: Options | ||
): Promise<Array<String>>; | ||
@@ -30,0 +31,0 @@ |
83
index.js
const fs = require("fs"); | ||
const { sep, resolve } = require("path"); | ||
const { sep, resolve, normalize } = require("path"); | ||
@@ -8,11 +8,19 @@ const readdirOpts = { withFileTypes: true }; | ||
if (options.resolvePaths) dir = resolve(dir); | ||
const paths = []; | ||
const dirs = [dir]; | ||
dir = cleanPath(dir); | ||
const paths = [], | ||
dirs = [dir]; | ||
for (var i = 0; i < dirs.length && !(--options.maxDepth < 0); ++i) { | ||
var currentDir = dirs[i]; | ||
let currentDir = dirs[i]; | ||
if (options.includeDirs) paths[paths.length] = currentDir; | ||
const dirents = fs.readdirSync(currentDir, readdirOpts); | ||
dirents.forEach(function(dirent) { | ||
recurse(dirent, currentDir, paths, options, dirs); | ||
}); | ||
try { | ||
const dirents = fs.readdirSync(currentDir, readdirOpts); | ||
// in cases where we have / as path | ||
if (currentDir === sep) currentDir = ""; | ||
dirents.forEach(function(dirent) { | ||
recurse(dirent, currentDir, paths, options, dirs); | ||
}); | ||
} catch (error) { | ||
if (!options.ignoreErrors) throw error; | ||
continue; | ||
} | ||
} | ||
@@ -23,29 +31,33 @@ return paths; | ||
function async(dir, options = {}) { | ||
return new Promise(function(pResolve) { | ||
const paths = []; | ||
return new Promise(function(pResolve, pReject) { | ||
if (options.resolvePaths) dir = resolve(dir); | ||
const dirs = [dir]; | ||
let cursor = 0; | ||
let readCount = 0; | ||
let currentDepth = options.maxDepth; | ||
dir = cleanPath(dir); | ||
const dirs = [dir], | ||
paths = []; | ||
let cursor = 0, | ||
readCount = 0, | ||
currentDepth = options.maxDepth; | ||
function walk() { | ||
let total = dirs.length; | ||
if (total === cursor) return pResolve(paths); | ||
for (; cursor < total; ++cursor) { | ||
if (--currentDepth < 0) { | ||
pResolve(paths); | ||
break; | ||
} | ||
const dir = dirs[cursor]; | ||
if (options.includeDirs) paths[paths.length] = dir; | ||
fs.readdir(dir, readdirOpts, function(_, dirents) { | ||
if (--currentDepth < 0) return pResolve(paths); | ||
let currentDir = dirs[cursor]; | ||
if (options.includeDirs) paths[paths.length] = currentDir; | ||
fs.readdir(currentDir, readdirOpts, function(error, dirents) { | ||
++readCount; | ||
if (error) { | ||
if (!options.ignoreErrors) return pReject(error); | ||
/* istanbul ignore next */ | ||
if (readCount === total) walk(); | ||
return; | ||
} | ||
// in cases where we have / as path | ||
if (currentDir === sep) currentDir = ""; | ||
for (var j = 0; j < dirents.length; ++j) { | ||
recurse(dirents[j], dir, paths, options, dirs); | ||
recurse(dirents[j], currentDir, paths, options, dirs); | ||
} | ||
if (++readCount === total) { | ||
if (dirs.length === cursor) { | ||
pResolve(paths); | ||
} else { | ||
walk(); | ||
} | ||
} | ||
if (readCount === total) walk(); | ||
}); | ||
@@ -58,3 +70,3 @@ } | ||
function recurse(dirent, dir, paths, options, dirs) { | ||
function recurse(dirent, currentDir, paths, options, dirs) { | ||
// In node < 10, Dirent is not present. Instead we get string paths | ||
@@ -64,3 +76,3 @@ | ||
const dirName = dirent.name || dirent; | ||
let fullPath = `${dir}${sep}${dirName}`; | ||
let fullPath = `${currentDir}${sep}${dirName}`; | ||
@@ -82,2 +94,11 @@ /* istanbul ignore next */ | ||
function cleanPath(dirPath) { | ||
let normalized = normalize(dirPath); | ||
// to account for / path | ||
if (normalized.length > 1 && normalized[normalized.length - 1] === sep) | ||
normalized = normalized.substring(0, normalized.length - 1); | ||
return normalized; | ||
} | ||
module.exports = { | ||
@@ -84,0 +105,0 @@ sync, |
{ | ||
"name": "fdir", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "The fastest directory crawler for NodeJS. Crawls 10k files in 13ms.", | ||
@@ -41,2 +41,3 @@ "main": "index.js", | ||
"klaw-sync": "^6.0.0", | ||
"mock-fs": "^4.11.0", | ||
"recur-readdir": "0.0.1", | ||
@@ -43,0 +44,0 @@ "recursive-files": "^1.0.2", |
@@ -166,3 +166,3 @@ <p align="center"> | ||
I know you don't give a shit. Fine. There's no point behind this. It's "just for fun". No, wait. Actually, I created this, first of all, for me. I needed fast directory access in another app of mine, so `fdir` came into being. | ||
I know you don't care. Fine. There's no point behind this. It's "just for fun". No, wait. Actually, I created this, first of all, for me. I needed fast directory access in another app of mine, so `fdir` came into being. | ||
@@ -181,3 +181,3 @@ **5. Why are all the other libraries so slow?** | ||
**8. Why should I give a shit?** | ||
**8. Why should I care?** | ||
@@ -188,3 +188,3 @@ You shouldn't. But here's my email in case you do: **thecodrr[at]protonmail.com**. Don't worry, I don't bite. | ||
Would love if you throw a coffee [over here](https://paypal.me/cupertino). Or just be, you know, polite and give me a star? Maybe even follow me? | ||
Would love if you buy me a cup of coffee [right over here](https://ko-fi.com/thecodrr). Or just be, you know, polite and give me a star? Maybe even follow me? | ||
@@ -191,0 +191,0 @@ ## 🦮 LICENSE |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13186
117
12