Skip to content

Commit

Permalink
refactor(copy): backport nodejs/node#41351
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Nov 28, 2023
1 parent 5d4bf0b commit 1a80ec5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
12 changes: 11 additions & 1 deletion lib/copy/copy-sync.js
Expand Up @@ -106,7 +106,17 @@ function mkDirAndCopy (srcMode, src, dest, opts) {
}

function copyDir (src, dest, opts) {
fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
const dir = fs.opendirSync(src)

try {
let dirent

while ((dirent = dir.readSync()) !== null) {
copyDirItem(dirent.name, src, dest, opts)
}
} finally {
dir.closeSync()
}
}

function copyDirItem (item, src, dest, opts) {
Expand Down
14 changes: 6 additions & 8 deletions lib/copy/copy.js
Expand Up @@ -113,23 +113,21 @@ 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
await Promise.all(items.map(async item => {
const srcItem = path.join(src, item)
const destItem = path.join(dest, item)
for await (const item of await fs.opendir(src)) {
const srcItem = path.join(src, item.name)
const destItem = path.join(dest, item.name)

// skip the item if it is matches by the filter function
const include = await runFilter(srcItem, destItem, opts)
if (!include) return
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
return getStatsAndPerformCopy(destStat, srcItem, destItem, opts)
}))
await getStatsAndPerformCopy(destStat, srcItem, destItem, opts)
}

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

0 comments on commit 1a80ec5

Please sign in to comment.