Skip to content

Commit

Permalink
use fs.rm from stdlib to simplify remove task
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Nov 16, 2023
1 parent a986bd7 commit c064732
Showing 1 changed file with 2 additions and 37 deletions.
39 changes: 2 additions & 37 deletions gulp.d/tasks/remove.js
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)))

0 comments on commit c064732

Please sign in to comment.