@igorskyflyer/recursive-readdir
Version:
📖 Fast, type-safe recursive directory reader for Node.js with depth control, entry filtering, and sync/async APIs. 📁
133 lines (132 loc) • 4.36 kB
TypeScript
/** Used for maxDepth parameter
* @readonly
*/
export declare enum Depth {
/** All subdirectories */
All = -1,
/** Only the root directory */
Root = 0
}
/** Used for entries filtering parameter
* @readonly
*/
export declare enum Entry {
/** All directory entries - files and subdirectories */
All = 2730,
/** Only files */
FilesOnly = 3003,
/** Only subdirectories */
DirectoriesOnly = 3276
}
/**
* Represents information about a directory entry during recursive traversal.
*/
export type RecursiveFilterParams = {
/** Path of directory entry */
path: string;
/** Indicates whether the entry is a directory */
isDirectory: boolean;
/** True if the entry was skipped, often due to lack of permissions */
wasSkipped: boolean;
};
/**
* Predicate function type for filtering file-system entries.
*/
export type FilterCallback = (
/** A `RecursiveFilterParams` object containing details about the entry */
entry: RecursiveFilterParams) => boolean;
/**
* Options for configuring recursive directory reading.
*/
export type RecursiveDirOptions = {
/** Function used for filtering when traversing the provided directory */
filter?: FilterCallback;
/**
* Indicates which entries to show: files-only, directories-only, or all (default)
* Possible values:
* - Entry.FilesOnly
* - Entry.DirectoriesOnly
* - Entry.All (default)
*/
entries?: Entry;
/**
* The level of child directories to read
* Possible values:
* - Depth.All
* - Depth.Root (default)
* - any number >= 0
*/
maxDepth?: Depth;
/** Whether a trailing slash should be added to directory paths */
addTrailingSlash?: boolean;
};
/**
* Asynchronously gets files/directories inside the given directory.
* @param {string} directory the directory whose files/directories should be listed
* @param {RecursiveDirOptions} [options] additional options
* @returns {Promise<string[]>} returns Promise\<string[]\>
*/
export declare function readDir(directory: string, options: RecursiveDirOptions): Promise<string[]>;
/**
* Synchronously gets files/directories inside the given directory.
* @param {string} directory the directory whose files/directories should be listed
* @param {RecursiveDirOptions} [options] additional options
* @returns {string[]} returns string[]
*/
export declare function readDirSync(directory: string, options: RecursiveDirOptions): string[];
/**
* RecursiveDir class
* @class
*/
export declare class RecursiveDir {
#private;
constructor();
/**
* Synchronously gets files/directories inside the given directory.
* @param {string} directory the directory whose files/directories should be listed
* @returns {string[]} returns string[]
*/
readDirSync(directory: string): string[];
/**
* Asynchronously gets files/directories inside the given directory.
* @param {string} directory the directory whose files/directories should be listed
* @returns {Promise<string[]>} returns Promise\<string[]\>
*/
readDir(directory: string): Promise<string[]>;
/**
* Sets the **entries** property which controls which entries to show, files-only, directories-only or all (**default**), use any of the following values,
*
* - `Entry.FilesOnly`,
* - `Entry.DirectoriesOnly`,
* - `Entry.All` (**default**),
* @param {Entry} value
* @returns {RecursiveDir}
*/
entries(value: Entry): RecursiveDir;
/**
* Sets **maxDepth** which controls how many child directories'
* entries are being listed.
*
* Possible values:
*
* - `Depth.All`
* - `Depth.Root` (**default**)
* - any arbitrary value that conforms the condition `maxDepth >= 0`.
* @param {Depth} value
* @returns {RecursiveDir}
*/
maxDepth(value: Depth): RecursiveDir;
/**
* Sets **filter** predicate function used for filtering
* directory entries (directories/files)
* @param {FilterCallback} value
* @returns {RecursiveDir}
*/
filter(value: FilterCallback): RecursiveDir;
/**
* Sets whether a trailing slash should be added to directory entries.
* @param {boolean} value
* @returns {RecursiveDir}
*/
addTrailingSlash(value: boolean): RecursiveDir;
}