Skip to content

Commit

Permalink
Run copy*() filter before running fs.stat() on items (#971)
Browse files Browse the repository at this point in the history
Fixes #965
  • Loading branch information
RyanZim authored Oct 31, 2022
1 parent 1d1622b commit b3146f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
12 changes: 2 additions & 10 deletions lib/copy/copy-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,12 @@ function copySync (src, dest, opts) {

const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy', opts)
stat.checkParentPathsSync(src, srcStat, dest, 'copy')
return handleFilterAndCopy(destStat, src, dest, opts)
}

function handleFilterAndCopy (destStat, src, dest, opts) {
if (opts.filter && !opts.filter(src, dest)) return
const destParent = path.dirname(dest)
if (!fs.existsSync(destParent)) mkdirsSync(destParent)
return getStats(destStat, src, dest, opts)
}

function startCopy (destStat, src, dest, opts) {
if (opts.filter && !opts.filter(src, dest)) return
return getStats(destStat, src, dest, opts)
}

function getStats (destStat, src, dest, opts) {
const statSync = opts.dereference ? fs.statSync : fs.lstatSync
const srcStat = statSync(src)
Expand Down Expand Up @@ -121,8 +112,9 @@ function copyDir (src, dest, opts) {
function copyDirItem (item, src, dest, opts) {
const srcItem = path.join(src, item)
const destItem = path.join(dest, item)
if (opts.filter && !opts.filter(srcItem, destItem)) return
const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy', opts)
return startCopy(destStat, srcItem, destItem, opts)
return getStats(destStat, srcItem, destItem, opts)
}

function onLink (destStat, src, dest, opts) {
Expand Down
35 changes: 19 additions & 16 deletions lib/copy/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ function copy (src, dest, opts, cb) {
const { srcStat, destStat } = stats
stat.checkParentPaths(src, srcStat, dest, 'copy', err => {
if (err) return cb(err)
if (opts.filter) return handleFilter(checkParentDir, destStat, src, dest, opts, cb)
return checkParentDir(destStat, src, dest, opts, cb)
runFilter(src, dest, opts, (err, include) => {
if (err) return cb(err)
if (!include) return cb()

checkParentDir(destStat, src, dest, opts, cb)
})
})
})
}
Expand All @@ -53,16 +57,10 @@ function checkParentDir (destStat, src, dest, opts, cb) {
})
}

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

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

function getStats (destStat, src, dest, opts, cb) {
Expand Down Expand Up @@ -178,12 +176,17 @@ function copyDirItems (items, src, dest, opts, cb) {
function copyDirItem (items, item, src, dest, opts, cb) {
const srcItem = path.join(src, item)
const destItem = path.join(dest, item)
stat.checkPaths(srcItem, destItem, 'copy', opts, (err, stats) => {
runFilter(srcItem, destItem, opts, (err, include) => {
if (err) return cb(err)
const { destStat } = stats
startCopy(destStat, srcItem, destItem, opts, err => {
if (!include) return copyDirItems(items, src, dest, opts, cb)

stat.checkPaths(srcItem, destItem, 'copy', opts, (err, stats) => {
if (err) return cb(err)
return copyDirItems(items, src, dest, opts, cb)
const { destStat } = stats
getStats(destStat, srcItem, destItem, opts, err => {
if (err) return cb(err)
return copyDirItems(items, src, dest, opts, cb)
})
})
})
}
Expand Down

0 comments on commit b3146f0

Please sign in to comment.