From 7ae5f03a78bce6d2370fda85eee1459c41f86adc Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Sun, 4 Jan 2015 16:55:30 +0000 Subject: [PATCH] copy: options object or filter to pass to ncp --- lib/copy.js | 14 ++++++++------ test/copy.test.js | 10 ++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/copy.js b/lib/copy.js index 94bf045c..31adb474 100644 --- a/lib/copy.js +++ b/lib/copy.js @@ -24,10 +24,12 @@ var copyFileSync = function(srcFile, destFile) { fs.closeSync(fdw) } -function copy(src, dest, filter, callback) { - if( typeof filter == "function" && !callback) { - callback = filter - filter = null +function copy(src, dest, options, callback) { + if( typeof options == "function" && !callback) { + callback = options + options = {} + } else if (typeof options == "function" || options instanceof RegExp) { + options = {filter: options} } callback = callback || function(){} @@ -44,10 +46,10 @@ function copy(src, dest, filter, callback) { } fs.exists(dir, function(dirExists) { - if (dirExists) return ncp(src, dest, {filter: filter}, callback) + if (dirExists) return ncp(src, dest, options, callback) mkdir.mkdirs(dir, function(err) { if (err) return callback(err) - ncp(src, dest, {filter: filter}, callback) + ncp(src, dest, options, callback) }) }) }) diff --git a/test/copy.test.js b/test/copy.test.js index 876a0f26..642dd9c4 100644 --- a/test/copy.test.js +++ b/test/copy.test.js @@ -68,6 +68,16 @@ describe('fs-extra', function() { }) }) + it("accepts options object in place of filter", function(done) { + var srcFile1 = testlib.createFileWithData(path.join(DIR, "1.jade"), SIZE) + var destFile1 = path.join(DIR, "dest1.jade") + var options = {filter: /.html$|.css$/i} + fs.copy(srcFile1, destFile1, options, function() { + assert(!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')