Updated recursive copy logic to honor the preserveFiles option
This commit is contained in:
parent
13f486d867
commit
156eaceed6
2 changed files with 23 additions and 11 deletions
|
|
@ -233,9 +233,9 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
|
||||||
try {
|
try {
|
||||||
if(fs.statSync(newDirLocation).isDirectory()) {
|
if(fs.statSync(newDirLocation).isDirectory()) {
|
||||||
if(opts.forceDelete) {
|
if(opts.forceDelete) {
|
||||||
exports.rmdirSyncRecursive(newDirLocation);
|
exports.rmdirSyncRecursive(newDirLocation);
|
||||||
} else {
|
} else if(!opts.preserveFiles) {
|
||||||
return new Error('You are trying to delete a directory that already exists. Specify forceDelete in the opts argument to override this. Bailing~');
|
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) { }
|
} catch(e) { }
|
||||||
|
|
@ -413,8 +413,9 @@ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, opts, clbk)
|
||||||
return exports.rmdirRecursive(newDir, function(err) {
|
return exports.rmdirRecursive(newDir, function(err) {
|
||||||
copyDirRecursive.apply(this, originalArguments);
|
copyDirRecursive.apply(this, originalArguments);
|
||||||
});
|
});
|
||||||
else
|
else if(typeof opts !== 'undefined' && typeof opts !== 'function' && !opts.preserveFiles) {
|
||||||
return clbk(new Error('You are trying to delete a directory that already exists. Specify forceDelete in an options object to override this.'));
|
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')
|
if(typeof opts === 'function')
|
||||||
|
|
|
||||||
|
|
@ -78,9 +78,13 @@ function checkResultDontInflate(test, files) {
|
||||||
function checkResultPreserveFiles(test, files) {
|
function checkResultPreserveFiles(test, files) {
|
||||||
checkResultHidden(test, files);
|
checkResultHidden(test, files);
|
||||||
var contents = fs.readFileSync(path.join(__dirname, path.join('testdir2', '.hidden.txt')), "utf8");
|
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");
|
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) {
|
function checkResultOverwriteFiles(test, files) {
|
||||||
|
|
@ -196,12 +200,19 @@ module.exports = testCase({
|
||||||
// wrench.mkdirSyncRecursive(testdir1, 0777);
|
// wrench.mkdirSyncRecursive(testdir1, 0777);
|
||||||
wrench.copyDirSyncRecursive(dir, testdir1, { excludeHiddenUnix: false });
|
wrench.copyDirSyncRecursive(dir, testdir1, { excludeHiddenUnix: false });
|
||||||
wrench.copyDirSyncRecursive(dir, testdir2, { 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');
|
wrench.rmdirSyncRecursive(path.join(testdir2, 'foo'));
|
||||||
fs.writeFileSync(path.join(testdir1, "bar.txt"), 'just some text for bar.txt');
|
fs.mkdirSync(path.join(testdir2, 'foo'));
|
||||||
|
fs.writeFileSync(path.join(testdir2, 'foo/lorem.txt'), 'Lorem ipsum, please preserve my content.');
|
||||||
wrench.copyDirSyncRecursive(testdir1, testdir2, { excludeHiddenUnix: false, preserveFiles: true });
|
|
||||||
|
|
||||||
|
var err = wrench.copyDirSyncRecursive(testdir1, testdir2, { excludeHiddenUnix: false, preserveFiles: true });
|
||||||
|
test.equal(err, undefined, 'Error should not be returned');
|
||||||
|
|
||||||
var files = wrench.readdirSyncRecursive(testdir2);
|
var files = wrench.readdirSyncRecursive(testdir2);
|
||||||
|
|
||||||
checkResultPreserveFiles(test, files);
|
checkResultPreserveFiles(test, files);
|
||||||
|
|
|
||||||
Reference in a new issue