@in-browser/fdir
Version:
The fastest directory crawler & globbing alternative to glob, fast-glob, & tiny-glob. Crawls 1m files in < 1s
40 lines (39 loc) • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.build = build;
const readdirOpts = { withFileTypes: true };
const walkAsync = (state, crawlPath, directoryPath, currentDepth, callback) => {
if (currentDepth < 0)
return state.queue.dequeue(null, state);
state.visited.push(crawlPath);
state.counts.directories++;
state.queue.enqueue();
// Get the file system implementation to use
const fileSystem = state.options.fileSystem;
// Perf: Node >= 10 introduced withFileTypes that helps us
// skip an extra fs.stat call.
fileSystem.readdir(crawlPath || ".", readdirOpts, (error, entries = []) => {
callback(entries, directoryPath, currentDepth);
state.queue.dequeue(state.options.suppressErrors ? null : error, state);
});
};
const walkSync = (state, crawlPath, directoryPath, currentDepth, callback) => {
if (currentDepth < 0)
return;
state.visited.push(crawlPath);
state.counts.directories++;
// Get the file system implementation to use
const fileSystem = state.options.fileSystem;
let entries = [];
try {
entries = fileSystem.readdirSync(crawlPath || ".", readdirOpts);
}
catch (e) {
if (!state.options.suppressErrors)
throw e;
}
callback(entries, directoryPath, currentDepth);
};
function build(isSynchronous) {
return isSynchronous ? walkSync : walkAsync;
}