diff --git a/lib/copy/copy.js b/lib/copy/copy.js index 5514ab50..f270c1d7 100644 --- a/lib/copy/copy.js +++ b/lib/copy/copy.js @@ -34,22 +34,25 @@ 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.')) - return startCopy(src, dest, opts, cb) -} -function startCopy (src, dest, opts, cb) { if (opts.filter && !opts.filter(src, dest)) return cb() + const destParent = path.dirname(dest) pathExists(destParent, (err, dirExists) => { if (err) return cb(err) - if (dirExists) return getStats(src, dest, opts, cb) + if (dirExists) return startCopy(src, dest, opts, cb) mkdirp(destParent, err => { if (err) return cb(err) - return getStats(src, dest, opts, cb) + return startCopy(src, dest, opts, cb) }) }) } +function startCopy (src, dest, opts, cb) { + if (opts.filter && !opts.filter(src, dest)) return cb() + return getStats(src, dest, opts, cb) +} + function getStats (src, dest, opts, cb) { const stat = opts.dereference ? fs.stat : fs.lstat stat(src, (err, st) => {