Skip to content

Commit

Permalink
Merge pull request #521 from jprichardson/async-filter
Browse files Browse the repository at this point in the history
Apply the async filter to initial filter checking in copy
  • Loading branch information
RyanZim committed Nov 20, 2017
2 parents 86d2ad6 + a06f88b commit 42e42be
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/copy/copy.js
Expand Up @@ -35,8 +35,11 @@ function copy (src, dest, opts, cb) {
// don't allow src and dest to be the same
if (src === dest) return cb(new Error('Source and destination must not be the same.'))

if (opts.filter && !opts.filter(src, dest)) return cb()
if (opts.filter) return handleFilter(checkParentDir, src, dest, opts, cb)
return checkParentDir(src, dest, opts, cb)
}

function checkParentDir (src, dest, opts, cb) {
const destParent = path.dirname(dest)
pathExists(destParent, (err, dirExists) => {
if (err) return cb(err)
Expand All @@ -49,13 +52,16 @@ function copy (src, dest, opts, cb) {
}

function startCopy (src, dest, opts, cb) {
if (opts.filter) {
Promise.resolve(opts.filter(src, dest))
.then(include => {
if (include) getStats(src, dest, opts, cb)
else cb()
}, error => cb(error))
} else getStats(src, dest, opts, cb)
if (opts.filter) return handleFilter(getStats, src, dest, opts, cb)
return getStats(src, dest, opts, cb)
}

function handleFilter (onInclude, src, dest, opts, cb) {
Promise.resolve(opts.filter(src, dest))
.then(include => {
if (include) return onInclude(src, dest, opts, cb)
return cb()
}, error => cb(error))
}

function getStats (src, dest, opts, cb) {
Expand Down

0 comments on commit 42e42be

Please sign in to comment.