walkdir.d.ts
7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { EventEmitter } from "events";
import { Stats } from "fs";
/*~ This declaration specifies that the function
*~ is the exported object from the file
*/
export = walkdir;
declare function walkdir(
path:string,
options:{sync:true,return_object:true}&walkdir.WalkOptions,
eventListener?:walkdir.WalkEventListener)
:{[path:string]:Stats};
declare function walkdir(
path:string,
options:{sync:true,return_object?:false}&walkdir.WalkOptions,
eventListener?:walkdir.WalkEventListener)
:string[];
declare function walkdir(
path:string,
options?:({sync?:false}&walkdir.WalkOptions)|walkdir.WalkEventListener,
eventListener?:walkdir.WalkEventListener)
:walkdir.WalkEmitter;
declare function walkdir(
path:string,
options?:walkdir.WalkOptions|walkdir.WalkEventListener,
eventListener?:walkdir.WalkEventListener)
:walkdir.WalkEmitter|string[]|{[path:string]:Stats};
/*~ If you want to expose types from your module as well, you can
*~ place them in this block. Often you will want to describe the
*~ shape of the return type of the function; that type should
*~ be declared in here, as this example shows.
*/
declare namespace walkdir {
export type WalkOptions = {
/**
* follow symlinks. default FALSE
*/
"follow_symlinks"?: boolean,
/**
* only go one level deep. convenience param.
*/
"no_recurse"?: boolean,
/**
* only travel to max depth. emits an error if hit.
*/
"max_depth"?: number,
/**
* on filesystems where inodes are not unique like windows (or perhaps hardlinks) some files may not be emitted due to inode collision.
* turning off this behavior may be required but at the same time may lead to hitting max_depth via link loop.
*/
"track_inodes"?: boolean;
/**
* make this syncronous. the same as calling walkdir.sync
*/
"sync"?:boolean,
/**
* return an object of {path:stat} instead of just the resolved path names
*/
"return_object"?: boolean, // if true the sync return will be in {path:stat} format instead of [path,path,...]
/**
* dont build up an internal list or object of all of the paths. this can be an important optimization for listing HUGE trees.
*/
"no_return"?: boolean,
/**
* filter. filter an array of paths from readdir
*/
"filter"?:(directory:string,files:string[])=>string[]|Promise<string[]>,
/**
* provide an alternate implementation of fs like graceful-fs
*/
"fs"?:any,
/***
* default True. if false this will use stat insteqad of lstat and not find links at all.
*/
"find_links"?:boolean,
}
export type WalkEmitter = EventEmitter&{
/**
* cancel a walk in progress
*/
end():void
/**
* pause the walk
*/
pause():void;
/**
* resume the walk
*/
resume():void;
/**
* pass paths to ignore for the remainder of the walk. directories ignored will not have events emitted for any of their children.
* @param paths string|string[]
*/
ignore(paths:string|string[]):void;
/**
* emitted if there is an error from the file system reading the initial or target directory
*/
on(event:"error",listener:(error:Error)=>void):WalkEmitter;
/**
* emitted when there is an error from the filesystem reading as nested path.
*/
on(event:"fail",listener:(path:string,error:Error)=>void):WalkEmitter;
/**
* the stat of the target directory. not emitted through any other event.
*/
on(event:"targetdirectory",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
/**
* helpful event that lets you know if a directory is empty
*/
on(event:"empty",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
/**
* found a path. this is the expected use case. every path for everything inside target directory is emitted here.
*/
on(event:"path",listener:(this:WalkEmitter,path:string,stat:Stats,depth:number)=>void):WalkEmitter;
/**
* found a directory
*/
on(event:"directory",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
/**
* found a file
*/
on(event:"file",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
/**
* found a symlink
*/
on(event:"link",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
/**
* found a socket
*/
on(event:"socket",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
/**
* found a fifo
*/
on(event:"fifo",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
/**
* found a block device / disk
*/
on(event:"blockdevice",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
/**
* found a character device / tty / terminal
*/
on(event:"characterdevice",listener:(path:string,stat:Stats,depth:number)=>void):WalkEmitter;
};
export type WalkEventListener = (this:WalkEmitter,path:string,stat:Stats,ignore:(path:string|string[])=>void)=>void
// same a walkdir
export function find(
path:string,
options:{sync:true,return_object:true}&walkdir.WalkOptions,
eventListener?:walkdir.WalkEventListener)
:{[path:string]:Stats};
export function find(
path:string,
options:{sync:true,return_object?:false}&walkdir.WalkOptions,
eventListener?:walkdir.WalkEventListener)
:string[];
export function find(
path:string,
options?:({sync?:false}&walkdir.WalkOptions)|walkdir.WalkEventListener,
eventListener?:walkdir.WalkEventListener)
:walkdir.WalkEmitter;
//always sync:true but otherwise the same as walkdir
export function sync(path:string,options:walkdir.WalkOptions&{return_object:true},eventListener?:walkdir.WalkEventListener):{[path:string]:Stats};
export function sync(path:string,options?:walkdir.WalkOptions&{return_object?:false},eventListener?:walkdir.WalkEventListener):string[];
export function sync(path:string,options?:walkdir.WalkOptions&{return_object?:boolean},eventListener?:walkdir.WalkEventListener):string[]|{[path:string]:Stats};
export function sync(path:string,eventListener?:walkdir.WalkEventListener):string[];
// always sync:false. a promise of whatever is the same as walkdir.
export function async(path:string,options:walkdir.WalkOptions&{return_object:true},eventListener?:walkdir.WalkEventListener):Promise<{[path:string]:Stats}>;
export function async(path:string,options?:walkdir.WalkOptions&{return_object?:false},eventListener?:walkdir.WalkEventListener):Promise<string[]>;
export function async(path:string,options?:walkdir.WalkOptions&{return_object?:boolean},eventListener?:walkdir.WalkEventListener):Promise<string[]>|Promise<{[path:string]:Stats}>;
}