| 1 |
/** |
| 2 |
* Recursive version of readdir. Exposes a streaming API and promise API. |
| 3 |
* Streaming API allows to use a small amount of RAM. |
| 4 |
* |
| 5 |
* @module |
| 6 |
* @example |
| 7 |
```js |
| 8 |
import readdirp from 'readdirp'; |
| 9 |
for await (const entry of readdirp('.')) { |
| 10 |
const {path} = entry; |
| 11 |
console.log(`${JSON.stringify({path})}`); |
| 12 |
} |
| 13 |
``` |
| 14 |
*/ |
| 15 |
/*! readdirp - MIT License (c) 2012-2019 Thorsten Lorenz, Paul Miller (https://paulmillr.com) */ |
| 16 |
import type { Dirent, Stats } from 'node:fs'; |
| 17 |
import { Readable } from 'node:stream'; |
| 18 |
/** Path in file system. */ |
| 19 |
export type Path = string; |
| 20 |
/** Emitted entry. Contains relative & absolute path, basename, and either stats or dirent. */ |
| 21 |
export interface EntryInfo { |
| 22 |
path: string; |
| 23 |
fullPath: string; |
| 24 |
stats?: Stats; |
| 25 |
dirent?: Dirent; |
| 26 |
basename: string; |
| 27 |
} |
| 28 |
/** Path or dir entries (files) */ |
| 29 |
export type PathOrDirent = Dirent | Path; |
| 30 |
/** Filterer for files */ |
| 31 |
export type Tester = (entryInfo: EntryInfo) => boolean; |
| 32 |
export type Predicate = string[] | string | Tester; |
| 33 |
export declare const EntryTypes: { |
| 34 |
readonly FILE_TYPE: "files"; |
| 35 |
readonly DIR_TYPE: "directories"; |
| 36 |
readonly FILE_DIR_TYPE: "files_directories"; |
| 37 |
readonly EVERYTHING_TYPE: "all"; |
| 38 |
}; |
| 39 |
export type EntryType = (typeof EntryTypes)[keyof typeof EntryTypes]; |
| 40 |
/** |
| 41 |
* Options for readdirp. |
| 42 |
* * type: files, directories, or both |
| 43 |
* * lstat: whether to use symlink-friendly stat |
| 44 |
* * depth: max depth |
| 45 |
* * alwaysStat: whether to use stat (more resources) or dirent |
| 46 |
* * highWaterMark: streaming param, specifies max amount of resources per entry |
| 47 |
*/ |
| 48 |
export type ReaddirpOptions = { |
| 49 |
root: string; |
| 50 |
fileFilter?: Predicate; |
| 51 |
directoryFilter?: Predicate; |
| 52 |
type?: EntryType; |
| 53 |
lstat?: boolean; |
| 54 |
depth?: number; |
| 55 |
alwaysStat?: boolean; |
| 56 |
highWaterMark?: number; |
| 57 |
}; |
| 58 |
/** Directory entry. Contains path, depth count, and files. */ |
| 59 |
export interface DirEntry { |
| 60 |
/** Undefined when the directory could not be read (a 'warn' was emitted). */ |
| 61 |
files: PathOrDirent[] | undefined; |
| 62 |
depth: number; |
| 63 |
path: Path; |
| 64 |
} |
| 65 |
/** Readable readdir stream, emitting new files as they're being listed. */ |
| 66 |
interface PendingDir { |
| 67 |
path: Path; |
| 68 |
depth: number; |
| 69 |
pending?: Promise<DirEntry>; |
| 70 |
} |
| 71 |
export declare class ReaddirpStream extends Readable { |
| 72 |
/** |
| 73 |
* Directories discovered but not yet emitted from. Listings are read |
| 74 |
* lazily (on pop, plus one prefetch) instead of eagerly on discovery: |
| 75 |
* keeping whole listings for every queued dir balloons RAM on wide trees. |
| 76 |
*/ |
| 77 |
parents: PendingDir[]; |
| 78 |
reading: boolean; |
| 79 |
parent?: DirEntry; |
| 80 |
_stat: Function; |
| 81 |
_maxDepth: number; |
| 82 |
_wantsDir: boolean; |
| 83 |
_wantsFile: boolean; |
| 84 |
_wantsEverything: boolean; |
| 85 |
_root: Path; |
| 86 |
_isDirent: boolean; |
| 87 |
_statsProp: 'dirent' | 'stats'; |
| 88 |
_rdOptions: { |
| 89 |
encoding: 'utf8'; |
| 90 |
withFileTypes: boolean; |
| 91 |
}; |
| 92 |
_fileFilter: Tester; |
| 93 |
_directoryFilter: Tester; |
| 94 |
_relStart: number; |
| 95 |
constructor(options?: Partial<ReaddirpOptions>); |
| 96 |
_read(batch: number): Promise<void>; |
| 97 |
_exploreDir(path: Path, depth: number): Promise<DirEntry>; |
| 98 |
_formatEntry(dirent: PathOrDirent, path: Path): EntryInfo | undefined | Promise<EntryInfo | undefined>; |
| 99 |
_onError(err: Error): void; |
| 100 |
_getEntryType(entry: EntryInfo): '' | 'file' | 'directory' | Promise<'' | 'file' | 'directory'>; |
| 101 |
_getSymlinkEntryType(entry: EntryInfo): Promise<'' | 'file' | 'directory'>; |
| 102 |
_includeAsFile(entry: EntryInfo): boolean | undefined; |
| 103 |
} |
| 104 |
/** |
| 105 |
* Streaming version: Reads all files and directories in given root recursively. |
| 106 |
* Consumes ~constant small amount of RAM. |
| 107 |
* @param root Root directory |
| 108 |
* @param options Options to specify root (start directory), filters and recursion depth |
| 109 |
*/ |
| 110 |
export declare function readdirp(root: Path, options?: Partial<ReaddirpOptions>): ReaddirpStream; |
| 111 |
/** |
| 112 |
* Promise version: Reads all files and directories in given root recursively. |
| 113 |
* Compared to streaming version, will consume a lot of RAM e.g. when 1 million files are listed. |
| 114 |
* @returns array of paths and their entry infos |
| 115 |
*/ |
| 116 |
export declare function readdirpPromise(root: Path, options?: Partial<ReaddirpOptions>): Promise<EntryInfo[]>; |
| 117 |
export default readdirp; |