tiny-readdir 2.4.0 → 2.6.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.
@@ -0,0 +1,5 @@
1
+ import type { Callback } from './types';
2
+ declare const NOOP_PROMISE_LIKE: {
3
+ then: (fn: Callback) => void;
4
+ };
5
+ export { NOOP_PROMISE_LIKE };
@@ -0,0 +1,9 @@
1
+ /* IMPORT */
2
+ /* MAIN */
3
+ const NOOP_PROMISE_LIKE = {
4
+ then: (fn) => {
5
+ fn();
6
+ }
7
+ };
8
+ /* EXPORT */
9
+ export { NOOP_PROMISE_LIKE };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- import type { Options, Result } from './types';
1
+ import type { Dirent, Options, Result } from './types';
2
2
  declare const readdir: (rootPath: string, options?: Options) => Promise<Result>;
3
3
  export default readdir;
4
+ export type { Dirent, Options, Result };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /* IMPORT */
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
+ import { NOOP_PROMISE_LIKE } from './constants.js';
4
5
  import { isFunction, makeCounterPromise } from './utils.js';
5
6
  /* MAIN */
6
7
  //TODO: Streamline the type of dirnmaps
@@ -11,6 +12,7 @@ const readdir = (rootPath, options) => {
11
12
  const ignore = options?.ignore ?? (() => false);
12
13
  const isIgnored = isFunction(ignore) ? ignore : (targetPath) => ignore.test(targetPath);
13
14
  const signal = options?.signal ?? { aborted: false };
15
+ const onDirents = options?.onDirents || (() => { });
14
16
  const directories = [];
15
17
  const directoriesNames = new Set();
16
18
  const directoriesNamesToPaths = {};
@@ -139,9 +141,12 @@ const readdir = (rootPath, options) => {
139
141
  return decrement();
140
142
  if (!dirents.length)
141
143
  return decrement();
142
- const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), directoriesNamesToPaths: {}, files: [], filesNames: new Set(), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set(), symlinksNamesToPaths: {} };
143
- handleDirents(dirmap, rootPath, dirents, depth);
144
- decrement();
144
+ const promise = onDirents(dirents) || NOOP_PROMISE_LIKE;
145
+ promise.then(() => {
146
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), directoriesNamesToPaths: {}, files: [], filesNames: new Set(), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set(), symlinksNamesToPaths: {} };
147
+ handleDirents(dirmap, rootPath, dirents, depth);
148
+ decrement();
149
+ });
145
150
  });
146
151
  };
