diff --git a/lib/wrench.js b/lib/wrench.js index 01f5e95..e2ca388 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -233,9 +233,9 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) { try { if(fs.statSync(newDirLocation).isDirectory()) { if(opts.forceDelete) { - exports.rmdirSyncRecursive(newDirLocation); - } else { - return new Error('You are trying to delete a directory that already exists. Specify forceDelete in the opts argument to override this. Bailing~'); + exports.rmdirSyncRecursive(newDirLocation); + } else if(!opts.preserveFiles) { + return new Error('You are trying to copy a directory onto a directory that already exists. Specify forceDelete or preserveFiles in the opts argument to specify desired behavior'); } } } catch(e) { } @@ -413,8 +413,9 @@ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, opts, clbk) 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.')); + else if(typeof opts !== 'undefined' && typeof opts !== 'function' && !opts.preserveFiles) { + return clbk(new Error('You are trying to copy a directory onto a directory that already exists. Specify forceDelete or preserveFiles in the opts argument to specify desired behavior')); + } } if(typeof opts === 'function') diff --git a/tests/copydirsync_unix.js b/tests/copydirsync_unix.js index c48bb62..4f7679c 100644 --- a/tests/copydirsync_unix.js +++ b/tests/copydirsync_unix.js @@ -78,9 +78,13 @@ function checkResultDontInflate(test, files) { function checkResultPreserveFiles(test, files) { checkResultHidden(test, files); var contents = fs.readFileSync(path.join(__dirname, path.join('testdir2', '.hidden.txt')), "utf8"); - test.deepEqual(contents, 'hidden file'); + test.deepEqual(contents, 'hidden file'); // Preserved Content contents = fs.readFileSync(path.join(__dirname, path.join('testdir2', 'bar.txt')), "utf8"); - test.deepEqual(contents, 'shown file'); + test.deepEqual(contents, 'shown file'); // Preserved Content + contents = fs.readFileSync(path.join(__dirname, path.join('testdir2', 'foo/lorem.txt')), "utf8"); + test.deepEqual(contents, 'Lorem ipsum, please preserve my content.'); // Preserved Content + contents = fs.readFileSync(path.join(__dirname, path.join('testdir2', 'foo/dolor.md')), "utf8"); + test.deepEqual(contents, '#dolor sit amet'); // Copied Content } function checkResultOverwriteFiles(test, files) { @@ -196,12 +200,19 @@ module.exports = testCase({ // wrench.mkdirSyncRecursive(testdir1, 0777); wrench.copyDirSyncRecursive(dir, testdir1, { excludeHiddenUnix: false }); wrench.copyDirSyncRecursive(dir, testdir2, { excludeHiddenUnix: false }); + + fs.writeFileSync(path.join(testdir1, '.hidden.txt'), 'just some text for .hidden.txt'); + fs.writeFileSync(path.join(testdir1, 'bar.txt'), 'just some text for bar.txt'); + fs.writeFileSync(path.join(testdir1, 'foo/lorem.txt'), 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. '); + fs.writeFileSync(path.join(testdir1, 'foo/dolor.md'), '#dolor sit amet'); - fs.writeFileSync(path.join(testdir1, ".hidden.txt"), 'just some text for .hidden.txt'); - fs.writeFileSync(path.join(testdir1, "bar.txt"), 'just some text for bar.txt'); - - wrench.copyDirSyncRecursive(testdir1, testdir2, { excludeHiddenUnix: false, preserveFiles: true }); + wrench.rmdirSyncRecursive(path.join(testdir2, 'foo')); + fs.mkdirSync(path.join(testdir2, 'foo')); + fs.writeFileSync(path.join(testdir2, 'foo/lorem.txt'), 'Lorem ipsum, please preserve my content.'); + var err = wrench.copyDirSyncRecursive(testdir1, testdir2, { excludeHiddenUnix: false, preserveFiles: true }); + test.equal(err, undefined, 'Error should not be returned'); + var files = wrench.readdirSyncRecursive(testdir2); checkResultPreserveFiles(test, files);