-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use fs.rm from stdlib to simplify remove task
- Loading branch information
1 parent
a986bd7
commit c064732
Showing
1 changed file
with
2 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,9 @@ | ||
'use strict' | ||
|
||
const { promises: fsp } = require('fs') | ||
const fs = require('fs') | ||
const { Transform } = require('stream') | ||
const ospath = require('path') | ||
const map = (transform) => new Transform({ objectMode: true, transform }) | ||
const vfs = require('vinyl-fs') | ||
|
||
module.exports = (files) => () => | ||
vfs.src(files, { allowEmpty: true }).pipe(map((file, enc, next) => remove(file.path, next))) | ||
|
||
function remove (dir, cb) { | ||
return rmdir(dir).then(cb).catch(cb) | ||
} | ||
|
||
/** | ||
* Removes the specified directory (including all of its contents) or file. | ||
* Equivalent to fs.promises.rmdir(dir, { recursive: true }) in Node 12. | ||
*/ | ||
function rmdir (dir) { | ||
return fsp | ||
.readdir(dir, { withFileTypes: true }) | ||
.then((lst) => | ||
Promise.all( | ||
lst.map((it) => | ||
it.isDirectory() | ||
? rmdir(ospath.join(dir, it.name)) | ||
: fsp.unlink(ospath.join(dir, it.name)).catch((unlinkErr) => { | ||
if (unlinkErr.code !== 'ENOENT') throw unlinkErr | ||
}) | ||
) | ||
) | ||
) | ||
.then(() => fsp.rmdir(dir)) | ||
.catch((err) => { | ||
if (err.code === 'ENOENT') return | ||
if (err.code === 'ENOTDIR') { | ||
return fsp.unlink(dir).catch((unlinkErr) => { | ||
if (unlinkErr.code !== 'ENOENT') throw unlinkErr | ||
}) | ||
} | ||
throw err | ||
}) | ||
} | ||
vfs.src(files, { allowEmpty: true }).pipe(map((file, enc, next) => fs.rm(file.path, { recursive: true }, next))) |