147
152
  const populateResultFromSymlink = async (rootPath, depth) => {
package/dist/types.d.ts CHANGED
@@ -1,4 +1,16 @@
1
1
  type Callback = () => void;
2
+ type PromiseMaybe<T> = Promise<T> | T;
3
+ type Dirent = {
4
+ isFile: () => boolean;
5
+ isDirectory: () => boolean;
6
+ isBlockDevice: () => boolean;
7
+ isCharacterDevice: () => boolean;
8
+ isSymbolicLink: () => boolean;
9
+ isFIFO: () => boolean;
10
+ isSocket: () => boolean;
11
+ name: string;
12
+ path: string;
13
+ };
2
14
  type Options = {
3
15
  depth?: number;
4
16
  limit?: number;
@@ -7,6 +19,7 @@ type Options = {
7
19
  signal?: {
8
20
  aborted: boolean;
9
21
  };
22
+ onDirents?: (dirents: Dirent[]) => PromiseMaybe<undefined>;
10
23
  };
11
24
  type ResultDirectory = {
12
25
  directories: string[];
@@ -25,4 +38,4 @@ type ResultDirectories = {
25
38
  type Result = ResultDirectory & {
26
39
  map: ResultDirectories;
27
40
  };
28
- export type { Callback, Options, ResultDirectory, ResultDirectories, Result };
41
+ export type { Callback, PromiseMaybe, Dirent, Options, ResultDirectory, ResultDirectories, Result };
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.4.0",
5
+ "version": "2.6.0",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",
8
8
  "exports": "./dist/index.js",
package/readme.md CHANGED
@@ -20,7 +20,8 @@ const result = await readdir ( '/foo/bar', {
20
20
  limit: 1_000_000, // Maximum number of files explored, useful as a stop gap in some edge cases
21
21
  followSymlinks: true, // Whether to follow symlinks or not
22
22
  ignore: targetPath => /node_modules/.test ( targetPath ), // Function that if returns true will ignore this particular file or a directory and its descendants
23
- signal: aborter.signal // Optional abort signal, useful for aborting potentially expensive operations
23
+ signal: aborter.signal, // Optional abort signal, useful for aborting potentially expensive operations
24
+ 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
24
25
  });
25
26
 
26
27
  console.log ( result.directories ); // => Array of absolute paths pointing to directories
@@ -0,0 +1,16 @@
1
+
2
+ /* IMPORT */
3
+
4
+ import type {Callback} from './types';
5
+
6
+ /* MAIN */
7
+
8
+ const NOOP_PROMISE_LIKE = {
9
+ then: ( fn: Callback ) => {
10
+ fn ();
11
+ }
12
+ };
13
+
14
+ /* EXPORT */
15
+
16
+ export {NOOP_PROMISE_LIKE};
package/src/index.ts CHANGED
@@ -3,8 +3,9 @@
3
3
 
4
4
  import fs from 'node:fs';
5
5
  import path from 'node:path';
6
+ import {NOOP_PROMISE_LIKE} from './constants';
6
7
  import {isFunction, makeCounterPromise} from './utils';
7
- import type {Options, ResultDirectory, ResultDirectories, Result} from './types';
8
+ import type {Dirent, Options, ResultDirectory, ResultDirectories, Result} from './types';
8
9
 
9
10
  /* MAIN */
10
11
 
@@ -18,6 +19,7 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
18
19
  const ignore = options?.ignore ?? (() => false);
19
20
  const isIgnored = isFunction ( ignore ) ? ignore : ( targetPath: string ) => ignore.test ( targetPath );
20
21
  const signal = options?.signal ?? { aborted: false };
22
+ const onDirents = options?.onDirents || (() => {});
21
23
  const directories: string[] = [];
22
24
  const directoriesNames: Set<string> = new Set ();
23
25
  const directoriesNamesToPaths: Record<string, string[]> = {};
@@ -182,11 +184,17 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
182
184
 
183
185
  if ( !dirents.length ) return decrement ();
184
186
 
185
- const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set (), directoriesNamesToPaths: {}, files: [], filesNames: new Set (), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set (), symlinksNamesToPaths: {} };
187
+ const promise = onDirents ( dirents ) || NOOP_PROMISE_LIKE;
186
188
 
187
- handleDirents ( dirmap, rootPath, dirents, depth );
189
+ promise.then ( () => {
188
190
 
189
- decrement ();
191
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set (), directoriesNamesToPaths: {}, files: [], filesNames: new Set (), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set (), symlinksNamesToPaths: {} };
192
+
193
+ handleDirents ( dirmap, rootPath, dirents, depth );
194
+
195
+ decrement ();
196
+
197
+ });
190
198
 
191
199
  });
192
200
 
@@ -244,3 +252,4 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
244
252
  /* EXPORT */
245
253
 
246
254
  export default readdir;
255
+ export type {Dirent, Options, Result};
package/src/types.ts CHANGED
@@ -3,14 +3,29 @@
3
3
 
4
4
  type Callback = () => void;
5
5
 
6
+ type PromiseMaybe<T> = Promise<T> | T;
7
+
6
8
  /* MAIN */
7
9
 
10
+ type Dirent = {
11
+ isFile: () => boolean,
12
+ isDirectory: () => boolean,
13
+ isBlockDevice: () => boolean,
14
+ isCharacterDevice: () => boolean,
15
+ isSymbolicLink: () => boolean,
16
+ isFIFO: () => boolean,
17
+ isSocket: () => boolean,
18
+ name: string,
19
+ path: string
20
+ };
21
+
8
22
  type Options = {
9
23
  depth?: number,
10
24
  limit?: number,
11
25
  followSymlinks?: boolean,
12
26
  ignore?: (( targetPath: string ) => boolean) | RegExp,
13
- signal?: { aborted: boolean }
27
+ signal?: { aborted: boolean },
28
+ onDirents?: ( dirents: Dirent[] ) => PromiseMaybe<undefined>
14
29
  };
15
30
 
16
31
  type ResultDirectory = {
@@ -35,4 +50,4 @@ type Result = ResultDirectory & {
35
50
 
36
51
  /* EXPORT */
37
52
 
38
- export type {Callback, Options, ResultDirectory, ResultDirectories, Result};
53
+ export type {Callback, PromiseMaybe, Dirent, Options, ResultDirectory, ResultDirectories, Result};