Skip to content

Commit

Permalink
perf(copy): run filter in parallel as well
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 1, 2023
1 parent 46b1ac3 commit f3e9808
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions lib/copy/copy.js
Expand Up @@ -7,7 +7,7 @@ const { pathExists } = require('../path-exists')
const { utimesMillis } = require('../util/utimes')
const stat = require('../util/stat')

async function copy (src, dest, opts = {}) {
async function copy(src, dest, opts = {}) {
if (typeof opts === 'function') {
opts = { filter: opts }
}
Expand Down Expand Up @@ -42,12 +42,12 @@ async function copy (src, dest, opts = {}) {
await getStatsAndPerformCopy(destStat, src, dest, opts)
}

async function runFilter (src, dest, opts) {
async function runFilter(src, dest, opts) {
if (!opts.filter) return true
return opts.filter(src, dest)
}

async function getStatsAndPerformCopy (destStat, src, dest, opts) {
async function getStatsAndPerformCopy(destStat, src, dest, opts) {
const statFn = opts.dereference ? fs.stat : fs.lstat
const srcStat = await statFn(src)

Expand All @@ -65,7 +65,7 @@ async function getStatsAndPerformCopy (destStat, src, dest, opts) {
throw new Error(`Unknown file: ${src}`)
}

async function onFile (srcStat, destStat, src, dest, opts) {
async function onFile(srcStat, destStat, src, dest, opts) {
if (!destStat) return copyFile(srcStat, src, dest, opts)

if (opts.overwrite) {
Expand All @@ -77,7 +77,7 @@ async function onFile (srcStat, destStat, src, dest, opts) {
}
}

async function copyFile (srcStat, src, dest, opts) {
async function copyFile(srcStat, src, dest, opts) {
await fs.copyFile(src, dest)
if (opts.preserveTimestamps) {
// Make sure the file is writable before setting the timestamp
Expand All @@ -99,15 +99,15 @@ async function copyFile (srcStat, src, dest, opts) {
return fs.chmod(dest, srcStat.mode)
}

function fileIsNotWritable (srcMode) {
function fileIsNotWritable(srcMode) {
return (srcMode & 0o200) === 0
}

function makeFileWritable (dest, srcMode) {
function makeFileWritable(dest, srcMode) {
return fs.chmod(dest, srcMode | 0o200)
}

async function onDir (srcStat, destStat, src, dest, opts) {
async function onDir(srcStat, destStat, src, dest, opts) {
// the dest directory might not exist, create it
if (!destStat) {
await fs.mkdir(dest)
Expand All @@ -120,15 +120,16 @@ async function onDir (srcStat, destStat, src, dest, opts) {
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) continue

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)
runFilter(srcItem, destItem, opts).then(include => {
if (include) {
// only copy the item if it matches the filter function
return 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)
})
}
})
)
}
Expand All @@ -140,7 +141,7 @@ async function onDir (srcStat, destStat, src, dest, opts) {
}
}

async function onLink (destStat, src, dest, opts) {
async function onLink(destStat, src, dest, opts) {
let resolvedSrc = await fs.readlink(src)
if (opts.dereference) {
resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
Expand Down

0 comments on commit f3e9808

Please sign in to comment.