From 7157760c127c46dca777915faf9e6a01d74d264e Mon Sep 17 00:00:00 2001 From: Dave Geddes Date: Tue, 10 Jan 2012 12:38:39 -0700 Subject: [PATCH 1/2] added failSilent option to wrench.rmdirSyncRecursive for cases where you want to delete a directory that may or may not exist: wrench.rmdirSyncRecursive('dir', true); --- lib/wrench.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/wrench.js b/lib/wrench.js index 911c162..6e47f6a 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -13,15 +13,19 @@ 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 * Sync-function, which blocks things until it's done. No idea why anybody would want an * Asynchronous version. :\ */ -exports.rmdirSyncRecursive = function(path) { - var files = fs.readdirSync(path), - currDir = path; +exports.rmdirSyncRecursive = function(path, failSilent) { + 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 */ for(var i = 0; i < files.length; i++) { From 9bda890fe09ec849dc2f88750b0070af211cba79 Mon Sep 17 00:00:00 2001 From: Dave Geddes Date: Tue, 10 Jan 2012 12:41:04 -0700 Subject: [PATCH 2/2] removed redundant variable --- lib/wrench.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/wrench.js b/lib/wrench.js index 6e47f6a..a9227b1 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -20,7 +20,7 @@ var fs = require("fs"); * Asynchronous version. :\ */ exports.rmdirSyncRecursive = function(path, failSilent) { - var files, currDir = path; + var files; try { files = fs.readdirSync(path); } catch (err) { if (failSilent) return; @@ -29,16 +29,16 @@ exports.rmdirSyncRecursive = function(path, failSilent) { /* Loop through and delete everything in the sub-tree after checking it */ 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 - exports.rmdirSyncRecursive(currDir + "/" + files[i]); + exports.rmdirSyncRecursive(path + "/" + files[i]); 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? - 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