diff --git a/lib/wrench.js b/lib/wrench.js index 4c8fb9e..771b9c6 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -151,7 +151,7 @@ exports.rmdirSyncRecursive = function(path, failSilent) { } /* Now that we know everything in the sub-tree has been deleted, we can delete the main - directory. Huzzah for the shopkeep. */ + directory. Huzzah for the shopkeep. */ return fs.rmdirSync(path); }; @@ -192,16 +192,35 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) { } var currFile = fs.lstatSync(sourceDir + "/" + files[i]); + + var fCopyFile = function(srcFile, destFile) { + if (opts.preserveFiles && fs.existsSync(destFile)) return; + + var contents = fs.readFileSync(srcFile); + fs.writeFileSync(destFile, contents); + }; + if(currFile.isDirectory()) { /* recursion this thing right on back. */ exports.copyDirSyncRecursive(sourceDir + "/" + files[i], newDirLocation + "/" + files[i], opts); } else if(currFile.isSymbolicLink()) { var symlinkFull = fs.readlinkSync(sourceDir + "/" + files[i]); - fs.symlinkSync(symlinkFull, newDirLocation + "/" + files[i]); + + if (!opts.inflateSymlinks) { + fs.symlinkSync(symlinkFull, newDirLocation + "/" + files[i]); + continue; + } + + var tmpCurrFile = fs.lstatSync(sourceDir + "/" + symlinkFull); + if (tmpCurrFile.isDirectory()) { + exports.copyDirSyncRecursive(sourceDir + "/" + symlinkFull, newDirLocation + "/" + files[i], opts); + } else { + /* At this point, we've hit a file actually worth copying... so copy it on over. */ + fCopyFile(sourceDir + "/" + symlinkFull, newDirLocation + "/" + files[i]); + } } else { /* At this point, we've hit a file actually worth copying... so copy it on over. */ - var contents = fs.readFileSync(sourceDir + "/" + files[i]); - fs.writeFileSync(newDirLocation + "/" + files[i], contents); + fCopyFile(sourceDir + "/" + files[i], newDirLocation + "/" + files[i]); } } }; diff --git a/tests/copydirsync_unix.js b/tests/copydirsync_unix.js index 59949a8..ad2a130 100644 --- a/tests/copydirsync_unix.js +++ b/tests/copydirsync_unix.js @@ -32,6 +32,50 @@ function checkResultShown(test, files) { test.deepEqual(files, check); } +function checkResultInflate(test, files) { + var check = [ + '.hidden', + 'bar.txt', + 'test', + path.join('.hidden', 'dolor.md') + ]; + + test.deepEqual(files, check); + + test.deepEqual(fs.lstatSync(path.join(__dirname, 'testdir/.hidden')).isSymbolicLink(), false); + test.deepEqual(fs.lstatSync(path.join(__dirname, 'testdir/bar.txt')).isSymbolicLink(), false); +} + +function checkResultDontInflate(test, files) { + var check = [ + '.hidden', + 'bar.txt', + 'test', + path.join('.hidden', 'dolor.md') + ]; + + test.deepEqual(files, check); + + test.deepEqual(fs.lstatSync(path.join(__dirname, 'testdir/.hidden')).isSymbolicLink(), true); + test.deepEqual(fs.lstatSync(path.join(__dirname, 'testdir/bar.txt')).isSymbolicLink(), true); +} + +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'); + contents = fs.readFileSync(path.join(__dirname, path.join('testdir2', 'bar.txt')), "utf8"); + test.deepEqual(contents, 'shown file'); +} + +function checkResultOverwriteFiles(test, files) { + checkResultHidden(test, files); + var contents = fs.readFileSync(path.join(__dirname, path.join('testdir2', '.hidden.txt')), "utf8"); + test.deepEqual(contents, 'just some text for .hidden.txt'); + contents = fs.readFileSync(path.join(__dirname, path.join('testdir2', 'bar.txt')), "utf8"); + test.deepEqual(contents, 'just some text for bar.txt'); +} + module.exports = testCase({ test_copyDirSyncRecursiveHidden: function(test) { var dir = path.join(__dirname, 'shown'); @@ -65,8 +109,93 @@ module.exports = testCase({ wrench.rmdirSyncRecursive(testdir); + test.done(); + }, + test_copyDirSyncRecursiveInflate: function(test) { + var dir = path.join(__dirname, 'withsymlinks'); + var testdir = path.join(__dirname, 'testdir'); + + test.ok(path.existsSync(dir), 'Folders should exist'); + + wrench.mkdirSyncRecursive(testdir, 0777); + wrench.copyDirSyncRecursive(dir, testdir, { excludeHiddenUnix: false, inflateSymlinks: true }); + + var files = wrench.readdirSyncRecursive(testdir); + + checkResultInflate(test, files); + + wrench.rmdirSyncRecursive(testdir); + + test.done(); + }, + test_copyDirSyncRecursiveDontInflate: function(test) { + var dir = path.join(__dirname, 'withsymlinks'); + var testdir = path.join(__dirname, 'testdir'); + + test.ok(path.existsSync(dir), 'Folders should exist'); + + wrench.mkdirSyncRecursive(testdir, 0777); + wrench.copyDirSyncRecursive(dir, testdir, { excludeHiddenUnix: false, inflateSymlinks: false }); + + var files = wrench.readdirSyncRecursive(testdir); + + checkResultDontInflate(test, files); + + wrench.rmdirSyncRecursive(testdir); + + test.done(); + }, + test_copyDirSyncRecursivePreserveFiles: function(test) { + var dir = path.join(__dirname, 'shown'), + testdir1 = path.join(__dirname, 'testdir1'), + testdir2 = path.join(__dirname, 'testdir2'); + + test.ok(path.existsSync(dir), 'Folders should exist'); + + 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, { preserve: true, excludeHiddenUnix: false, preserveFiles: true }); + + var files = wrench.readdirSyncRecursive(testdir2); + + checkResultPreserveFiles(test, files); + + wrench.rmdirSyncRecursive(testdir1); + wrench.rmdirSyncRecursive(testdir2); + + test.done(); + }, + test_copyDirSyncRecursiveOverwriteFiles: function(test) { + var dir = path.join(__dirname, 'shown'), + testdir1 = path.join(__dirname, 'testdir1'), + testdir2 = path.join(__dirname, 'testdir2'); + + test.ok(path.existsSync(dir), 'Folders should exist'); + + 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, { preserve: true, excludeHiddenUnix: false, preserveFiles: false }); + + var files = wrench.readdirSyncRecursive(testdir2); + + checkResultOverwriteFiles(test, files); + + wrench.rmdirSyncRecursive(testdir1); + wrench.rmdirSyncRecursive(testdir2); + test.done(); } + }); // vim: et ts=4 sw=4 diff --git a/tests/shown/.hidden.txt b/tests/shown/.hidden.txt index e69de29..b96b725 100644 --- a/tests/shown/.hidden.txt +++ b/tests/shown/.hidden.txt @@ -0,0 +1 @@ +hidden file \ No newline at end of file diff --git a/tests/shown/bar.txt b/tests/shown/bar.txt index e69de29..3d36c5c 100644 --- a/tests/shown/bar.txt +++ b/tests/shown/bar.txt @@ -0,0 +1 @@ +shown file \ No newline at end of file diff --git a/tests/withsymlinks/.hidden b/tests/withsymlinks/.hidden new file mode 120000 index 0000000..5f2d9d6 --- /dev/null +++ b/tests/withsymlinks/.hidden @@ -0,0 +1 @@ +../shown/.hidden \ No newline at end of file diff --git a/tests/withsymlinks/bar.txt b/tests/withsymlinks/bar.txt new file mode 120000 index 0000000..383a7bb --- /dev/null +++ b/tests/withsymlinks/bar.txt @@ -0,0 +1 @@ +../shown/bar.txt \ No newline at end of file diff --git a/tests/withsymlinks/test b/tests/withsymlinks/test new file mode 100644 index 0000000..dba6882 --- /dev/null +++ b/tests/withsymlinks/test @@ -0,0 +1 @@ +aaa bbb ccc ddd \ No newline at end of file