From d293448717d81e69d056794aad32fb2d4af2c246 Mon Sep 17 00:00:00 2001 From: Sukka Date: Tue, 28 Nov 2023 08:40:02 +0800 Subject: [PATCH] perf: copy dir contents in parallel (#1026) --- lib/copy/copy.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/copy/copy.js b/lib/copy/copy.js index 56560210..6304b021 100644 --- a/lib/copy/copy.js +++ b/lib/copy/copy.js @@ -113,21 +113,23 @@ async function onDir (srcStat, destStat, src, dest, opts) { await fs.mkdir(dest) } + const items = await fs.readdir(src) + // loop through the files in the current directory to copy everything - for (const item of await fs.readdir(src)) { + await Promise.all(items.map(async item => { const srcItem = path.join(src, item) const destItem = path.join(dest, item) // skip the item if it is matches by the filter function const include = await runFilter(srcItem, destItem, opts) - if (!include) continue + if (!include) return 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) - } + return getStatsAndPerformCopy(destStat, srcItem, destItem, opts) + })) if (!destStat) { await fs.chmod(dest, srcStat.mode)