1
- module . exports = rimraf
2
- rimraf . sync = rimrafSync
3
-
4
1
const assert = require ( "assert" )
5
2
const path = require ( "path" )
6
3
const fs = require ( "fs" )
@@ -21,7 +18,7 @@ let timeout = 0
21
18
22
19
const isWindows = ( process . platform === "win32" )
23
20
24
- function defaults ( options ) {
21
+ const defaults = options => {
25
22
const methods = [
26
23
'unlink' ,
27
24
'chmod' ,
@@ -30,7 +27,7 @@ function defaults (options) {
30
27
'rmdir' ,
31
28
'readdir'
32
29
]
33
- methods . forEach ( function ( m ) {
30
+ methods . forEach ( m => {
34
31
options [ m ] = options [ m ] || fs [ m ]
35
32
m = m + 'Sync'
36
33
options [ m ] = options [ m ] || fs [ m ]
@@ -48,7 +45,7 @@ function defaults (options) {
48
45
options . glob = options . glob || defaultGlobOpts
49
46
}
50
47
51
- function rimraf ( p , options , cb ) {
48
+ const rimraf = ( p , options , cb ) => {
52
49
if ( typeof options === 'function' ) {
53
50
cb = options
54
51
options = { }
@@ -66,47 +63,33 @@ function rimraf (p, options, cb) {
66
63
let errState = null
67
64
let n = 0
68
65
69
- if ( options . disableGlob || ! glob . hasMagic ( p ) )
70
- return afterGlob ( null , [ p ] )
71
-
72
- options . lstat ( p , function ( er , stat ) {
73
- if ( ! er )
74
- return afterGlob ( null , [ p ] )
75
-
76
- glob ( p , options . glob , afterGlob )
77
- } )
78
-
79
- function next ( er ) {
66
+ const next = ( er ) => {
80
67
errState = errState || er
81
68
if ( -- n === 0 )
82
69
cb ( errState )
83
70
}
84
71
85
- function afterGlob ( er , results ) {
72
+ const afterGlob = ( er , results ) => {
86
73
if ( er )
87
74
return cb ( er )
88
75
89
76
n = results . length
90
77
if ( n === 0 )
91
78
return cb ( )
92
79
93
- results . forEach ( function ( p ) {
94
- rimraf_ ( p , options , function CB ( er ) {
80
+ results . forEach ( p => {
81
+ const CB = ( er ) => {
95
82
if ( er ) {
96
83
if ( ( er . code === "EBUSY" || er . code === "ENOTEMPTY" || er . code === "EPERM" ) &&
97
84
busyTries < options . maxBusyTries ) {
98
85
busyTries ++
99
86
// try again, with the same exact callback as this one.
100
- return setTimeout ( function ( ) {
101
- rimraf_ ( p , options , CB )
102
- } , busyTries * 100 )
87
+ return setTimeout ( ( ) => rimraf_ ( p , options , CB ) , busyTries * 100 )
103
88
}
104
89
105
90
// this one won't happen if graceful-fs is used.
106
91
if ( er . code === "EMFILE" && timeout < options . emfileWait ) {
107
- return setTimeout ( function ( ) {
108
- rimraf_ ( p , options , CB )
109
- } , timeout ++ )
92
+ return setTimeout ( ( ) => rimraf_ ( p , options , CB ) , timeout ++ )
110
93
}
111
94
112
95
// already gone
@@ -115,9 +98,21 @@ function rimraf (p, options, cb) {
115
98
116
99
timeout = 0
117
100
next ( er )
118
- } )
101
+ }
102
+ rimraf_ ( p , options , CB )
119
103
} )
120
104
}
105
+
106
+ if ( options . disableGlob || ! glob . hasMagic ( p ) )
107
+ return afterGlob ( null , [ p ] )
108
+
109
+ options . lstat ( p , ( er , stat ) => {
110
+ if ( ! er )
111
+ return afterGlob ( null , [ p ] )
112
+
113
+ glob ( p , options . glob , afterGlob )
114
+ } )
115
+
121
116
}
122
117
123
118
// Two possible strategies.
@@ -131,14 +126,14 @@ function rimraf (p, options, cb) {
131
126
//
132
127
// If anyone ever complains about this, then I guess the strategy could
133
128
// be made configurable somehow. But until then, YAGNI.
134
- function rimraf_ ( p , options , cb ) {
129
+ const rimraf_ = ( p , options , cb ) => {
135
130
assert ( p )
136
131
assert ( options )
137
132
assert ( typeof cb === 'function' )
138
133
139
134
// sunos lets the root user unlink directories, which is... weird.
140
135
// so we have to lstat here and make sure it's not a dir.
141
- options . lstat ( p , function ( er , st ) {
136
+ options . lstat ( p , ( er , st ) => {
142
137
if ( er && er . code === "ENOENT" )
143
138
return cb ( null )
144
139
@@ -149,7 +144,7 @@ function rimraf_ (p, options, cb) {
149
144
if ( st && st . isDirectory ( ) )
150
145
return rmdir ( p , options , er , cb )
151
146
152
- options . unlink ( p , function ( er ) {
147
+ options . unlink ( p , er => {
153
148
if ( er ) {
154
149
if ( er . code === "ENOENT" )
155
150
return cb ( null )
@@ -165,18 +160,18 @@ function rimraf_ (p, options, cb) {
165
160
} )
166
161
}
167
162
168
- function fixWinEPERM ( p , options , er , cb ) {
163
+ const fixWinEPERM = ( p , options , er , cb ) => {
169
164
assert ( p )
170
165
assert ( options )
171
166
assert ( typeof cb === 'function' )
172
167
if ( er )
173
168
assert ( er instanceof Error )
174
169
175
- options . chmod ( p , 0o666 , function ( er2 ) {
170
+ options . chmod ( p , 0o666 , er2 => {
176
171
if ( er2 )
177
172
cb ( er2 . code === "ENOENT" ? null : er )
178
173
else
179
- options . stat ( p , function ( er3 , stats ) {
174
+ options . stat ( p , ( er3 , stats ) => {
180
175
if ( er3 )
181
176
cb ( er3 . code === "ENOENT" ? null : er )
182
177
else if ( stats . isDirectory ( ) )
@@ -187,7 +182,7 @@ function fixWinEPERM (p, options, er, cb) {
187
182
} )
188
183
}
189
184
190
- function fixWinEPERMSync ( p , options , er ) {
185
+ const fixWinEPERMSync = ( p , options , er ) => {
191
186
assert ( p )
192
187
assert ( options )
193
188
if ( er )
@@ -218,7 +213,7 @@ function fixWinEPERMSync (p, options, er) {
218
213
options . unlinkSync ( p )
219
214
}
220
215
221
- function rmdir ( p , options , originalEr , cb ) {
216
+ const rmdir = ( p , options , originalEr , cb ) => {
222
217
assert ( p )
223
218
assert ( options )
224
219
if ( originalEr )
@@ -228,7 +223,7 @@ function rmdir (p, options, originalEr, cb) {
228
223
// try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
229
224
// if we guessed wrong, and it's not a directory, then
230
225
// raise the original error.
231
- options . rmdir ( p , function ( er ) {
226
+ options . rmdir ( p , er => {
232
227
if ( er && ( er . code === "ENOTEMPTY" || er . code === "EEXIST" || er . code === "EPERM" ) )
233
228
rmkids ( p , options , cb )
234
229
else if ( er && er . code === "ENOTDIR" )
@@ -238,20 +233,20 @@ function rmdir (p, options, originalEr, cb) {
238
233
} )
239
234
}
240
235
241
- function rmkids ( p , options , cb ) {
236
+ const rmkids = ( p , options , cb ) => {
242
237
assert ( p )
243
238
assert ( options )
244
239
assert ( typeof cb === 'function' )
245
240
246
- options . readdir ( p , function ( er , files ) {
241
+ options . readdir ( p , ( er , files ) => {
247
242
if ( er )
248
243
return cb ( er )
249
244
let n = files . length
250
245
if ( n === 0 )
251
246
return options . rmdir ( p , cb )
252
247
let errState
253
- files . forEach ( function ( f ) {
254
- rimraf ( path . join ( p , f ) , options , function ( er ) {
248
+ files . forEach ( f => {
249
+ rimraf ( path . join ( p , f ) , options , er => {
255
250
if ( errState )
256
251
return
257
252
if ( er )
@@ -266,7 +261,7 @@ function rmkids(p, options, cb) {
266
261
// this looks simpler, and is strictly *faster*, but will
267
262
// tie up the JavaScript thread and fail on excessively
268
263
// deep directory trees.
269
- function rimrafSync ( p , options ) {
264
+ const rimrafSync = ( p , options ) => {
270
265
options = options || { }
271
266
defaults ( options )
272
267
@@ -325,7 +320,7 @@ function rimrafSync (p, options) {
325
320
}
326
321
}
327
322
328
- function rmdirSync ( p , options , originalEr ) {
323
+ const rmdirSync = ( p , options , originalEr ) => {
329
324
assert ( p )
330
325
assert ( options )
331
326
if ( originalEr )
@@ -343,12 +338,10 @@ function rmdirSync (p, options, originalEr) {
343
338
}
344
339
}
345
340
346
- function rmkidsSync ( p , options ) {
341
+ const rmkidsSync = ( p , options ) => {
347
342
assert ( p )
348
343
assert ( options )
349
- options . readdirSync ( p ) . forEach ( function ( f ) {
350
- rimrafSync ( path . join ( p , f ) , options )
351
- } )
344
+ options . readdirSync ( p ) . forEach ( f => rimrafSync ( path . join ( p , f ) , options ) )
352
345
353
346
// We only end up here once we got ENOTEMPTY at least once, and
354
347
// at this point, we are guaranteed to have removed all the kids.
@@ -370,3 +363,6 @@ function rmkidsSync (p, options) {
370
363
}
371
364
} while ( true )
372
365
}
366
+
367
+ module . exports = rimraf
368
+ rimraf . sync = rimrafSync
0 commit comments