Skip to content

Commit

Permalink
Merge pull request #33 from Srirangan/master
Browse files Browse the repository at this point in the history
fs_extra.copySync
  • Loading branch information
jprichardson committed Oct 12, 2013
2 parents 1bb07a3 + 83226a3 commit 044d296
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules/
node_modules/

.idea
*.iml
21 changes: 19 additions & 2 deletions lib/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var fs = require('fs')
, ncp = require('ncp').ncp
, path = require('path')
, mkdir = require('./mkdir')
, mkdir = require('./mkdir'),
create = require('./create')

var BUF_LENGTH = 64 * 1024
var _buff = new Buffer(BUF_LENGTH)
Expand Down Expand Up @@ -58,7 +59,23 @@ function copy(src, dest, callback) {
})
}

function copySync(src, dest) {
var stats = fs.lstatSync(src),
destExists = fs.exists(dest);
if (stats.isFile()) {
if (!destExists) create.createFileSync(dest);
copyFileSync(src, dest);
}
else if (stats.isDirectory()) {
if (!destExists) mkdir.mkdirsSync(dest);
var contents = fs.readdirSync(src);
contents.forEach(function (content) {
copySync(src + "/" + content, dest + "/" + content);
});
}
}

module.exports.copyFileSync = copyFileSync;
module.exports.copyFile = copyFile;
module.exports.copy = copy;
module.exports.copy = copy;
module.exports.copySync = copySync;
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fs = fse;
// copy

fs.copy = require('./copy').copy;
fs.copySync = require('./copy').copySync;

// remove

Expand Down
106 changes: 91 additions & 15 deletions test/copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ describe('fs-extra', function() {
//DIR = path.join(DIR, 'copy')
//mkdir.sync(DIR)
})

afterEach(function(done) {
fs.remove(DIR, done);
})

/*
describe '+ copyFileSync()', ->
it 'should copy synchronously', ->
fileSrc = path.join(DIR, "TEST_fs-extra_src")
fileDest = path.join(DIR, "TEST_fs-extra_copy")
fileSrc = testutil.createFileWithData(fileSrc, SIZE)
srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex')
fs.copyFileSync(fileSrc, fileDest)
destMd5 = crypto.createHash('md5').update(fs.readFileSync(fileDest)).digest("hex")
T srcMd5 is destMd5
*/

Expand All @@ -40,7 +40,7 @@ describe('fs-extra', function() {
, fileSrc = testutil.createFileWithData(fileSrc, SIZE)
, srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest("hex")
, destMd5 = '';

fs.copy(fileSrc, fileDest, function(err) {
destMd5 = crypto.createHash('md5').update(fs.readFileSync(fileDest)).digest("hex");
T (srcMd5 === destMd5);
Expand Down Expand Up @@ -70,28 +70,28 @@ describe('fs-extra', function() {
var FILES = 2
, src = path.join(DIR, 'src')
, dest = path.join(DIR, 'dest');

mkdir(src, function(err) {
for (var i = 0; i < FILES; ++i)
for (var i = 0; i < FILES; ++i)
testutil.createFileWithData(path.join(src, i.toString()), SIZE);

var subdir = path.join(src, 'subdir');
mkdir(subdir, function(err) {
for (var i = 0; i < FILES; ++i)
for (var i = 0; i < FILES; ++i)
testutil.createFileWithData(path.join(subdir, i.toString()), SIZE);

fs.copy(src, dest, function(err) {
F (err);
T (fs.existsSync(dest));
for (var i = 0; i < FILES; ++i)

for (var i = 0; i < FILES; ++i)
T (fs.existsSync(path.join(dest, i.toString())));


var destSub = path.join(dest, 'subdir');
for (var j = 0; j < FILES; ++j)
T (fs.existsSync(path.join(destSub, j.toString())));

done()
})
})
Expand Down Expand Up @@ -123,6 +123,82 @@ describe('fs-extra', function() {
})
})
})

describe("+ copySync()", function () {
describe("> when the source is a file", function () {
it("should copy the file synchronously", function (done) {
var fileSrc = path.join(DIR, "TEST_fs-extra_src")
, fileDest = path.join(DIR, "TEST_fs-extra_copy")
, fileSrc = testutil.createFileWithData(fileSrc, SIZE)
, srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest("hex")
, destMd5 = '';
fs.copySync(fileSrc, fileDest);
destMd5 = crypto.createHash('md5').update(fs.readFileSync(fileDest)).digest("hex");
T(srcMd5 === destMd5);
done();
});
describe("> when the destination dir does not exist", function () {
it('should create the destination directory and copy the file', function (done) {
var src = path.join(DIR, 'file.txt'),
dest = path.join(DIR, 'this/path/does/not/exist/copied.txt'),
data = "did it copy?\n";

fs.writeFileSync(src, data, 'utf8');
fs.copySync(src, dest);
var data2 = fs.readFileSync(dest, 'utf8');
EQ(data, data2)
done();
})
});
});
describe("> when the source is a directory", function() {
it("should copy the directory synchronously", function(done) {
var FILES = 2,
src = path.join(DIR, 'src'),
dest = path.join(DIR, 'dest'),
i, j;
mkdir.sync(src);
for (i = 0; i < FILES; ++i)
testutil.createFileWithData(path.join(src, i.toString()), SIZE);
var subdir = path.join(src, 'subdir');
mkdir.sync(subdir);
for (i = 0; i < FILES; ++i)
testutil.createFileWithData(path.join(subdir, i.toString()), SIZE);
fs.copySync(src, dest);
T (fs.existsSync(dest));

for (i = 0; i < FILES; ++i) T (fs.existsSync(path.join(dest, i.toString())));

var destSub = path.join(dest, 'subdir');
for (j = 0; j < FILES; ++j) T (fs.existsSync(path.join(destSub, j.toString())));

done()
});
describe("> when the destination dir does not exist", function() {
it("should create the destination directory and copy the file", function(done) {
var src = path.join(DIR, 'data/');
fs.mkdirsSync(src);

var d1 = "file1",
d2 = "file2",
f1 = fs.writeFileSync(path.join(src, "f1.txt"), d1),
f2 = fs.writeFileSync(path.join(src, "f2.txt"), d2);

var dest = path.join(DIR, 'this/path/does/not/exist/outputDir');

fs.copySync(src, dest);

var o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8'),
o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8');

EQ (d1, o1);
EQ (d2, o2);

done()
});
});
});
});
})


0 comments on commit 044d296

Please sign in to comment.