Skip to content

Commit

Permalink
Merge pull request #100 from overlookmotel/copy-options
Browse files Browse the repository at this point in the history
copy: options object or filter to pass to ncp
  • Loading branch information
jprichardson committed Jan 5, 2015
2 parents a999eff + 7ae5f03 commit 913a6fc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){}

Expand All @@ -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)
})
})
})
Expand Down
10 changes: 10 additions & 0 deletions test/copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 913a6fc

Please sign in to comment.