|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const { promises: fsp } = require('fs') |
| 3 | +const fs = require('fs') |
4 | 4 | const { Transform } = require('stream') |
5 | | -const ospath = require('path') |
6 | 5 | const map = (transform) => new Transform({ objectMode: true, transform }) |
7 | 6 | const vfs = require('vinyl-fs') |
8 | 7 |
|
9 | 8 | 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