Skip to content

Commit

Permalink
Merge pull request #85 from Ackar/master
Browse files Browse the repository at this point in the history
Copy symlinks in copySync
  • Loading branch information
jprichardson committed Sep 22, 2014
2 parents e8456ee + e1c3056 commit 57bc435
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ function copy(src, dest, filter, callback) {
})
}

function copySync(src, dest, filter) {
function copySync(src, dest, filter, recursive) {
filter = filter || function () { return true; };
var stats = fs.lstatSync(src),
recursive = recursive || false;
var stats = recursive ? fs.lstatSync(src) : fs.statSync(src),
destFolder = path.dirname(dest),
destFolderExists = fs.exists(destFolder),
performCopy = false;
Expand All @@ -75,7 +76,10 @@ function copySync(src, dest, filter) {
if (!destFolderExists) mkdir.mkdirsSync(dest);
var contents = fs.readdirSync(src);
contents.forEach(function (content) {
copySync(src + "/" + content, dest + "/" + content, filter);
copySync(src + "/" + content, dest + "/" + content, filter, true);
});
} else if (recursive && stats.isSymbolicLink()) {
var srcPath = fs.readlinkSync(src);
fs.symlinkSync(srcPath, dest);
}
}
31 changes: 31 additions & 0 deletions test/copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ describe('fs-extra', function() {
done();
});

it("should follow symlinks", function (done) {
var fileSrc = path.join(DIR, "TEST_fs-extra_src")
, fileDest = path.join(DIR, "TEST_fs-extra_copy")
, linkSrc = path.join(DIR, "TEST_fs-extra_copy_link")
, fileSrc = testutil.createFileWithData(fileSrc, SIZE)
, srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest("hex")
, destMd5 = '';

fs.symlinkSync(fileSrc, linkSrc);
fs.copySync(linkSrc, fileDest);
destMd5 = crypto.createHash('md5').update(fs.readFileSync(fileDest)).digest("hex");
T(srcMd5 === destMd5);
done();
});

it("should maintain file mode", function (done) {
var fileSrc = path.join(DIR, "TEST_fs-extra_src")
, fileDest = path.join(DIR, "TEST_fs-extra_copy")
Expand Down Expand Up @@ -341,6 +356,22 @@ describe('fs-extra', function() {
done()
});

it("should preserve symbolic links", function(done) {
var FILES = 2,
src = path.join(DIR, 'src'),
dest = path.join(DIR, 'dest'),
i, j;
mkdir.sync(src);
fs.symlinkSync('destination', path.join(src, 'symlink'));

fs.copySync(src, dest);

var link = fs.readlinkSync(path.join(dest, 'symlink'));
EQ (link, 'destination');

done()
});

it("should should apply filter recursively", function(done) {
var FILES = 2
var src = path.join(DIR, 'src')
Expand Down

0 comments on commit 57bc435

Please sign in to comment.