rimraf 2.2.6 → 2.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/rimraf.js +109 -39
  3. package/test/run.sh +8 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rimraf",
3
- "version": "2.2.6",
3
+ "version": "2.2.8",
4
4
  "main": "rimraf.js",
5
5
  "description": "A deep deletion module for node (like `rm -rf`)",
6
6
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
package/rimraf.js CHANGED
@@ -1,6 +1,7 @@
1
1
  module.exports = rimraf
2
2
  rimraf.sync = rimrafSync
3
3
 
4
+ var assert = require("assert")
4
5
  var path = require("path")
5
6
  var fs = require("fs")
6
7
 
@@ -11,11 +12,36 @@ exports.BUSYTRIES_MAX = 3
11
12
 
12
13
  var isWindows = (process.platform === "win32")
13
14
 
14
- function rimraf (p, cb) {
15
+ function defaults (options) {
16
+ var methods = [
17
+ 'unlink',
18
+ 'chmod',
19
+ 'stat',
20
+ 'rmdir',
21
+ 'readdir'
22
+ ]
23
+ methods.forEach(function(m) {
24
+ options[m] = options[m] || fs[m]
25
+ m = m + 'Sync'
26
+ options[m] = options[m] || fs[m]
27
+ })
28
+ }
29
+
30
+ function rimraf (p, options, cb) {
31
+ if (typeof options === 'function') {
32
+ cb = options
33
+ options = {}
34
+ }
35
+ assert(p)
36
+ assert(options)
37
+ assert(typeof cb === 'function')
38
+
39
+ defaults(options)
40
+
15
41
  if (!cb) throw new Error("No callback passed to rimraf()")
16
42
 
17
43
  var busyTries = 0
18
- rimraf_(p, function CB (er) {
44
+ rimraf_(p, options, function CB (er) {
19
45
  if (er) {
20
46
  if (isWindows && (er.code === "EBUSY" || er.code === "ENOTEMPTY") &&
21
47
  busyTries < exports.BUSYTRIES_MAX) {
@@ -23,14 +49,14 @@ function rimraf (p, cb) {
23
49
  var time = busyTries * 100
24
50
  // try again, with the same exact callback as this one.
25
51
  return setTimeout(function () {
26
- rimraf_(p, CB)
52
+ rimraf_(p, options, CB)
27
53
  }, time)
28
54
  }
29
55
 
30
56
  // this one won't happen if graceful-fs is used.
31
57
  if (er.code === "EMFILE" && timeout < exports.EMFILE_MAX) {
32
58
  return setTimeout(function () {
33
- rimraf_(p, CB)
59
+ rimraf_(p, options, CB)
34
60
  }, timeout ++)
35
61
  }
36
62
 
@@ -54,64 +80,91 @@ function rimraf (p, cb) {
54
80
  //
55
81
  // If anyone ever complains about this, then I guess the strategy could
56
82
  // be made configurable somehow. But until then, YAGNI.
57
- function rimraf_ (p, cb) {
58
- fs.unlink(p, function (er) {
83
+ function rimraf_ (p, options, cb) {
84
+ assert(p)
85
+ assert(options)
86
+ assert(typeof cb === 'function')
87
+
88
+ options.unlink(p, function (er) {
59
89
  if (er) {
60
90
  if (er.code === "ENOENT")
61
91
  return cb(null)
62
92
  if (er.code === "EPERM")
63
- return (isWindows) ? fixWinEPERM(p, er, cb) : rmdir(p, er, cb)
93
+ return (isWindows)
94
+ ? fixWinEPERM(p, options, er, cb)
95
+ : rmdir(p, options, er, cb)
64
96
  if (er.code === "EISDIR")
65
- return rmdir(p, er, cb)
97
+ return rmdir(p, options, er, cb)
66
98
  }
67
99
  return cb(er)
68
100
  })
69
101
  }
70
102
 
71
- function fixWinEPERM (p, er, cb) {
72
- fs.chmod(p, 666, function (er2) {
103
+ function fixWinEPERM (p, options, er, cb) {
104
+ assert(p)
105
+ assert(options)
106
+ assert(typeof cb === 'function')
107
+ if (er)
108
+ assert(er instanceof Error)
109
+
110
+ options.chmod(p, 666, function (er2) {
73
111
  if (er2)
74
112
  cb(er2.code === "ENOENT" ? null : er)
75
113
  else
76
- fs.stat(p, function(er3, stats) {
114
+ options.stat(p, function(er3, stats) {
77
115
  if (er3)
78
116
  cb(er3.code === "ENOENT" ? null : er)
79
117
  else if (stats.isDirectory())
80
- rmdir(p, er, cb)
118
+ rmdir(p, options, er, cb)
81
119
  else
82
- fs.unlink(p, cb)
120
+ options.unlink(p, cb)
83
121
  })
84
122
  })
85
123
  }
86
124
 
87
- function fixWinEPERMSync (p, er, cb) {
125
+ function fixWinEPERMSync (p, options, er) {
126
+ assert(p)
127
+ assert(options)
128
+ if (er)
129
+ assert(er instanceof Error)
130
+
88
131
  try {
89
- fs.chmodSync(p, 666)
132
+ options.chmodSync(p, 666)
90
133
  } catch (er2) {
91
- if (er2.code !== "ENOENT")
134
+ if (er2.code === "ENOENT")
135
+ return
136
+ else
92
137
  throw er
93
138
  }
94
139
 
95
140
  try {
96
- var stats = fs.statSync(p)
141
+ var stats = options.statSync(p)
97
142
  } catch (er3) {
98
- if (er3 !== "ENOENT")
143
+ if (er3.code === "ENOENT")
144
+ return
145
+ else
99
146
  throw er
100
147
  }
101
148
 
102
149
  if (stats.isDirectory())
103
- rmdirSync(p, er)
150
+ rmdirSync(p, options, er)
104
151
  else
105
- fs.unlinkSync(p)
152
+ options.unlinkSync(p)
106
153
  }
107
154
 
108
- function rmdir (p, originalEr, cb) {
155
+ function rmdir (p, options, originalEr, cb) {
156
+ assert(p)
157
+ assert(options)
158
+ if (originalEr)
159
+ assert(originalEr instanceof Error)
160
+ assert(typeof cb === 'function')
161
+
109
162
  // try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
110
163
  // if we guessed wrong, and it's not a directory, then
111
164
  // raise the original error.
112
- fs.rmdir(p, function (er) {
165
+ options.rmdir(p, function (er) {
113
166
  if (er && (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM"))
114
- rmkids(p, cb)
167
+ rmkids(p, options, cb)
115
168
  else if (er && er.code === "ENOTDIR")
116
169
  cb(originalEr)
117
170
  else
@@ -119,22 +172,26 @@ function rmdir (p, originalEr, cb) {
119
172
  })
120
173
  }
121
174
 
122
- function rmkids(p, cb) {
123
- fs.readdir(p, function (er, files) {
175
+ function rmkids(p, options, cb) {
176
+ assert(p)
177
+ assert(options)
178
+ assert(typeof cb === 'function')
179
+
180
+ options.readdir(p, function (er, files) {
124
181
  if (er)
125
182
  return cb(er)
126
183
  var n = files.length
127
184
  if (n === 0)
128
- return fs.rmdir(p, cb)
185
+ return options.rmdir(p, cb)
129
186
  var errState
130
187
  files.forEach(function (f) {
131
- rimraf(path.join(p, f), function (er) {
188
+ rimraf(path.join(p, f), options, function (er) {
132
189
  if (errState)
133
190
  return
134
191
  if (er)
135
192
  return cb(errState = er)
136
193
  if (--n === 0)
137
- fs.rmdir(p, cb)
194
+ options.rmdir(p, cb)
138
195
  })
139
196
  })
140
197
  })
@@ -143,36 +200,49 @@ function rmkids(p, cb) {
143
200
  // this looks simpler, and is strictly *faster*, but will
144
201
  // tie up the JavaScript thread and fail on excessively
145
202
  // deep directory trees.
146
- function rimrafSync (p) {
203
+ function rimrafSync (p, options) {
204
+ options = options || {}
205
+ defaults(options)
206
+
207
+ assert(p)
208
+ assert(options)
209
+
147
210
  try {
148
- fs.unlinkSync(p)
211
+ options.unlinkSync(p)
149
212
  } catch (er) {
150
213
  if (er.code === "ENOENT")
151
214
  return
152
215
  if (er.code === "EPERM")
153
- return isWindows ? fixWinEPERMSync(p, er) : rmdirSync(p, er)
216
+ return isWindows ? fixWinEPERMSync(p, options, er) : rmdirSync(p, options, er)
154
217
  if (er.code !== "EISDIR")
155
218
  throw er
156
- rmdirSync(p, er)
219
+ rmdirSync(p, options, er)
157
220
  }
158
221
  }
159
222
 
160
- function rmdirSync (p, originalEr) {
223
+ function rmdirSync (p, options, originalEr) {
224
+ assert(p)
225
+ assert(options)
226
+ if (originalEr)
227
+ assert(originalEr instanceof Error)
228
+
161
229
  try {
162
- fs.rmdirSync(p)
230
+ options.rmdirSync(p)
163
231
  } catch (er) {
164
232
  if (er.code === "ENOENT")
165
233
  return
166
234
  if (er.code === "ENOTDIR")
167
235
  throw originalEr
168
236
  if (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM")
169
- rmkidsSync(p)
237
+ rmkidsSync(p, options)
170
238
  }
171
239
  }
172
240
 
173
- function rmkidsSync (p) {
174
- fs.readdirSync(p).forEach(function (f) {
175
- rimrafSync(path.join(p, f))
241
+ function rmkidsSync (p, options) {
242
+ assert(p)
243
+ assert(options)
244
+ options.readdirSync(p).forEach(function (f) {
245
+ rimrafSync(path.join(p, f), options)
176
246
  })
177
- fs.rmdirSync(p)
247
+ options.rmdirSync(p, options)
178
248
  }
package/test/run.sh CHANGED
@@ -1,10 +1,16 @@
1
1
  #!/bin/bash
2
2
  set -e
3
+ code=0
3
4
  for i in test-*.js; do
4
5
  echo -n $i ...
5
6
  bash setup.sh
6
7
  node $i
7
- ! [ -d target ]
8
- echo "pass"
8
+ if [ -d target ]; then
9
+ echo "fail"
10
+ code=1
11
+ else
12
+ echo "pass"
13
+ fi
9
14
  done
10
15
  rm -rf target
16
+ exit $code