Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter parameter fs.copy and fs.copySync #35

Closed
wants to merge 2 commits 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
21 changes: 13 additions & 8 deletions lib/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var copyFileSync = function(srcFile, destFile) {
return fs.closeSync(fdw);
}

function copy(src, dest, callback) {
function copy(src, dest, filter, callback) {
callback = callback || function(){}

fs.lstat(src, function(err, stats) {
Expand All @@ -43,21 +43,27 @@ function copy(src, dest, callback) {
}

fs.exists(dir, function(dirExists) {
if (dirExists) return ncp(src, dest, callback)
if (dirExists) return ncp(src, dest, {filter: filter}, callback);
mkdir.mkdirs(dir, function(err) {
if (err) return callback(err)
ncp(src, dest, callback)
ncp(src, dest, {filter: filter}, callback);
})
})
})
}

function copySync(src, dest) {
function copySync(src, dest, filter) {
filter = filter || function () { return true; };
var stats = fs.lstatSync(src),
destExists = fs.exists(dest);
destExists = fs.exists(dest),
performCopy = false;
if (stats.isFile()) {
if (!destExists) create.createFileSync(dest);
copyFileSync(src, dest);
if (filter instanceof RegExp) performCopy = filter.test(src);
else if (typeof filter === "function") performCopy = filter(src);
if(performCopy) {
if (!destExists) create.createFileSync(dest);
copyFileSync(src, dest);
}
}
else if (stats.isDirectory()) {
if (!destExists) mkdir.mkdirsSync(dest);
Expand All @@ -67,4 +73,3 @@ function copySync(src, dest) {
});
}
}

60 changes: 56 additions & 4 deletions test/copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,33 @@ describe('fs-extra', function() {
, srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest("hex")
, destMd5 = '';

fs.copy(fileSrc, fileDest, function(err) {
fs.copy(fileSrc, fileDest, null, function(err) {
destMd5 = crypto.createHash('md5').update(fs.readFileSync(fileDest)).digest("hex");
T (srcMd5 === destMd5);
done()
})
})

it("should only copy files allowed by filter regex", function(done) {
var srcFile1 = testutil.createFileWithData(path.join(DIR, "1.jade"), SIZE);
var destFile1 = path.join(DIR, "dest1.jade");
var filter = /.html$|.css$/i;
fs.copy(srcFile1, destFile1, filter, function() {
T(!fs.existsSync(destFile1));
done();
});
});

it("should only copy files allowed by filter fn", function(done) {
var srcFile1 = testutil.createFileWithData(path.join(DIR, "1.css"), SIZE);
var destFile1 = path.join(DIR, "dest1.css");
var filter = function(s) { return s.split(".").pop() !== "css";};
fs.copy(srcFile1, destFile1, filter, function() {
T(!fs.existsSync(destFile1));
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')
Expand All @@ -56,7 +76,7 @@ describe('fs-extra', function() {

fs.writeFileSync(src, data, 'utf8')

fs.copy(src, dest, function(err) {
fs.copy(src, dest, null, function(err) {
var data2 = fs.readFileSync(dest, 'utf8')
EQ (data, data2)
done(err)
Expand All @@ -80,7 +100,7 @@ describe('fs-extra', function() {
for (var i = 0; i < FILES; ++i)
testutil.createFileWithData(path.join(subdir, i.toString()), SIZE);

fs.copy(src, dest, function(err) {
fs.copy(src, dest, null, function(err) {
F (err);
T (fs.existsSync(dest));

Expand Down Expand Up @@ -110,7 +130,7 @@ describe('fs-extra', function() {

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

fs.copy(src, dest, function(err) {
fs.copy(src, dest, null, function(err) {
var o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8')
var o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8')

Expand All @@ -137,6 +157,38 @@ describe('fs-extra', function() {
T(srcMd5 === destMd5);
done();
});
it("should only copy files allowed by filter regex", function(done) {
var srcFile1 = testutil.createFileWithData(path.join(DIR, "1.html"), SIZE),
srcFile2 = testutil.createFileWithData(path.join(DIR, "2.css"), SIZE),
srcFile3 = testutil.createFileWithData(path.join(DIR, "3.jade"), SIZE);
var destFile1 = path.join(DIR, "dest1.html"),
destFile2 = path.join(DIR, "dest2.css"),
destFile3 = path.join(DIR, "dest3.jade");
var filter = /.html$|.css$/i;
fs.copySync(srcFile1, destFile1, filter);
fs.copySync(srcFile2, destFile2, filter);
fs.copySync(srcFile3, destFile3, filter);
T(fs.existsSync(destFile1));
T(fs.existsSync(destFile2));
T(!fs.existsSync(destFile3));
done();
});
it("should only copy files allowed by filter fn", function(done) {
var srcFile1 = testutil.createFileWithData(path.join(DIR, "1.html"), SIZE),
srcFile2 = testutil.createFileWithData(path.join(DIR, "2.css"), SIZE),
srcFile3 = testutil.createFileWithData(path.join(DIR, "3.jade"), SIZE);
var destFile1 = path.join(DIR, "dest1.html"),
destFile2 = path.join(DIR, "dest2.css"),
destFile3 = path.join(DIR, "dest3.jade");
var filter = function(s) { return s.split(".").pop() !== "css";};
fs.copySync(srcFile1, destFile1, filter);
fs.copySync(srcFile2, destFile2, filter);
fs.copySync(srcFile3, destFile3, filter);
T(fs.existsSync(destFile1));
T(!fs.existsSync(destFile2));
T(fs.existsSync(destFile3));
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'),
Expand Down