rm-rf
Version:
Rimraf and glob all in one.
48 lines (44 loc) • 882 B
JavaScript
;
var glob = require('glob');
var rimraf = require('rimraf');
function loop(n, each, done) {
if (n === 0) return done();
const finalDone = done || (() => {
});
let counter = 0;
let i = -1;
while (++i < n) {
call(i);
}
function call(i2) {
let unlock = true;
each(() => {
if (!unlock) return;
unlock = false;
if (++counter === n) finalDone?.();
}, i2);
}
}
function rmRf(pattern, options, callback) {
let cb = callback;
let opts = options;
if (typeof callback === "undefined") {
cb = options;
} else {
opts = {};
}
const filenames = glob.globSync(pattern, opts);
if (filenames.length === 0) {
return cb?.();
}
loop(
filenames.length,
(done, i) => {
const filename = filenames[i];
rimraf.rimrafSync(filename);
done();
},
cb
);
}
module.exports = rmRf;