From a06f88b1099e04ebe26aa9e47dbf4a356a7deeb7 Mon Sep 17 00:00:00 2001 From: Mani Maghsoudlou Date: Sun, 19 Nov 2017 00:34:48 -0800 Subject: [PATCH] Apply the async filter to initial filter checking in copy --- lib/copy/copy.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/copy/copy.js b/lib/copy/copy.js index 421436a5..fd9687fb 100644 --- a/lib/copy/copy.js +++ b/lib/copy/copy.js @@ -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) @@ -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) {