Skip to content

Commit

Permalink
perf(copy): parallel copy
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Nov 28, 2023
1 parent 1a80ec5 commit 46b1ac3
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/copy/copy.js
Expand Up @@ -113,6 +113,8 @@ async function onDir (srcStat, destStat, src, dest, opts) {
await fs.mkdir(dest)
}

const promises = []

// loop through the files in the current directory to copy everything
for await (const item of await fs.opendir(src)) {
const srcItem = path.join(src, item.name)
Expand All @@ -122,13 +124,17 @@ async function onDir (srcStat, destStat, src, dest, opts) {
const include = await runFilter(srcItem, destItem, opts)
if (!include) continue

const { destStat } = await stat.checkPaths(srcItem, destItem, 'copy', opts)

// If the item is a copyable file, `getStatsAndPerformCopy` will copy it
// If the item is a directory, `getStatsAndPerformCopy` will call `onDir` recursively
await getStatsAndPerformCopy(destStat, srcItem, destItem, opts)
promises.push(
stat.checkPaths(srcItem, destItem, 'copy', opts).then(({ destStat }) => {
// If the item is a copyable file, `getStatsAndPerformCopy` will copy it
// If the item is a directory, `getStatsAndPerformCopy` will call `onDir` recursively
return getStatsAndPerformCopy(destStat, srcItem, destItem, opts)
})
)
}

await Promise.all(promises)

if (!destStat) {
await fs.chmod(dest, srcStat.mode)
}
Expand Down

0 comments on commit 46b1ac3

Please sign in to comment.