tiny-readdir 2.3.0 → 2.4.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.js CHANGED
@@ -3,6 +3,7 @@ import fs from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import { isFunction, makeCounterPromise } from './utils.js';
5
5
  /* MAIN */
6
+ //TODO: Streamline the type of dirnmaps
6
7
  const readdir = (rootPath, options) => {
7
8
  const followSymlinks = options?.followSymlinks ?? false;
8
9
  const maxDepth = options?.depth ?? Infinity;
@@ -12,14 +13,17 @@ const readdir = (rootPath, options) => {
12
13
  const signal = options?.signal ?? { aborted: false };
13
14
  const directories = [];
14
15
  const directoriesNames = new Set();
16
+ const directoriesNamesToPaths = {};
15
17
  const files = [];
16
18
  const filesNames = new Set();
19
+ const filesNamesToPaths = {};
17
20
  const symlinks = [];
18
21
  const symlinksNames = new Set();
22
+ const symlinksNamesToPaths = {};
19
23
  const map = {};
20
24
  const visited = new Set();
21
- const resultEmpty = { directories: [], directoriesNames: new Set(), files: [], filesNames: new Set(), symlinks: [], symlinksNames: new Set(), map: {} };
22
- const result = { directories, directoriesNames, files, filesNames, symlinks, symlinksNames, map };
25
+ const resultEmpty = { directories: [], directoriesNames: new Set(), directoriesNamesToPaths: {}, files: [], filesNames: new Set(), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set(), symlinksNamesToPaths: {}, map: {} };
26
+ const result = { directories, directoriesNames, directoriesNamesToPaths, files, filesNames, filesNamesToPaths, symlinks, symlinksNames, symlinksNamesToPaths, map };
23
27
  const { promise, increment, decrement } = makeCounterPromise();
24
28
  let foundPaths = 0;
25
29
  const handleDirectory = (dirmap, subPath, name, depth) => {
@@ -30,8 +34,12 @@ const readdir = (rootPath, options) => {
30
34
  foundPaths += 1;
31
35
  dirmap.directories.push(subPath);
32
36
  dirmap.directoriesNames.add(name);
37
+ // dirmap.directoriesNamesToPaths.propertyIsEnumerable(name) || ( dirmap.directoriesNamesToPaths[name] = [] );
38
+ // dirmap.directoriesNamesToPaths[name].push ( subPath );
33
39
  directories.push(subPath);
34
40
  directoriesNames.add(name);
41
+ directoriesNamesToPaths.propertyIsEnumerable(name) || (directoriesNamesToPaths[name] = []);
42
+ directoriesNamesToPaths[name].push(subPath);
35
43
  visited.add(subPath);
36
44
  if (depth >= maxDepth)
37
45
  return;
@@ -47,8 +55,12 @@ const readdir = (rootPath, options) => {
47
55
  foundPaths += 1;
48
56
  dirmap.files.push(subPath);
49
57
  dirmap.filesNames.add(name);
58
+ // dirmap.filesNamesToPaths.propertyIsEnumerable(name) || ( dirmap.filesNamesToPaths[name] = [] );
59
+ // dirmap.filesNamesToPaths[name].push ( subPath );
50
60
  files.push(subPath);
51
61
  filesNames.add(name);
62
+ filesNamesToPaths.propertyIsEnumerable(name) || (filesNamesToPaths[name] = []);
63
+ filesNamesToPaths[name].push(subPath);
52
64
  visited.add(subPath);
53
65
  };
54
66
  const handleSymlink = (dirmap, subPath, name, depth) => {
@@ -59,8 +71,12 @@ const readdir = (rootPath, options) => {
59
71
  foundPaths += 1;
60
72
  dirmap.symlinks.push(subPath);
61
73
  dirmap.symlinksNames.add(name);
74
+ // dirmap.symlinksNamesToPaths.propertyIsEnumerable(name) || ( dirmap.symlinksNamesToPaths[name] = [] );
75
+ // dirmap.symlinksNamesToPaths[name].push ( subPath );
62
76
  symlinks.push(subPath);
63
77
  symlinksNames.add(name);
78
+ symlinksNamesToPaths.propertyIsEnumerable(name) || (symlinksNamesToPaths[name] = []);
79
+ symlinksNamesToPaths[name].push(subPath);
64
80
  visited.add(subPath);
65
81
  if (!followSymlinks)
66
82
  return;
@@ -123,7 +139,7 @@ const readdir = (rootPath, options) => {
123
139
  return decrement();
124
140
  if (!dirents.length)
125
141
  return decrement();
126
- const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), files: [], filesNames: new Set(), symlinks: [], symlinksNames: new Set() };
142
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), directoriesNamesToPaths: {}, files: [], filesNames: new Set(), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set(), symlinksNamesToPaths: {} };
127
143
  handleDirents(dirmap, rootPath, dirents, depth);
128
144
  decrement();
129
145
  });
@@ -141,7 +157,7 @@ const readdir = (rootPath, options) => {
141
157
  if (signal.aborted)
142
158
  return decrement();
143
159
  const name = path.basename(realPath);
144
- const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), files: [], filesNames: new Set(), symlinks: [], symlinksNames: new Set() };
160
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), directoriesNamesToPaths: {}, files: [], filesNames: new Set(), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set(), symlinksNamesToPaths: {} };
145
161
  handleStat(dirmap, realPath, name, stat, depth);
