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

copy: options object or filter to pass to ncp #100

Merged
merged 1 commit into from
Jan 5, 2015
Merged
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
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