Make copy friendlier with option not to blow away existing dirs

Let's play nice.  Now when using copyDirSyncRecursive, you can pass an optional opts object.  If this object contains a preserve member, then the function will preserve exisiting directories and only overwrite existing files within the directories.  Without passing the option it behaves the same as always
This commit is contained in:
Jannon 2012-02-10 16:16:08 -08:00
parent a31e8029cd
commit 83b721a579

View file

@ -99,17 +99,22 @@ exports.rmdirSyncRecursive = function(path, failSilent) {
* *
* Note: Directories should be passed to this function without a trailing slash. * Note: Directories should be passed to this function without a trailing slash.
*/ */
exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) { exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
/* Copying over something is... tricky. The user should know what they're doing at this point, so...
* blow any existing directory away! if (!opts || !opts.preserve) {
*/
try { try {
if(fs.statSync(newDirLocation).isDirectory()) exports.rmdirSyncRecursive(newDirLocation); if(fs.statSync(newDirLocation).isDirectory()) exports.rmdirSyncRecursive(newDirLocation);
} catch(e) { } } catch(e) { }
}
/* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */ /* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */
var checkDir = fs.statSync(sourceDir); var checkDir = fs.statSync(sourceDir);
try {
fs.mkdirSync(newDirLocation, checkDir.mode); fs.mkdirSync(newDirLocation, checkDir.mode);
} catch (e) {
//if the directory already exists, that's okay
if (e.code !== 'EEXIST') throw e;
}
var files = fs.readdirSync(sourceDir); var files = fs.readdirSync(sourceDir);
@ -118,7 +123,7 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) {
if(currFile.isDirectory()) { if(currFile.isDirectory()) {
/* recursion this thing right on back. */ /* recursion this thing right on back. */
exports.copyDirSyncRecursive(sourceDir + "/" + files[i], newDirLocation + "/" + files[i]); exports.copyDirSyncRecursive(sourceDir + "/" + files[i], newDirLocation + "/" + files[i], opts);
} else if(currFile.isSymbolicLink()) { } else if(currFile.isSymbolicLink()) {
var symlinkFull = fs.readlinkSync(sourceDir + "/" + files[i]); var symlinkFull = fs.readlinkSync(sourceDir + "/" + files[i]);
fs.symlinkSync(symlinkFull, newDirLocation + "/" + files[i]); fs.symlinkSync(symlinkFull, newDirLocation + "/" + files[i]);