diff --git a/lib/wrench.js b/lib/wrench.js index 9c9b400..bcd7a39 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -14,7 +14,6 @@ var fs = require("fs"), _path = require("path"); - /* wrench.readdirSyncRecursive("directory_path"); * * Recursively dives through directories and read the contents of all the @@ -202,6 +201,8 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) { var contents = fs.readFileSync(srcFile); fs.writeFileSync(destFile, contents); + var stat = fs.lstatSync(srcFile); + fs.chmodSync(destFile, stat.mode); }; if(currFile.isDirectory()) { @@ -289,29 +290,32 @@ exports.chownSyncRecursive = function(sourceDir, uid, gid) { * Recursively dives through directories and obliterates everything about it. */ exports.rmdirRecursive = function rmdirRecursive(dir, failSilent, clbk){ - fs.readdir(dir, function(err, files){ + if(clbk === null || typeof clbk == 'undefined') + clbk = function(err) {}; + + fs.readdir(dir, function(err, files) { if(err && typeof failSilent === 'boolean' && !failSilent) return clbk(err); - if(typeof failSilent === 'function') - clbk = failSilent; + if(typeof failSilent === 'function') + clbk = failSilent; - (function rmFile(err){ - if (err) return clbk(err); - - var filename = files.shift(); - if (filename === null || typeof filename == 'undefined') - return fs.rmdir(dir, clbk); - - var file = dir+'/'+filename; - fs.lstat(file, function(err, stat){ + (function rmFile(err){ if (err) return clbk(err); - if (stat.isDirectory()) - rmdirRecursive(file, rmFile); - else - fs.unlink(file, rmFile); - }); - })(); + + var filename = files.shift(); + if (filename === null || typeof filename == 'undefined') + return fs.rmdir(dir, clbk); + + var file = dir+'/'+filename; + fs.lstat(file, function(err, stat){ + if (err) return clbk(err); + if (stat.isDirectory()) + rmdirRecursive(file, rmFile); + else + fs.unlink(file, rmFile); + }); + })(); }); }; @@ -324,12 +328,15 @@ exports.rmdirRecursive = function rmdirRecursive(dir, failSilent, clbk){ */ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, opts, clbk) { var originalArguments = Array.prototype.slice.apply(arguments); + srcDir = _path.normalize(srcDir); + newDir = _path.normalize(newDir); + fs.stat(newDir, function(err, newDirStat){ if(!err) { if(typeof opts !== 'undefined' && typeof opts !== 'function' && opts.forceDelete) - return exports.rmdirRecursive(newDir, function(err){ + return exports.rmdirRecursive(newDir, function(err) { copyDirRecursive.apply(this, originalArguments); - }); + }); else return clbk(new Error('You are trying to delete a directory that already exists. Specify forceDelete in an options object to override this.')); } diff --git a/package.json b/package.json index 065aeab..260358f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "wrench", "description": "Recursive filesystem (and other) operations that Node *should* have.", - "version": "1.5.1", + "version": "1.5.2", "author": "Ryan McGrath ", "repository": {