added failSilent option to rmdirSyncRecursive #10
1 changed files with 12 additions and 8 deletions
|
|
@ -13,28 +13,32 @@
|
||||||
|
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
|
||||||
/* wrench.rmdirSyncRecursive("directory_path");
|
/* wrench.rmdirSyncRecursive("directory_path", failSilent);
|
||||||
*
|
*
|
||||||
* Recursively dives through directories and obliterates everything about it. This is a
|
* Recursively dives through directories and obliterates everything about it. This is a
|
||||||
* Sync-function, which blocks things until it's done. No idea why anybody would want an
|
* Sync-function, which blocks things until it's done. No idea why anybody would want an
|
||||||
* Asynchronous version. :\
|
* Asynchronous version. :\
|
||||||
*/
|
*/
|
||||||
exports.rmdirSyncRecursive = function(path) {
|
exports.rmdirSyncRecursive = function(path, failSilent) {
|
||||||
var files = fs.readdirSync(path),
|
var files;
|
||||||
currDir = path;
|
try { files = fs.readdirSync(path); }
|
||||||
|
catch (err) {
|
||||||
|
if (failSilent) return;
|
||||||
|
throw new Error(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
/* Loop through and delete everything in the sub-tree after checking it */
|
/* Loop through and delete everything in the sub-tree after checking it */
|
||||||
for(var i = 0; i < files.length; i++) {
|
for(var i = 0; i < files.length; i++) {
|
||||||
var currFile = fs.statSync(currDir + "/" + files[i]);
|
var currFile = fs.statSync(path + "/" + files[i]);
|
||||||
|
|
||||||
if(currFile.isDirectory()) // Recursive function back to the beginning
|
if(currFile.isDirectory()) // Recursive function back to the beginning
|
||||||
exports.rmdirSyncRecursive(currDir + "/" + files[i]);
|
exports.rmdirSyncRecursive(path + "/" + files[i]);
|
||||||
|
|
||||||
else if(currFile.isSymbolicLink()) // Unlink symlinks
|
else if(currFile.isSymbolicLink()) // Unlink symlinks
|
||||||
fs.unlinkSync(currDir + "/" + files[i]);
|
fs.unlinkSync(path + "/" + files[i]);
|
||||||
|
|
||||||
else // Assume it's a file - perhaps a try/catch belongs here?
|
else // Assume it's a file - perhaps a try/catch belongs here?
|
||||||
fs.unlinkSync(currDir + "/" + files[i]);
|
fs.unlinkSync(path + "/" + files[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Now that we know everything in the sub-tree has been deleted, we can delete the main
|
/* Now that we know everything in the sub-tree has been deleted, we can delete the main
|
||||||
|
|
|
||||||
Reference in a new issue