1 import { EventEmitter } from "events";
2 import { Stats } from "fs";
5 /*~ This declaration specifies that the function
6 *~ is the exported object from the file
11 declare function walkdir(
13 options:{sync:true,return_object:true}&walkdir.WalkOptions,
14 eventListener?:walkdir.WalkEventListener)
15 :{[path:string]:Stats};
17 declare function walkdir(
19 options:{sync:true,return_object?:false}&walkdir.WalkOptions,
20 eventListener?:walkdir.WalkEventListener)
23 declare function walkdir(
25 options?:({sync?:false}&walkdir.WalkOptions)|walkdir.WalkEventListener,
26 eventListener?:walkdir.WalkEventListener)
29 declare function walkdir(
31 options?:walkdir.WalkOptions|walkdir.WalkEventListener,
32 eventListener?:walkdir.WalkEventListener)
33 :walkdir.WalkEmitter|string[]|{[path:string]:Stats};
35 /*~ If you want to expose types from your module as well, you can
36 *~ place them in this block. Often you will want to describe the
37 *~ shape of the return type of the function; that type should
38 *~ be declared in here, as this example shows.
40 declare namespace walkdir {
41 export type WalkOptions = {
43 * follow symlinks. default FALSE
45 "follow_symlinks"?: boolean,
47 * only go one level deep. convenience param.
49 "no_recurse"?: boolean,
51 * only travel to max depth. emits an error if hit.
55 * on filesystems where inodes are not unique like windows (or perhaps hardlinks) some files may not be emitted due to inode collision.
56 * turning off this behavior may be required but at the same time may lead to hitting max_depth via link loop.
58 "track_inodes"?: boolean;
60 * make this syncronous. the same as calling walkdir.sync
64 * return an object of {path:stat} instead of just the resolved path names
66 "return_object"?: boolean, // if true the sync return will be in {path:stat} format instead of [path,path,...]
68 * dont build up an internal list or object of all of the paths. this can be an important optimization for listing HUGE trees.
70 "no_return"?: boolean,
72 * filter. filter an array of paths from readdir
74 "filter"?:(directory:string,files:string[])=>string[]|Promise<string[]>,
76 * provide an alternate implementation of fs like graceful-fs
80 * default True. if false this will use stat insteqad of lstat and not find links at all.
82 "find_links"?:boolean,
85 export type WalkEmitter = EventEmitter&{
87 * cancel a walk in progress
99 * pass paths to ignore for the remainder of the walk. directories ignored will not have events emitted for any of their children.
100 * @param paths string|string[]
102 ignore(paths:string|string[]):void;
105 * emitted if there is an error from the file system reading the initial or target directory
107 on(event:"error",listener:(error:Error)=>void):WalkEmitter;
109 * emitted when there is an error from the filesystem reading as nested path.
111 on(event:"fail",listener:(path:string,error:Error)=>void):WalkEmitter;
113 * the stat of the target directory. not emitted through any other event.
115 on(event:"targetdirectory",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
117 * helpful event that lets you know if a directory is empty
119 on(event:"empty",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
121 * found a path. this is the expected use case. every path for everything inside target directory is emitted here.
123 on(event:"path",listener:(this:WalkEmitter,path:string,stat:Stats,depth:number)=>void):WalkEmitter;
127 on(event:"directory",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
131 on(event:"file",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
135 on(event:"link",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
139 on(event:"socket",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
143 on(event:"fifo",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
145 * found a block device / disk
147 on(event:"blockdevice",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
149 * found a character device / tty / terminal
151 on(event:"characterdevice",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
154 export type WalkEventListener = (this:WalkEmitter,path:string,stat:Stats,ignore:(path:string|string[])=>void)=>void
157 export function find(
159 options:{sync:true,return_object:true}&walkdir.WalkOptions,
160 eventListener?:walkdir.WalkEventListener)
161 :{[path:string]:Stats};
163 export function find(
165 options:{sync:true,return_object?:false}&walkdir.WalkOptions,
166 eventListener?:walkdir.WalkEventListener)
169 export function find(
171 options?:({sync?:false}&walkdir.WalkOptions)|walkdir.WalkEventListener,
172 eventListener?:walkdir.WalkEventListener)
173 :walkdir.WalkEmitter;
176 //always sync:true but otherwise the same as walkdir
177 export function sync(path:string,options:walkdir.WalkOptions&{return_object:true},eventListener?:walkdir.WalkEventListener):{[path:string]:Stats};
178 export function sync(path:string,options?:walkdir.WalkOptions&{return_object?:false},eventListener?:walkdir.WalkEventListener):string[];
179 export function sync(path:string,options?:walkdir.WalkOptions&{return_object?:boolean},eventListener?:walkdir.WalkEventListener):string[]|{[path:string]:Stats};
181 // always sync:false. a promise of whatever is the same as walkdir.
182 export function async(path:string,options:walkdir.WalkOptions&{return_object:true},eventListener?:walkdir.WalkEventListener):Promise<{[path:string]:Stats}>;
183 export function async(path:string,options?:walkdir.WalkOptions&{return_object?:false},eventListener?:walkdir.WalkEventListener):Promise<string[]>;
184 export function async(path:string,options?:walkdir.WalkOptions&{return_object?:boolean},eventListener?:walkdir.WalkEventListener):Promise<string[]>|Promise<{[path:string]:Stats}>;