Skip to content

Commit d7e41a2

Browse files
committed
Use arrow functions
BREAKING CHANGE: this drops support for node 0.x
1 parent 592a4b6 commit d7e41a2

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

rimraf.js

+42-46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
module.exports = rimraf
2-
rimraf.sync = rimrafSync
3-
41
const assert = require("assert")
52
const path = require("path")
63
const fs = require("fs")
@@ -21,7 +18,7 @@ let timeout = 0
2118

2219
const isWindows = (process.platform === "win32")
2320

24-
function defaults (options) {
21+
const defaults = options => {
2522
const methods = [
2623
'unlink',
2724
'chmod',
@@ -30,7 +27,7 @@ function defaults (options) {
3027
'rmdir',
3128
'readdir'
3229
]
33-
methods.forEach(function(m) {
30+
methods.forEach(m => {
3431
options[m] = options[m] || fs[m]
3532
m = m + 'Sync'
3633
options[m] = options[m] || fs[m]
@@ -48,7 +45,7 @@ function defaults (options) {
4845
options.glob = options.glob || defaultGlobOpts
4946
}
5047

51-
function rimraf (p, options, cb) {
48+
const rimraf = (p, options, cb) => {
5249
if (typeof options === 'function') {
5350
cb = options
5451
options = {}
@@ -66,47 +63,33 @@ function rimraf (p, options, cb) {
6663
let errState = null
6764
let n = 0
6865

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) => {
8067
errState = errState || er
8168
if (--n === 0)
8269
cb(errState)
8370
}
8471

85-
function afterGlob (er, results) {
72+
const afterGlob = (er, results) => {
8673
if (er)
8774
return cb(er)
8875

8976
n = results.length
9077
if (n === 0)
9178
return cb()
9279

93-
results.forEach(function (p) {
94-
rimraf_(p, options, function CB (er) {
80+
results.forEach(p => {
81+
const CB = (er) => {
9582
if (er) {
9683
if ((er.code === "EBUSY" || er.code === "ENOTEMPTY" || er.code === "EPERM") &&
9784
busyTries < options.maxBusyTries) {
9885
busyTries ++
9986
// 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)
10388
}
10489

10590
// this one won't happen if graceful-fs is used.
10691
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 ++)
11093
}
11194

11295
// already gone
@@ -115,9 +98,21 @@ function rimraf (p, options, cb) {
11598

11699
timeout = 0
117100
next(er)
118-
})
101+
}
102+
rimraf_(p, options, CB)
119103
})
120104
}
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+
121116
}
122117

123118
// Two possible strategies.
@@ -131,14 +126,14 @@ function rimraf (p, options, cb) {
131126
//
132127
// If anyone ever complains about this, then I guess the strategy could
133128
// be made configurable somehow. But until then, YAGNI.
134-
function rimraf_ (p, options, cb) {
129+
const rimraf_ = (p, options, cb) => {
135130
assert(p)
136131
assert(options)
137132
assert(typeof cb === 'function')
138133

139134
// sunos lets the root user unlink directories, which is... weird.
140135
// 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) => {
142137
if (er && er.code === "ENOENT")
143138
return cb(null)
144139

@@ -149,7 +144,7 @@ function rimraf_ (p, options, cb) {
149144
if (st && st.isDirectory())
150145
return rmdir(p, options, er, cb)
151146

152-
options.unlink(p, function (er) {
147+
options.unlink(p, er => {
153148
if (er) {
154149
if (er.code === "ENOENT")
155150
return cb(null)
@@ -165,18 +160,18 @@ function rimraf_ (p, options, cb) {
165160
})
166161
}
167162

168-
function fixWinEPERM (p, options, er, cb) {
163+
const fixWinEPERM = (p, options, er, cb) => {
169164
assert(p)
170165
assert(options)
171166
assert(typeof cb === 'function')
172167
if (er)
173168
assert(er instanceof Error)
174169

175-
options.chmod(p, 0o666, function (er2) {
170+
options.chmod(p, 0o666, er2 => {
176171
if (er2)
177172
cb(er2.code === "ENOENT" ? null : er)
178173
else
179-
options.stat(p, function(er3, stats) {
174+
options.stat(p, (er3, stats) => {
180175
if (er3)
181176
cb(er3.code === "ENOENT" ? null : er)
182177
else if (stats.isDirectory())
@@ -187,7 +182,7 @@ function fixWinEPERM (p, options, er, cb) {
187182
})
188183
}
189184

190-
function fixWinEPERMSync (p, options, er) {
185+
const fixWinEPERMSync = (p, options, er) => {
191186
assert(p)
192187
assert(options)
193188
if (er)
@@ -218,7 +213,7 @@ function fixWinEPERMSync (p, options, er) {
218213
options.unlinkSync(p)
219214
}
220215

221-
function rmdir (p, options, originalEr, cb) {
216+
const rmdir = (p, options, originalEr, cb) => {
222217
assert(p)
223218
assert(options)
224219
if (originalEr)
@@ -228,7 +223,7 @@ function rmdir (p, options, originalEr, cb) {
228223
// try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
229224
// if we guessed wrong, and it's not a directory, then
230225
// raise the original error.
231-
options.rmdir(p, function (er) {
226+
options.rmdir(p, er => {
232227
if (er && (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM"))
233228
rmkids(p, options, cb)
234229
else if (er && er.code === "ENOTDIR")
@@ -238,20 +233,20 @@ function rmdir (p, options, originalEr, cb) {
238233
})
239234
}
240235

241-
function rmkids(p, options, cb) {
236+
const rmkids = (p, options, cb) => {
242237
assert(p)
243238
assert(options)
244239
assert(typeof cb === 'function')
245240

246-
options.readdir(p, function (er, files) {
241+
options.readdir(p, (er, files) => {
247242
if (er)
248243
return cb(er)
249244
let n = files.length
250245
if (n === 0)
251246
return options.rmdir(p, cb)
252247
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 => {
255250
if (errState)
256251
return
257252
if (er)
@@ -266,7 +261,7 @@ function rmkids(p, options, cb) {
266261
// this looks simpler, and is strictly *faster*, but will
267262
// tie up the JavaScript thread and fail on excessively
268263
// deep directory trees.
269-
function rimrafSync (p, options) {
264+
const rimrafSync = (p, options) => {
270265
options = options || {}
271266
defaults(options)
272267

@@ -325,7 +320,7 @@ function rimrafSync (p, options) {
325320
}
326321
}
327322

328-
function rmdirSync (p, options, originalEr) {
323+
const rmdirSync = (p, options, originalEr) => {
329324
assert(p)
330325
assert(options)
331326
if (originalEr)
@@ -343,12 +338,10 @@ function rmdirSync (p, options, originalEr) {
343338
}
344339
}
345340

346-
function rmkidsSync (p, options) {
341+
const rmkidsSync = (p, options) => {
347342
assert(p)
348343
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))
352345

353346
// We only end up here once we got ENOTEMPTY at least once, and
354347
// at this point, we are guaranteed to have removed all the kids.
@@ -370,3 +363,6 @@ function rmkidsSync (p, options) {
370363
}
371364
} while (true)
372365
}
366+
367+
module.exports = rimraf
368+
rimraf.sync = rimrafSync

0 commit comments

Comments
 (0)