Permalink
Browse files

added remove function to utils for removing directories recursively a…

…sync
  • Loading branch information...
1 parent 97c0837 commit 0c07c20fee8ef852c01ed07e87a8b2447ab52f06 @Shogun147 committed Oct 3, 2012
Showing with 38 additions and 0 deletions.
  1. +38 −0 lib/utils.js
View
@@ -315,6 +315,44 @@ module.exports = {
});
},
+ remove: function remove(path, callback) {
+ Fs.stat(path, function(error, stats) {
+ if (error) { return callback(error); }
+
+ if (stats.isFile()) {
+ Fs.unlink(path, function(error) {
+ return callback(error);
+ });
+ } else if (stats.isDirectory()) {
+ Fs.readdir(path, function(error, files) {
+ if (error) { return callback(error); }
+
+ var pending = files.length;
+
+ if (!pending) {
+ Fs.rmdir(path, function(error) {
+ callback(error);
+ });
+ }
+
+ files.forEach(function(file) {
+ remove(path +'/'+ file, function(error) {
+ if (error) {
+ return callback(error);
+ }
+
+ if (!--pending) {
+ Fs.rmdir(path, function(error) {
+ callback(error);
+ });
+ }
+ });
+ });
+ });
+ }
+ });
+ },
+
parse_cookies: function parse_cookies(cookie) {
var cookies = {};

0 comments on commit 0c07c20

Please sign in to comment.