1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.readdir = exports.readdirWithFileTypes = exports.read = void 0;
|
---|
4 | const fsStat = require("@nodelib/fs.stat");
|
---|
5 | const rpl = require("run-parallel");
|
---|
6 | const constants_1 = require("../constants");
|
---|
7 | const utils = require("../utils");
|
---|
8 | const common = require("./common");
|
---|
9 | function read(directory, settings, callback) {
|
---|
10 | if (!settings.stats && constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
|
---|
11 | readdirWithFileTypes(directory, settings, callback);
|
---|
12 | return;
|
---|
13 | }
|
---|
14 | readdir(directory, settings, callback);
|
---|
15 | }
|
---|
16 | exports.read = read;
|
---|
17 | function readdirWithFileTypes(directory, settings, callback) {
|
---|
18 | settings.fs.readdir(directory, { withFileTypes: true }, (readdirError, dirents) => {
|
---|
19 | if (readdirError !== null) {
|
---|
20 | callFailureCallback(callback, readdirError);
|
---|
21 | return;
|
---|
22 | }
|
---|
23 | const entries = dirents.map((dirent) => ({
|
---|
24 | dirent,
|
---|
25 | name: dirent.name,
|
---|
26 | path: common.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
|
---|
27 | }));
|
---|
28 | if (!settings.followSymbolicLinks) {
|
---|
29 | callSuccessCallback(callback, entries);
|
---|
30 | return;
|
---|
31 | }
|
---|
32 | const tasks = entries.map((entry) => makeRplTaskEntry(entry, settings));
|
---|
33 | rpl(tasks, (rplError, rplEntries) => {
|
---|
34 | if (rplError !== null) {
|
---|
35 | callFailureCallback(callback, rplError);
|
---|
36 | return;
|
---|
37 | }
|
---|
38 | callSuccessCallback(callback, rplEntries);
|
---|
39 | });
|
---|
40 | });
|
---|
41 | }
|
---|
42 | exports.readdirWithFileTypes = readdirWithFileTypes;
|
---|
43 | function makeRplTaskEntry(entry, settings) {
|
---|
44 | return (done) => {
|
---|
45 | if (!entry.dirent.isSymbolicLink()) {
|
---|
46 | done(null, entry);
|
---|
47 | return;
|
---|
48 | }
|
---|
49 | settings.fs.stat(entry.path, (statError, stats) => {
|
---|
50 | if (statError !== null) {
|
---|
51 | if (settings.throwErrorOnBrokenSymbolicLink) {
|
---|
52 | done(statError);
|
---|
53 | return;
|
---|
54 | }
|
---|
55 | done(null, entry);
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | entry.dirent = utils.fs.createDirentFromStats(entry.name, stats);
|
---|
59 | done(null, entry);
|
---|
60 | });
|
---|
61 | };
|
---|
62 | }
|
---|
63 | function readdir(directory, settings, callback) {
|
---|
64 | settings.fs.readdir(directory, (readdirError, names) => {
|
---|
65 | if (readdirError !== null) {
|
---|
66 | callFailureCallback(callback, readdirError);
|
---|
67 | return;
|
---|
68 | }
|
---|
69 | const tasks = names.map((name) => {
|
---|
70 | const path = common.joinPathSegments(directory, name, settings.pathSegmentSeparator);
|
---|
71 | return (done) => {
|
---|
72 | fsStat.stat(path, settings.fsStatSettings, (error, stats) => {
|
---|
73 | if (error !== null) {
|
---|
74 | done(error);
|
---|
75 | return;
|
---|
76 | }
|
---|
77 | const entry = {
|
---|
78 | name,
|
---|
79 | path,
|
---|
80 | dirent: utils.fs.createDirentFromStats(name, stats)
|
---|
81 | };
|
---|
82 | if (settings.stats) {
|
---|
83 | entry.stats = stats;
|
---|
84 | }
|
---|
85 | done(null, entry);
|
---|
86 | });
|
---|
87 | };
|
---|
88 | });
|
---|
89 | rpl(tasks, (rplError, entries) => {
|
---|
90 | if (rplError !== null) {
|
---|
91 | callFailureCallback(callback, rplError);
|
---|
92 | return;
|
---|
93 | }
|
---|
94 | callSuccessCallback(callback, entries);
|
---|
95 | });
|
---|
96 | });
|
---|
97 | }
|
---|
98 | exports.readdir = readdir;
|
---|
99 | function callFailureCallback(callback, error) {
|
---|
100 | callback(error);
|
---|
101 | }
|
---|
102 | function callSuccessCallback(callback, result) {
|
---|
103 | callback(null, result);
|
---|
104 | }
|
---|