Skip to content
This repository has been archived by the owner on Jun 19, 2019. It is now read-only.

Updated recursive copy logic to honor the preserveFiles option #100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/wrench.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down Expand Up @@ -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')
Expand Down
27 changes: 19 additions & 8 deletions tests/copydirsync_unix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');

wrench.copyDirSyncRecursive(testdir1, testdir2, { excludeHiddenUnix: false, preserveFiles: true });


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

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);
Expand Down