Merge pull request #17 from jannon/master

Making copy a little friendlier (allowing existing directories)
This commit is contained in:
Ryan McGrath 2012-02-18 01:51:43 -08:00
commit ee67ebfc77

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]);