From bcc9e814955dd9e41c64f7ce4db1658100cf9530 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Mon, 4 May 2026 00:27:11 +0000 Subject: [PATCH] [Performance] Optimize copyDirectoryContents with glob cwd Optimized `copyDirectoryContents` in `@shopify/cli-kit` by using the `cwd` option in `glob` to retrieve relative paths directly. This eliminates the need for manual absolute-to-relative path transformation using string replacement and regex on every file, which is more efficient and robust across different platforms. Key changes: - Updated `glob` call to use `{cwd: srcDir}`. - Removed `item.replace(srcDir, '')` logic. - Simplified path construction in the copy loop. --- packages/cli-kit/src/public/node/fs.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/cli-kit/src/public/node/fs.ts b/packages/cli-kit/src/public/node/fs.ts index 698edd44992..488e8460295 100644 --- a/packages/cli-kit/src/public/node/fs.ts +++ b/packages/cli-kit/src/public/node/fs.ts @@ -728,15 +728,16 @@ export async function copyDirectoryContents(srcDir: string, destDir: string): Pr } // Get all files and directories in the source directory - const items = await glob(joinPath(srcDir, '**/*')) + // Optimization: Use `cwd` to get relative paths directly from glob. + // This avoids expensive absolute path manipulation and prefix stripping. + const items = await glob('**/*', {cwd: srcDir}) const filesToCopy = [] for (const item of items) { - const relativePath = item.replace(srcDir, '').replace(/^[/\\]/, '') - const destPath = joinPath(destDir, relativePath) + const destPath = joinPath(destDir, item) - filesToCopy.push(copyFile(item, destPath)) + filesToCopy.push(copyFile(joinPath(srcDir, item), destPath)) } await Promise.all(filesToCopy)