146
162
  decrement();
147
163
  });
package/dist/types.d.ts CHANGED
@@ -11,10 +11,13 @@ type Options = {
11
11
  type ResultDirectory = {
12
12
  directories: string[];
13
13
  directoriesNames: Set<string>;
14
+ directoriesNamesToPaths: Record<string, string[]>;
14
15
  files: string[];
15
16
  filesNames: Set<string>;
17
+ filesNamesToPaths: Record<string, string[]>;
16
18
  symlinks: string[];
17
19
  symlinksNames: Set<string>;
20
+ symlinksNamesToPaths: Record<string, string[]>;
18
21
  };
19
22
  type ResultDirectories = {
20
23
  [path: string]: ResultDirectory;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "tiny-readdir",
3
3
  "repository": "github:fabiospampinato/tiny-readdir",
4
4
  "description": "A simple promisified recursive readdir function.",
5
- "version": "2.3.0",
5
+ "version": "2.4.0",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",
8
8
  "exports": "./dist/index.js",
package/readme.md CHANGED
@@ -31,6 +31,10 @@ console.log ( result.directoriesNames ); // => Set of directories names found
31
31
  console.log ( result.filesNames ); // => Set of files name found
32
32
  console.log ( result.symlinksNames ); // => Set of symlinks names found
33
33
 
34
+ console.log ( result.directoriesNamesToPaths ); // => Record of directories names found to their paths
35
+ console.log ( result.filesNamesToPaths ); // => Record of files names found to their paths
36
+ console.log ( result.symlinksNamesToPaths ); // => Record of symlinks names found to their paths
37
+
34
38
  setTimeout ( () => aborter.abort (), 10000 ); // Aborting if it's going to take longer than 10s
35
39
  ```
36
40
 
package/src/index.ts CHANGED
@@ -8,6 +8,8 @@ import type {Options, ResultDirectory, ResultDirectories, Result} from './types'
8
8
 
9
9
  /* MAIN */
10
10
 
11
+ //TODO: Streamline the type of dirnmaps
12
+
11
13
  const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
12
14
 
13
15
  const followSymlinks = options?.followSymlinks ?? false;
@@ -18,14 +20,17 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
18
20
  const signal = options?.signal ?? { aborted: false };
19
21
  const directories: string[] = [];
20
22
  const directoriesNames: Set<string> = new Set ();
23
+ const directoriesNamesToPaths: Record<string, string[]> = {};
21
24
  const files: string[] = [];
22
25
  const filesNames: Set<string> = new Set ();
26
+ const filesNamesToPaths: Record<string, string[]> = {};
23
27
  const symlinks: string[] = [];
24
28
  const symlinksNames: Set<string> = new Set ();
29
+ const symlinksNamesToPaths: Record<string, string[]> = {};
25
30
  const map: ResultDirectories = {};
26
31
  const visited = new Set<string> ();
27
- const resultEmpty: Result = { directories: [], directoriesNames: new Set (), files: [], filesNames: new Set (), symlinks: [], symlinksNames: new Set (), map: {} };
28
- const result: Result = { directories, directoriesNames, files, filesNames, symlinks, symlinksNames, map };
32
+ const resultEmpty: Result = { directories: [], directoriesNames: new Set (), directoriesNamesToPaths: {}, files: [], filesNames: new Set (), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set (), symlinksNamesToPaths: {}, map: {} };
33
+ const result: Result = { directories, directoriesNames, directoriesNamesToPaths, files, filesNames, filesNamesToPaths, symlinks, symlinksNames, symlinksNamesToPaths, map };
29
34
  const {promise, increment, decrement} = makeCounterPromise ();
30
35
 
31
36
  let foundPaths = 0;
@@ -39,8 +44,12 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
39
44
  foundPaths += 1;
40
45
  dirmap.directories.push ( subPath );
41
46
  dirmap.directoriesNames.add ( name );
47
+ // dirmap.directoriesNamesToPaths.propertyIsEnumerable(name) || ( dirmap.directoriesNamesToPaths[name] = [] );
48
+ // dirmap.directoriesNamesToPaths[name].push ( subPath );
42
49
  directories.push ( subPath );
43
50
  directoriesNames.add ( name );
51
+ directoriesNamesToPaths.propertyIsEnumerable(name) || ( directoriesNamesToPaths[name] = [] );
52
+ directoriesNamesToPaths[name].push ( subPath );
44
53
  visited.add ( subPath );
45
54
 
46
55
  if ( depth >= maxDepth ) return;
@@ -60,8 +69,12 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
60
69
  foundPaths += 1;
61
70
  dirmap.files.push ( subPath );
62
71
  dirmap.filesNames.add ( name );
72
+ // dirmap.filesNamesToPaths.propertyIsEnumerable(name) || ( dirmap.filesNamesToPaths[name] = [] );
73
+ // dirmap.filesNamesToPaths[name].push ( subPath );
63
74
  files.push ( subPath );
64
75
  filesNames.add ( name );
76
+ filesNamesToPaths.propertyIsEnumerable(name) || ( filesNamesToPaths[name] = [] );
77
+ filesNamesToPaths[name].push ( subPath );
65
78
  visited.add ( subPath );
66
79
 
67
80
  };
@@ -75,8 +88,12 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
75
88
  foundPaths += 1;
76
89
  dirmap.symlinks.push ( subPath );
77
90
  dirmap.symlinksNames.add ( name );
91
+ // dirmap.symlinksNamesToPaths.propertyIsEnumerable(name) || ( dirmap.symlinksNamesToPaths[name] = [] );
92
+ // dirmap.symlinksNamesToPaths[name].push ( subPath );
78
93
  symlinks.push ( subPath );
79
94
  symlinksNames.add ( name );
95
+ symlinksNamesToPaths.propertyIsEnumerable(name) || ( symlinksNamesToPaths[name] = [] );
96
+ symlinksNamesToPaths[name].push ( subPath );
80
97
  visited.add ( subPath );
81
98
 
82
99
  if ( !followSymlinks ) return;
@@ -165,7 +182,7 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
165
182
 
166
183
  if ( !dirents.length ) return decrement ();
167
184
 
168
- const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set (), files: [], filesNames: new Set (), symlinks: [], symlinksNames: new Set () };
185
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set (), directoriesNamesToPaths: {}, files: [], filesNames: new Set (), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set (), symlinksNamesToPaths: {} };
169
186
 
170
187
  handleDirents ( dirmap, rootPath, dirents, depth );
171
188
 
@@ -192,7 +209,7 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
192
209
  if ( signal.aborted ) return decrement ();
193
210
 
194
211
  const name = path.basename ( realPath );
195
- const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set (), files: [], filesNames: new Set (), symlinks: [], symlinksNames: new Set () };
212
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set (), directoriesNamesToPaths: {}, files: [], filesNames: new Set (), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set (), symlinksNamesToPaths: {} };
196
213
 
197
214
  handleStat ( dirmap, realPath, name, stat, depth );
198
215
 
package/src/types.ts CHANGED
@@ -16,10 +16,13 @@ type Options = {
16
16
  type ResultDirectory = {
17
17
  directories: string[],
18
18
  directoriesNames: Set<string>,
19
+ directoriesNamesToPaths: Record<string, string[]>,
19
20
  files: string[],
20
21
  filesNames: Set<string>,
22
+ filesNamesToPaths: Record<string, string[]>,
21
23
  symlinks: string[],
22
- symlinksNames: Set<string>
24
+ symlinksNames: Set<string>,
25
+ symlinksNamesToPaths: Record<string, string[]>
23
26
  };
24
27
 
25
28
  type ResultDirectories = {
package/test/index.js CHANGED
@@ -46,66 +46,90 @@ describe ( 'Tiny Readdir', it => {
46
46
  const expected = {
47
47
  directories: [folder1Path, folder2Path, folder1DeepPath, root2Path],
48
48
  directoriesNames: new Set ( [folder1Path, folder2Path, folder1DeepPath, root2Path].map ( toBasename ) ),
49
+ directoriesNamesToPaths: { folder1: [folder1Path], folder2: [folder2Path], deep: [folder1DeepPath], root2: [root2Path] },
49
50
  files: [file1aPath, file1bPath, file2Path, fileDeep1Path],
50
51
  filesNames: new Set ( [file1aPath, file1bPath, file2Path, fileDeep1Path].map ( toBasename ) ),
52
+ filesNamesToPaths: { 'file1a.txt': [file1aPath], 'file1b.txt': [file1bPath], 'file2.txt': [file2Path], 'file1.txt': [fileDeep1Path] },
51
53
  symlinks: [symlink1FromPath, symlink2FromPath],
52
54
  symlinksNames: new Set ( [symlink1FromPath, symlink2FromPath].map ( toBasename ) ),
55
+ symlinksNamesToPaths: { symlink: [symlink1FromPath, symlink2FromPath] },
53
56
  map: {
54
57
  [root1Path]: {
55
58
  directories: [folder1Path, folder2Path],
56
59
  directoriesNames: new Set ( [folder1Path, folder2Path].map ( toBasename ) ),
60
+ directoriesNamesToPaths: {},
57
61
  files: [],
58
62
  filesNames: new Set (),
63
+ filesNamesToPaths: {},
59
64
  symlinks: [symlink1FromPath],
60
- symlinksNames: new Set ( [symlink1FromPath].map ( toBasename ) )
65
+ symlinksNames: new Set ( [symlink1FromPath].map ( toBasename ) ),
66
+ symlinksNamesToPaths: {}
61
67
  },
62
68
  [root2Path]: {
63
69
  directories: [],
64
70
  directoriesNames: new Set (),
71
+ directoriesNamesToPaths: {},
65
72
  files: [],
66
73
  filesNames: new Set (),
74
+ filesNamesToPaths: {},
67
75
  symlinks: [symlink2FromPath],
68
- symlinksNames: new Set ( [symlink2FromPath].map ( toBasename ) )
76
+ symlinksNames: new Set ( [symlink2FromPath].map ( toBasename ) ),
77
+ symlinksNamesToPaths: {}
69
78
  },
70
79
  [folder1Path]: {
71
80
  directories: [folder1DeepPath],
72
81
  directoriesNames: new Set ( [folder1DeepPath].map ( toBasename ) ),
82
+ directoriesNamesToPaths: {},
73
83
  files: [file1aPath, file1bPath],
74
84
  filesNames: new Set ( [file1aPath, file1bPath].map ( toBasename ) ),
85
+ filesNamesToPaths: {},
75
86
  symlinks: [],
76
- symlinksNames: new Set ()
87
+ symlinksNames: new Set (),
88
+ symlinksNamesToPaths: {}
77
89
  },
78
90
  [folder2Path]: {
79
91
  directories: [],
80
92
  directoriesNames: new Set (),
93
+ directoriesNamesToPaths: {},
81
94
  files: [file2Path],
82
95
  filesNames: new Set ( [file2Path].map ( toBasename ) ),
96
+ filesNamesToPaths: {},
83
97
  symlinks: [],
84
- symlinksNames: new Set ()
98
+ symlinksNames: new Set (),
99
+ symlinksNamesToPaths: {}
85
100
  },
86
101
  [folder1DeepPath]: {
87
102
  directories: [],
88
103
  directoriesNames: new Set (),
104
+ directoriesNamesToPaths: {},
89
105
  files: [fileDeep1Path],
90
106
  filesNames: new Set ( [fileDeep1Path].map ( toBasename ) ),
107
+ filesNamesToPaths: {},
91
108
  symlinks: [],
92
- symlinksNames: new Set ()
109
+ symlinksNames: new Set (),
110
+ symlinksNamesToPaths: {}
93
111
  },
94
112
  [symlink1FromPath]: {
95
113
  directories: [root2Path],
96
114
  directoriesNames: new Set ( [root2Path].map ( toBasename ) ),
115
+ directoriesNamesToPaths: {},
97
116
  files: [],
98
117
  filesNames: new Set (),
118
+ filesNamesToPaths: {},
99
119
  symlinks: [],
100
- symlinksNames: new Set ()
120
+ symlinksNames: new Set (),
121
+ symlinksNamesToPaths: {}
101
122
  },
102
123
  [symlink2FromPath]: {
103
124
  directories: [],
104
125
  directoriesNames: new Set (),
126
+ directoriesNamesToPaths: {},
105
127
  files: [],
106
128
  filesNames: new Set (),
129
+ filesNamesToPaths: {},
107
130
  symlinks: [],
108
- symlinksNames: new Set ()
131
+ symlinksNames: new Set (),
132
+ symlinksNamesToPaths: {}
109
133
  }
110
134
  }
111
135
  };
@@ -157,34 +181,46 @@ describe ( 'Tiny Readdir', it => {
157
181
  const expected = {
158
182
  directories: [folder1Path, folder2Path],
159
183
  directoriesNames: new Set ( [folder1Path, folder2Path].map ( toBasename ) ),
184
+ directoriesNamesToPaths: { folder1: [folder1Path], folder2: [folder2Path] },
160
185
  files: [],
161
186
  filesNames: new Set (),
187
+ filesNamesToPaths: {},
162
188
  symlinks: [symlink1FromPath],
163
189
  symlinksNames: new Set ( [symlink1FromPath].map ( toBasename ) ),
190
+ symlinksNamesToPaths: { symlink: [symlink1FromPath] },
164
191
  map: {
165
192
  [root1Path]: {
166
193
  directories: [folder1Path, folder2Path],
167
194
  directoriesNames: new Set ( [folder1Path, folder2Path].map ( toBasename ) ),
195
+ directoriesNamesToPaths: {},
168
196
  files: [],
169
197
  filesNames: new Set (),
198
+ filesNamesToPaths: {},
170
199
  symlinks: [symlink1FromPath],
171
- symlinksNames: new Set ( [symlink1FromPath].map ( toBasename ) )
200
+ symlinksNames: new Set ( [symlink1FromPath].map ( toBasename ) ),
201
+ symlinksNamesToPaths: {}
172
202
  },
173
203
  [folder1Path]: {
174
204
  directories: [],
175
205
  directoriesNames: new Set (),
206
+ directoriesNamesToPaths: {},
176
207
  files: [],
177
208
  filesNames: new Set (),
209
+ filesNamesToPaths: {},
178
210
  symlinks: [],
179
- symlinksNames: new Set ()
211
+ symlinksNames: new Set (),
212
+ symlinksNamesToPaths: {}
180
213
  },
181
214
  [folder2Path]: {
182
215
  directories: [],
183
216
  directoriesNames: new Set (),
217
+ directoriesNamesToPaths: {},
184
218
  files: [],
185
219
  filesNames: new Set (),
220
+ filesNamesToPaths: {},
186
221
  symlinks: [],
187
- symlinksNames: new Set ()
222
+ symlinksNames: new Set (),
223
+ symlinksNamesToPaths: {}
188
224
  }
189
225
  }
190
226
  };