1 | const { sep, resolve: pathResolve } = require("path");
|
2 | const { cleanPath } = require("../utils");
|
3 | const fns = require("./fns");
|
4 | const readdirOpts = { withFileTypes: true };
|
5 |
|
6 | function init(dir, options, callback, isSync) {
|
7 | if (options.resolvePaths) dir = pathResolve(dir);
|
8 | if (options.normalizePath) dir = cleanPath(dir);
|
9 |
|
10 | |
11 |
|
12 |
|
13 | const state = {
|
14 |
|
15 | paths: [""].slice(0, 0),
|
16 | queue: 0,
|
17 | counts: { files: 0, dirs: 0 },
|
18 | options,
|
19 | callback,
|
20 | };
|
21 |
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 | buildFunctions(options, isSync);
|
28 |
|
29 | return { state, callbackInvoker, dir };
|
30 | }
|
31 |
|
32 | function walkSingleDir(walk, state, dir, dirents, currentDepth, callback) {
|
33 | pushDir(dir, state.paths);
|
34 |
|
35 | if (dir === sep) dir = "";
|
36 |
|
37 | const files = getArray(state);
|
38 |
|
39 | for (var i = 0; i < dirents.length; ++i) {
|
40 | const dirent = dirents[i];
|
41 |
|
42 | if (dirent.isFile()) {
|
43 | const filename = joinPath(dirent.name, dir);
|
44 | pushFile(filename, files, dir, state);
|
45 | } else if (dirent.isDirectory()) {
|
46 | let dirPath = `${dir}${sep}${dirent.name}`;
|
47 | walkDir(walk, state, dirPath, dirent.name, currentDepth - 1, callback);
|
48 | }
|
49 | }
|
50 |
|
51 | groupFiles(dir, files, state);
|
52 | }
|
53 |
|
54 | function buildFunctions(options, isSync) {
|
55 | const {
|
56 | filters,
|
57 | onlyCountsVar,
|
58 | includeBasePath,
|
59 | includeDirs,
|
60 | groupVar,
|
61 | excludeFn,
|
62 | } = options;
|
63 |
|
64 | buildPushFile(filters, onlyCountsVar);
|
65 |
|
66 | pushDir = includeDirs ? fns.pushDir : fns.empty;
|
67 |
|
68 |
|
69 | joinPath = includeBasePath ? fns.joinPathWithBasePath : fns.joinPath;
|
70 |
|
71 |
|
72 | walkDir = excludeFn ? fns.walkDirExclude(excludeFn) : fns.walkDir;
|
73 |
|
74 |
|
75 | groupFiles = groupVar ? fns.groupFiles : fns.empty;
|
76 | getArray = groupVar ? fns.getArrayGroup : fns.getArray;
|
77 |
|
78 | buildCallbackInvoker(onlyCountsVar, isSync);
|
79 | }
|
80 |
|
81 | module.exports = { buildFunctions, init, walkSingleDir, readdirOpts };
|
82 |
|
83 | function buildPushFile(filters, onlyCountsVar) {
|
84 | if (filters.length && onlyCountsVar) {
|
85 | pushFile = fns.pushFileFilterAndCount(filters);
|
86 | } else if (filters.length) {
|
87 | pushFile = fns.pushFileFilter(filters);
|
88 | } else if (onlyCountsVar) {
|
89 | pushFile = fns.pushFileCount;
|
90 | } else {
|
91 | pushFile = fns.pushFile;
|
92 | }
|
93 | }
|
94 |
|
95 | function buildCallbackInvoker(onlyCountsVar, isSync) {
|
96 | if (onlyCountsVar) {
|
97 | callbackInvoker = isSync
|
98 | ? fns.callbackInvokerOnlyCountsSync
|
99 | : fns.callbackInvokerOnlyCountsAsync;
|
100 | } else {
|
101 | callbackInvoker = isSync
|
102 | ? fns.callbackInvokerDefaultSync
|
103 | : fns.callbackInvokerDefaultAsync;
|
104 | }
|
105 | }
|
106 |
|
107 |
|
108 | var pushFile = fns.empty;
|
109 | var pushDir = fns.empty;
|
110 | var walkDir = fns.empty;
|
111 | var joinPath = fns.empty;
|
112 | var groupFiles = fns.empty;
|
113 | var callbackInvoker = fns.empty;
|
114 | var getArray = fns.empty;
|