Skip to content

Commit c064732

Browse files
committed
use fs.rm from stdlib to simplify remove task
1 parent a986bd7 commit c064732

File tree

1 file changed

+2
-37
lines changed

1 file changed

+2
-37
lines changed

gulp.d/tasks/remove.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,9 @@
11
'use strict'
22

3-
const { promises: fsp } = require('fs')
3+
const fs = require('fs')
44
const { Transform } = require('stream')
5-
const ospath = require('path')
65
const map = (transform) => new Transform({ objectMode: true, transform })
76
const vfs = require('vinyl-fs')
87

98
module.exports = (files) => () =>
10-
vfs.src(files, { allowEmpty: true }).pipe(map((file, enc, next) => remove(file.path, next)))
11-
12-
function remove (dir, cb) {
13-
return rmdir(dir).then(cb).catch(cb)
14-
}
15-
16-
/**
17-
* Removes the specified directory (including all of its contents) or file.
18-
* Equivalent to fs.promises.rmdir(dir, { recursive: true }) in Node 12.
19-
*/
20-
function rmdir (dir) {
21-
return fsp
22-
.readdir(dir, { withFileTypes: true })
23-
.then((lst) =>
24-
Promise.all(
25-
lst.map((it) =>
26-
it.isDirectory()
27-
? rmdir(ospath.join(dir, it.name))
28-
: fsp.unlink(ospath.join(dir, it.name)).catch((unlinkErr) => {
29-
if (unlinkErr.code !== 'ENOENT') throw unlinkErr
30-
})
31-
)
32-
)
33-
)
34-
.then(() => fsp.rmdir(dir))
35-
.catch((err) => {
36-
if (err.code === 'ENOENT') return
37-
if (err.code === 'ENOTDIR') {
38-
return fsp.unlink(dir).catch((unlinkErr) => {
39-
if (unlinkErr.code !== 'ENOENT') throw unlinkErr
40-
})
41-
}
42-
throw err
43-
})
44-
}
9+
vfs.src(files, { allowEmpty: true }).pipe(map((file, enc, next) => fs.rm(file.path, { recursive: true }, next)))

0 commit comments

Comments
 (0)