async.js
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"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);
}