tiny-readdir 2.7.4 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -2
- package/dist/index.js +17 -49
- package/dist/types.d.ts +2 -14
- package/package.json +10 -6
- package/readme.md +1 -11
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Dirent, Options,
|
|
1
|
+
import type { Dirent, Options, Result } from './types.js';
|
|
2
2
|
declare const readdir: (rootPath: string, options?: Options) => Promise<Result>;
|
|
3
3
|
export default readdir;
|
|
4
|
-
export type { Dirent, Options,
|
|
4
|
+
export type { Dirent, Options, Result };
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,6 @@ import makeCounterPromise from 'promise-make-counter';
|
|
|
5
5
|
import { NOOP_PROMISE_LIKE } from './constants.js';
|
|
6
6
|
import { castArray, isFunction } from './utils.js';
|
|
7
7
|
/* MAIN */
|
|
8
|
-
//TODO: Streamline the type of dirmaps
|
|
9
8
|
const readdir = (rootPath, options) => {
|
|
10
9
|
const followSymlinks = options?.followSymlinks ?? false;
|
|
11
10
|
const maxDepth = options?.depth ?? Infinity;
|
|
@@ -16,34 +15,20 @@ const readdir = (rootPath, options) => {
|
|
|
16
15
|
const signal = options?.signal ?? { aborted: false };
|
|
17
16
|
const onDirents = options?.onDirents || (() => { });
|
|
18
17
|
const directories = [];
|
|
19
|
-
const directoriesNames = new Set();
|
|
20
|
-
const directoriesNamesToPaths = {};
|
|
21
18
|
const files = [];
|
|
22
|
-
const filesNames = new Set();
|
|
23
|
-
const filesNamesToPaths = {};
|
|
24
19
|
const symlinks = [];
|
|
25
|
-
const symlinksNames = new Set();
|
|
26
|
-
const symlinksNamesToPaths = {};
|
|
27
|
-
const map = {};
|
|
28
20
|
const visited = new Set();
|
|
29
|
-
const resultEmpty = { directories: [],
|
|
30
|
-
const result = { directories,
|
|
21
|
+
const resultEmpty = { directories: [], files: [], symlinks: [] };
|
|
22
|
+
const result = { directories, files, symlinks };
|
|
31
23
|
const { promise, increment, decrement } = makeCounterPromise();
|
|
32
24
|
let foundPaths = 0;
|
|
33
|
-
const handleDirectory = (
|
|
25
|
+
const handleDirectory = (subPath, depth) => {
|
|
34
26
|
if (visited.has(subPath))
|
|
35
27
|
return;
|
|
36
28
|
if (foundPaths >= maxPaths)
|
|
37
29
|
return;
|
|
38
30
|
foundPaths += 1;
|
|
39
|
-
dirmap.directories.push(subPath);
|
|
40
|
-
dirmap.directoriesNames.add(name);
|
|
41
|
-
// dirmap.directoriesNamesToPaths.propertyIsEnumerable(name) || ( dirmap.directoriesNamesToPaths[name] = [] );
|
|
42
|
-
// dirmap.directoriesNamesToPaths[name].push ( subPath );
|
|
43
31
|
directories.push(subPath);
|
|
44
|
-
directoriesNames.add(name);
|
|
45
|
-
directoriesNamesToPaths.propertyIsEnumerable(name) || (directoriesNamesToPaths[name] = []);
|
|
46
|
-
directoriesNamesToPaths[name].push(subPath);
|
|
47
32
|
visited.add(subPath);
|
|
48
33
|
if (depth >= maxDepth)
|
|
49
34
|
return;
|
|
@@ -51,36 +36,22 @@ const readdir = (rootPath, options) => {
|
|
|
51
36
|
return;
|
|
52
37
|
populateResultFromPath(subPath, depth + 1);
|
|
53
38
|
};
|
|
54
|
-
const handleFile = (
|
|
39
|
+
const handleFile = (subPath) => {
|
|
55
40
|
if (visited.has(subPath))
|
|
56
41
|
return;
|
|
57
42
|
if (foundPaths >= maxPaths)
|
|
58
43
|
return;
|
|
59
44
|
foundPaths += 1;
|
|
60
|
-
dirmap.files.push(subPath);
|
|
61
|
-
dirmap.filesNames.add(name);
|
|
62
|
-
// dirmap.filesNamesToPaths.propertyIsEnumerable(name) || ( dirmap.filesNamesToPaths[name] = [] );
|
|
63
|
-
// dirmap.filesNamesToPaths[name].push ( subPath );
|
|
64
45
|
files.push(subPath);
|
|
65
|
-
filesNames.add(name);
|
|
66
|
-
filesNamesToPaths.propertyIsEnumerable(name) || (filesNamesToPaths[name] = []);
|
|
67
|
-
filesNamesToPaths[name].push(subPath);
|
|
68
46
|
visited.add(subPath);
|
|
69
47
|
};
|
|
70
|
-
const handleSymlink = (
|
|
48
|
+
const handleSymlink = (subPath, depth) => {
|
|
71
49
|
if (visited.has(subPath))
|
|
72
50
|
return;
|
|
73
51
|
if (foundPaths >= maxPaths)
|
|
74
52
|
return;
|
|
75
53
|
foundPaths += 1;
|
|
76
|
-
dirmap.symlinks.push(subPath);
|
|
77
|
-
dirmap.symlinksNames.add(name);
|
|
78
|
-
// dirmap.symlinksNamesToPaths.propertyIsEnumerable(name) || ( dirmap.symlinksNamesToPaths[name] = [] );
|
|
79
|
-
// dirmap.symlinksNamesToPaths[name].push ( subPath );
|
|
80
54
|
symlinks.push(subPath);
|
|
81
|
-
symlinksNames.add(name);
|
|
82
|
-
symlinksNamesToPaths.propertyIsEnumerable(name) || (symlinksNamesToPaths[name] = []);
|
|
83
|
-
symlinksNamesToPaths[name].push(subPath);
|
|
84
55
|
visited.add(subPath);
|
|
85
56
|
if (!followSymlinks)
|
|
86
57
|
return;
|
|
@@ -90,22 +61,22 @@ const readdir = (rootPath, options) => {
|
|
|
90
61
|
return;
|
|
91
62
|
populateResultFromSymlink(subPath, depth + 1);
|
|
92
63
|
};
|
|
93
|
-
const handleStat = (
|
|
64
|
+
const handleStat = (rootPath, stat, depth) => {
|
|
94
65
|
if (signal.aborted)
|
|
95
66
|
return;
|
|
96
67
|
if (isIgnored(rootPath))
|
|
97
68
|
return;
|
|
98
69
|
if (stat.isDirectory()) {
|
|
99
|
-
handleDirectory(
|
|
70
|
+
handleDirectory(rootPath, depth);
|
|
100
71
|
}
|
|
101
72
|
else if (stat.isFile()) {
|
|
102
|
-
handleFile(
|
|
73
|
+
handleFile(rootPath);
|
|
103
74
|
}
|
|
104
75
|
else if (stat.isSymbolicLink()) {
|
|
105
|
-
handleSymlink(
|
|
76
|
+
handleSymlink(rootPath, depth);
|
|
106
77
|
}
|
|
107
78
|
};
|
|
108
|
-
const handleDirent = (
|
|
79
|
+
const handleDirent = (rootPath, dirent, depth) => {
|
|
109
80
|
if (signal.aborted)
|
|
110
81
|
return;
|
|
111
82
|
const separator = (rootPath === path.sep) ? '' : path.sep;
|
|
@@ -114,18 +85,18 @@ const readdir = (rootPath, options) => {
|
|
|
114
85
|
if (isIgnored(subPath))
|
|
115
86
|
return;
|
|
116
87
|
if (dirent.isDirectory()) {
|
|
117
|
-
handleDirectory(
|
|
88
|
+
handleDirectory(subPath, depth);
|
|
118
89
|
}
|
|
119
90
|
else if (dirent.isFile()) {
|
|
120
|
-
handleFile(
|
|
91
|
+
handleFile(subPath);
|
|
121
92
|
}
|
|
122
93
|
else if (dirent.isSymbolicLink()) {
|
|
123
|
-
handleSymlink(
|
|
94
|
+
handleSymlink(subPath, depth);
|
|
124
95
|
}
|
|
125
96
|
};
|
|
126
|
-
const handleDirents = (
|
|
97
|
+
const handleDirents = (rootPath, dirents, depth) => {
|
|
127
98
|
for (let i = 0, l = dirents.length; i < l; i++) {
|
|
128
|
-
handleDirent(
|
|
99
|
+
handleDirent(rootPath, dirents[i], depth);
|
|
129
100
|
}
|
|
130
101
|
};
|
|
131
102
|
const populateResultFromPath = (rootPath, depth) => {
|
|
@@ -145,8 +116,7 @@ const readdir = (rootPath, options) => {
|
|
|
145
116
|
return decrement();
|
|
146
117
|
const promise = onDirents(dirents) || NOOP_PROMISE_LIKE;
|
|
147
118
|
promise.then(() => {
|
|
148
|
-
|
|
149
|
-
handleDirents(dirmap, rootPath, dirents, depth);
|
|
119
|
+
handleDirents(rootPath, dirents, depth);
|
|
150
120
|
decrement();
|
|
151
121
|
});
|
|
152
122
|
});
|
|
@@ -163,9 +133,7 @@ const readdir = (rootPath, options) => {
|
|
|
163
133
|
return decrement();
|
|
164
134
|
if (signal.aborted)
|
|
165
135
|
return decrement();
|
|
166
|
-
|
|
167
|
-
const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), directoriesNamesToPaths: {}, files: [], filesNames: new Set(), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set(), symlinksNamesToPaths: {} };
|
|
168
|
-
handleStat(dirmap, realPath, name, stat, depth);
|
|
136
|
+
handleStat(realPath, stat, depth);
|
|
169
137
|
decrement();
|
|
170
138
|
});
|
|
171
139
|
});
|
package/dist/types.d.ts
CHANGED
|
@@ -22,21 +22,9 @@ type Options = {
|
|
|
22
22
|
};
|
|
23
23
|
onDirents?: (dirents: Dirent[]) => PromiseMaybe<undefined>;
|
|
24
24
|
};
|
|
25
|
-
type
|
|
25
|
+
type Result = {
|
|
26
26
|
directories: string[];
|
|
27
|
-
directoriesNames: Set<string>;
|
|
28
|
-
directoriesNamesToPaths: Record<string, string[]>;
|
|
29
27
|
files: string[];
|
|
30
|
-
filesNames: Set<string>;
|
|
31
|
-
filesNamesToPaths: Record<string, string[]>;
|
|
32
28
|
symlinks: string[];
|
|
33
|
-
symlinksNames: Set<string>;
|
|
34
|
-
symlinksNamesToPaths: Record<string, string[]>;
|
|
35
29
|
};
|
|
36
|
-
type
|
|
37
|
-
[path: string]: ResultDirectory;
|
|
38
|
-
};
|
|
39
|
-
type Result = ResultDirectory & {
|
|
40
|
-
map: ResultDirectories;
|
|
41
|
-
};
|
|
42
|
-
export type { Callback, PromiseMaybe, Dirent, Options, ResultDirectory, ResultDirectories, Result };
|
|
30
|
+
export type { Callback, PromiseMaybe, Dirent, Options, Result };
|
package/package.json
CHANGED
|
@@ -3,18 +3,21 @@
|
|
|
3
3
|
"repository": "github:fabiospampinato/tiny-readdir",
|
|
4
4
|
"description": "A simple promisified recursive readdir function.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "
|
|
6
|
+
"version": "3.0.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"exports": "./dist/index.js",
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"scripts": {
|
|
12
|
+
"benchmark": "tsex benchmark",
|
|
13
|
+
"benchmark:watch": "tsex benchmark --watch",
|
|
14
|
+
"benchmark:prepare": "cd tasks && git clone https://github.com/babel/babel.git",
|
|
12
15
|
"clean": "tsex clean",
|
|
13
16
|
"compile": "tsex compile",
|
|
14
17
|
"compile:watch": "tsex compile --watch",
|
|
15
|
-
"test": "
|
|
18
|
+
"test": "node test/index.js",
|
|
16
19
|
"test:watch": "tsex test --watch",
|
|
17
|
-
"prepublishOnly": "
|
|
20
|
+
"prepublishOnly": "npm run compile && npm run test"
|
|
18
21
|
},
|
|
19
22
|
"keywords": [
|
|
20
23
|
"readdir",
|
|
@@ -27,9 +30,10 @@
|
|
|
27
30
|
"promise-make-counter": "^1.0.2"
|
|
28
31
|
},
|
|
29
32
|
"devDependencies": {
|
|
30
|
-
"@types/node": "^18.19.
|
|
31
|
-
"
|
|
33
|
+
"@types/node": "^18.19.130",
|
|
34
|
+
"benchloop": "^2.1.1",
|
|
35
|
+
"fava": "^0.3.5",
|
|
32
36
|
"tsex": "^4.0.2",
|
|
33
|
-
"typescript": "^5.
|
|
37
|
+
"typescript": "^5.9.3"
|
|
34
38
|
}
|
|
35
39
|
}
|
package/readme.md
CHANGED
|
@@ -29,21 +29,11 @@ const result = await readdir ( '/foo/bar', {
|
|
|
29
29
|
|
|
30
30
|
setTimeout ( () => aborter.abort (), 10_000 ); // Aborting if it's going to take longer than 10s
|
|
31
31
|
|
|
32
|
-
// This is the
|
|
32
|
+
// This is the result object will look like
|
|
33
33
|
|
|
34
34
|
result.directories; // => Array of absolute paths pointing to directories
|
|
35
35
|
result.files; // => Array of absolute paths pointing to files
|
|
36
36
|
result.symlinks; // => Array of absolute paths pointing to symlinks
|
|
37
|
-
|
|
38
|
-
// This is more advanced information we'll get, which is useful in some cases
|
|
39
|
-
|
|
40
|
-
result.directoriesNames; // => Set of directories names found
|
|
41
|
-
result.filesNames; // => Set of files name found
|
|
42
|
-
result.symlinksNames; // => Set of symlinks names found
|
|
43
|
-
|
|
44
|
-
result.directoriesNamesToPaths; // => Record of directories names found to their paths
|
|
45
|
-
result.filesNamesToPaths; // => Record of files names found to their paths
|
|
46
|
-
result.symlinksNamesToPaths; // => Record of symlinks names found to their paths
|
|
47
37
|
```
|
|
48
38
|
|
|
49
39
|
## License
|