Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rimraf with futures
/*** Generated by streamline 0.1.35 - DO NOT EDIT ***/
var __global = typeof global !== 'undefined' ? global : window;
function __cb(_, fn){ var ctx = __global.__context; return function(err, result){ __global.__context = ctx; if (err) return _(err); try { return fn(null, result); } catch (ex) { return __propagate(_, ex); } } }
function __future(fn, args, i){ var done, err, result; var cb = function(e, r){ done = true; err = e, result = r; }; args = Array.prototype.slice.call(args); args[i] = function(e, r){ cb(e, r); }; fn.apply(this, args); return function(_){ if (done) _.call(this, err, result); else cb = _.bind(this); } .bind(this); }
function __nt(_, fn){ var i = 0; var cb = __cb(_, fn); var safeCb = function(){ try { cb(); } catch (ex) { __propagate(cb, ex); } }; if (typeof process != "undefined" && typeof process.nextTick == "function") return function(){ if (++i % 20 == 0) process.nextTick(safeCb); else cb(); }; else return function(){ if (++i % 20 == 0) setTimeout(safeCb); else cb(); }; }
function __propagate(_, err){ try { _(err); } catch (ex) { __trap(ex); } }
function __trap(err){ if (err) { if (__global.__context && __global.__context.errorHandler) __global.__context.errorHandler(err); else console.error("UNCAUGHT EXCEPTION: " + err.message + "\n" + err.stack); } }
function __tryCatch(_, fn){ try { fn(); } catch (e) { try { _(e); } catch (ex) { __trap(ex); } } }
/* 1 */ var path = require("path"), fs = require("fs");
/* 5 */ var timeout = 0;
/* 6 */ var rimraf = module.exports = function __1(p, opts, wait) {
if (!wait) {
return __future(__1, arguments, 2);
}
;
var busyTries, stat, rimrafs, i;
/* 7 */ opts = (opts || {
});
/* 8 */ opts.maxBusyTries = (opts.maxBusyTries || 3);
/* 9 */ busyTries = 0;
return (function(__break) {
var __loop = __nt(wait, function() {
/* 11 */ var __4 = true;
if (__4) {
return (function(__then) {
(function(wait) {
__tryCatch(wait, function() {
/* 13 */ return fs.lstat(p, __cb(wait, function(__0, __1) {
stat = __1;
return (function(__then) {
/* 14 */ if (!stat.isDirectory()) {
/* 14 */ return fs.unlink(p, __cb(wait, function(__0, __2) {
return wait(null, __2);
}));
}
else {
__then();
}
;
})(function() {
/* 15 */ return fs.readdir(p, __cb(wait, function(__0, __3) {
/* 15 */ rimrafs = __3.map(function(file) {
/* 16 */ return rimraf(path.join(p, file), opts);
});
/* 18 */ i = 0;
var __9 = false;
return (function(__break) {
var __loop = __nt(wait, function() {
if (__9) {
/* 18 */ i++;
}
else {
__9 = true;
}
;
/* 18 */ var __8 = (i < rimrafs.length);
if (__8) {
/* 19 */ return rimrafs[i](__cb(wait, __loop));
}
else {
__break();
}
;
});
__loop();
})(function() {
/* 20 */ return fs.rmdir(p, __cb(wait, function() {
/* 21 */ timeout = 0;
return wait(null);
}));
});
}));
});
}));
});
})(function(ex, __result) {
__tryCatch(wait, function() {
if (ex) {
return (function(__then) {
/* 24 */ if (ex.message.match(/^EMFILE/)) {
/* 25 */ return setTimeout(__cb(wait, __then), timeout += 10);
}
else {
return (function(__then) {
/* 26 */ if ((ex.message.match(/^EBUSY/) && (busyTries < opt.maxBusyTries))) {
/* 27 */ return setTimeout(__cb(wait, __then), (++busyTries * 100));
}
else {
/* 28 */ if (ex.message.match(/^ENOENT/)) {
return wait(null);
}
/* 30 */ else {
/* 31 */ return wait(ex);
}
;
__then();
}
;
})(__then);
}
;
})(__then);
}
else {
wait(null, __result);
}
;
});
});
})(function() {
__tryCatch(wait, __loop);
});
}
else {
__break();
}
;
});
__loop();
})(function() {
wait();
});
};
//streamline.options = { "callback" : "wait" }
var path = require('path'),
fs = require('fs');
var timeout = 0;
var rimraf = module.exports = function(p, opts, wait) {
opts = opts || {};
opts.maxBusyTries = opts.maxBusyTries || 3;
var busyTries = 0;
while (true) {
try {
var stat = fs.lstat(p, wait);
if (!stat.isDirectory()) return fs.unlink(p, wait);
var rimrafs = fs.readdir(p, wait).map(function(file) {
return rimraf(path.join(p, file), opts);
});
for (var i = 0; i < rimrafs.length; i++)
rimrafs[i](wait);
fs.rmdir(p, wait);
timeout = 0;
return;
} catch (ex) {
if (ex.message.match(/^EMFILE/)) {
setTimeout(wait, timeout += 10);
} else if (ex.message.match(/^EBUSY/) && busyTries < opt.maxBusyTries) {
setTimeout(wait, ++busyTries * 100);
} else if (ex.message.match(/^ENOENT/)) {
return;
} else {
throw ex;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.