tiny-readdir 2.7.4 → 3.1.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 +20 -52
- package/dist/types.d.ts +6 -16
- package/package.json +9 -5
- package/readme.md +3 -13
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,45 +5,30 @@ 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;
|
|
12
11
|
const maxPaths = options?.limit ?? Infinity;
|
|
13
12
|
const ignore = options?.ignore ?? [];
|
|
14
13
|
const ignores = castArray(ignore).map(ignore => isFunction(ignore) ? ignore : (targetPath) => ignore.test(targetPath));
|
|
15
|
-
const isIgnored = (targetPath) => ignores.some(ignore => ignore(targetPath));
|
|
14
|
+
const isIgnored = (targetPath, targetContext) => ignores.some(ignore => ignore(targetPath, targetContext));
|
|
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,42 +61,42 @@ 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
|
-
if (isIgnored(rootPath))
|
|
67
|
+
if (isIgnored(rootPath, stat))
|
|
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;
|
|
112
83
|
const name = dirent.name;
|
|
113
84
|
const subPath = `${rootPath}${separator}${name}`;
|
|
114
|
-
if (isIgnored(subPath))
|
|
85
|
+
if (isIgnored(subPath, dirent))
|
|
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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
type Callback = () => void;
|
|
2
2
|
type ArrayMaybe<T> = T[] | T;
|
|
3
3
|
type PromiseMaybe<T> = Promise<T> | T;
|
|
4
|
-
type
|
|
4
|
+
type DirentLike = {
|
|
5
5
|
isFile: () => boolean;
|
|
6
6
|
isDirectory: () => boolean;
|
|
7
7
|
isBlockDevice: () => boolean;
|
|
@@ -9,6 +9,8 @@ type Dirent = {
|
|
|
9
9
|
isSymbolicLink: () => boolean;
|
|
10
10
|
isFIFO: () => boolean;
|
|
11
11
|
isSocket: () => boolean;
|
|
12
|
+
};
|
|
13
|
+
type Dirent = DirentLike & {
|
|
12
14
|
name: string;
|
|
13
15
|
path: string;
|
|
14
16
|
};
|
|
@@ -16,27 +18,15 @@ type Options = {
|
|
|
16
18
|
depth?: number;
|
|
17
19
|
limit?: number;
|
|
18
20
|
followSymlinks?: boolean;
|
|
19
|
-
ignore?: ArrayMaybe<((targetPath: string) => boolean) | RegExp>;
|
|
21
|
+
ignore?: ArrayMaybe<((targetPath: string, targetContext: DirentLike) => boolean) | RegExp>;
|
|
20
22
|
signal?: {
|
|
21
23
|
aborted: boolean;
|
|
22
24
|
};
|
|
23
25
|
onDirents?: (dirents: Dirent[]) => PromiseMaybe<undefined>;
|
|
24
26
|
};
|
|
25
|
-
type
|
|
27
|
+
type Result = {
|
|
26
28
|
directories: string[];
|
|
27
|
-
directoriesNames: Set<string>;
|
|
28
|
-
directoriesNamesToPaths: Record<string, string[]>;
|
|
29
29
|
files: string[];
|
|
30
|
-
filesNames: Set<string>;
|
|
31
|
-
filesNamesToPaths: Record<string, string[]>;
|
|
32
30
|
symlinks: string[];
|
|
33
|
-
symlinksNames: Set<string>;
|
|
34
|
-
symlinksNamesToPaths: Record<string, string[]>;
|
|
35
|
-
};
|
|
36
|
-
type ResultDirectories = {
|
|
37
|
-
[path: string]: ResultDirectory;
|
|
38
|
-
};
|
|
39
|
-
type Result = ResultDirectory & {
|
|
40
|
-
map: ResultDirectories;
|
|
41
31
|
};
|
|
42
|
-
export type { Callback, PromiseMaybe, Dirent, Options,
|
|
32
|
+
export type { Callback, PromiseMaybe, DirentLike, 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.1.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
18
|
"test": "tsex test",
|
|
16
19
|
"test:watch": "tsex test --watch",
|
|
17
|
-
"prepublishOnly": "
|
|
20
|
+
"prepublishOnly": "npm run compile && node test/index.js"
|
|
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
|
@@ -20,30 +20,20 @@ const result = await readdir ( '/foo/bar', {
|
|
|
20
20
|
depth: 20, // Maximum depth to look at
|
|
21
21
|
limit: 1_000_000, // Maximum number of files explored, useful as a stop gap in some edge cases
|
|
22
22
|
followSymlinks: true, // Whether to follow symlinks or not
|
|
23
|
-
ignore: targetPath => /node_modules/.test ( targetPath ), // Function that if returns true will ignore this particular file or a directory and its descendants
|
|
23
|
+
ignore: ( targetPath, targetContext ) => /node_modules/.test ( targetPath ), // Function that if returns true will ignore this particular file or a directory and its descendants
|
|
24
24
|
signal: aborter.signal, // Optional abort signal, useful for aborting potentially expensive operations
|
|
25
25
|
onDirents: dirents => console.log ( dirents ) // Optional callback that will be called as soon as new dirents are available, useful for example for discovering ".gitignore" files while searching
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
// This is how we would abort the
|
|
28
|
+
// This is how we would abort the recursive read after 10s
|
|
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 what 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
|