|
@@ -0,0 +1,63 @@
|
|
1
|
+const rimraf = require('rimraf')
|
|
2
|
+const debug = require('debug')('cleanup')
|
|
3
|
+const tmpdir = require('tmp').dirSync
|
|
4
|
+const mkdirp = require('mkdirp')
|
|
5
|
+
|
|
6
|
+function cleanup (dirs, cb) {
|
|
7
|
+ if (!cb) cb = noop
|
|
8
|
+ if (!Array.isArray(dirs)) dirs = [dirs]
|
|
9
|
+ var pending = 1
|
|
10
|
+
|
|
11
|
+ function next (n) {
|
|
12
|
+ var dir = dirs[n]
|
|
13
|
+ if (!dir) return done()
|
|
14
|
+ ++pending
|
|
15
|
+ process.nextTick(next, n + 1)
|
|
16
|
+
|
|
17
|
+ rimraf(dir, (err) => {
|
|
18
|
+ if (err) return done(err)
|
|
19
|
+ done()
|
|
20
|
+ })
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ function done (err) {
|
|
24
|
+ if (err) {
|
|
25
|
+ pending = Infinity
|
|
26
|
+ return cb(err)
|
|
27
|
+ }
|
|
28
|
+ if (!--pending) return cb()
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ next(0)
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+function tmp () {
|
|
35
|
+ var path = '.'+tmpdir().name
|
|
36
|
+ mkdirp.sync(path)
|
|
37
|
+ return path
|
|
38
|
+}
|
|
39
|
+
|
|
40
|
+function replicate (a, b, opts, cb) {
|
|
41
|
+ if (typeof opts === 'function') return replicate(a, b, {}, opts)
|
|
42
|
+ if (!cb) cb = noop
|
|
43
|
+
|
|
44
|
+ var s = a.replicate(true, Object.assign({ live: false }, opts))
|
|
45
|
+ var d = b.replicate(false, Object.assign({ live: false }, opts))
|
|
46
|
+
|
|
47
|
+ s.pipe(d).pipe(s)
|
|
48
|
+
|
|
49
|
+ s.on('error', (err) => {
|
|
50
|
+ if (err) return cb(err)
|
|
51
|
+ })
|
|
52
|
+
|
|
53
|
+ s.on('end', cb)
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+function uniq (array) {
|
|
57
|
+ if (!Array.isArray(array)) array = [array]
|
|
58
|
+ return Array.from(new Set(array))
|
|
59
|
+}
|
|
60
|
+
|
|
61
|
+function noop () {}
|
|
62
|
+
|
|
63
|
+module.exports = { cleanup, tmp, replicate, uniq }
|