91 lines
3.4 KiB
JavaScript
91 lines
3.4 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const fsStat = require("@nodelib/fs.stat");
|
||
|
const rpl = require("run-parallel");
|
||
|
const constants_1 = require("../constants");
|
||
|
const utils = require("../utils");
|
||
|
function read(directory, settings, callback) {
|
||
|
if (!settings.stats && constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
|
||
|
return readdirWithFileTypes(directory, settings, callback);
|
||
|
}
|
||
|
return readdir(directory, settings, callback);
|
||
|
}
|
||
|
exports.read = read;
|
||
|
function readdirWithFileTypes(directory, settings, callback) {
|
||
|
settings.fs.readdir(directory, { withFileTypes: true }, (readdirError, dirents) => {
|
||
|
if (readdirError !== null) {
|
||
|
return callFailureCallback(callback, readdirError);
|
||
|
}
|
||
|
const entries = dirents.map((dirent) => ({
|
||
|
dirent,
|
||
|
name: dirent.name,
|
||
|
path: `${directory}${settings.pathSegmentSeparator}${dirent.name}`
|
||
|
}));
|
||
|
if (!settings.followSymbolicLinks) {
|
||
|
return callSuccessCallback(callback, entries);
|
||
|
}
|
||
|
const tasks = entries.map((entry) => makeRplTaskEntry(entry, settings));
|
||
|
rpl(tasks, (rplError, rplEntries) => {
|
||
|
if (rplError !== null) {
|
||
|
return callFailureCallback(callback, rplError);
|
||
|
}
|
||
|
callSuccessCallback(callback, rplEntries);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
exports.readdirWithFileTypes = readdirWithFileTypes;
|
||
|
function makeRplTaskEntry(entry, settings) {
|
||
|
return (done) => {
|
||
|
if (!entry.dirent.isSymbolicLink()) {
|
||
|
return done(null, entry);
|
||
|
}
|
||
|
settings.fs.stat(entry.path, (statError, stats) => {
|
||
|
if (statError !== null) {
|
||
|
if (settings.throwErrorOnBrokenSymbolicLink) {
|
||
|
return done(statError);
|
||
|
}
|
||
|
return done(null, entry);
|
||
|
}
|
||
|
entry.dirent = utils.fs.createDirentFromStats(entry.name, stats);
|
||
|
return done(null, entry);
|
||
|
});
|
||
|
};
|
||
|
}
|
||
|
function readdir(directory, settings, callback) {
|
||
|
settings.fs.readdir(directory, (readdirError, names) => {
|
||
|
if (readdirError !== null) {
|
||
|
return callFailureCallback(callback, readdirError);
|
||
|
}
|
||
|
const filepaths = names.map((name) => `${directory}${settings.pathSegmentSeparator}${name}`);
|
||
|
const tasks = filepaths.map((filepath) => {
|
||
|
return (done) => fsStat.stat(filepath, settings.fsStatSettings, done);
|
||
|
});
|
||
|
rpl(tasks, (rplError, results) => {
|
||
|
if (rplError !== null) {
|
||
|
return callFailureCallback(callback, rplError);
|
||
|
}
|
||
|
const entries = [];
|
||
|
names.forEach((name, index) => {
|
||
|
const stats = results[index];
|
||
|
const entry = {
|
||
|
name,
|
||
|
path: filepaths[index],
|
||
|
dirent: utils.fs.createDirentFromStats(name, stats)
|
||
|
};
|
||
|
if (settings.stats) {
|
||
|
entry.stats = stats;
|
||
|
}
|
||
|
entries.push(entry);
|
||
|
});
|
||
|
callSuccessCallback(callback, entries);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
exports.readdir = readdir;
|
||
|
function callFailureCallback(callback, error) {
|
||
|
callback(error);
|
||
|
}
|
||
|
function callSuccessCallback(callback, result) {
|
||
|
callback(null, result);
|
||
|
}
|