From d7af4ffc59d3d3111a7c8b009e008b2f3a641f9d Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sun, 3 May 2026 00:41:43 +0000 Subject: [PATCH] [Performance] Optimize copyDirectoryContents using glob cwd The `copyDirectoryContents` function used an absolute path with `glob`, followed by manual string replacement to calculate relative paths for the destination. This is less efficient and more error-prone than using the built-in `cwd` option in `glob`. This change: - Updates `copyDirectoryContents` to use the `cwd` option in the `glob` call. - Simplifies the relative path calculation by removing manual string stripping. - Reduces memory overhead by working with relative paths during the globbing process. Co-authored-by: Cursor --- 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..051e8ea4007 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: Using the `cwd` option in `glob` is more efficient than passing an absolute path + // and then manually stripping the source directory string to calculate relative destination paths. + 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)