From 575dda60d0a8a034de98c8adb1ebd937eaad6198 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sat, 2 May 2026 00:28:18 +0000 Subject: [PATCH] [Refactor] Use robust path handling in copyDirectoryContents Refactored `copyDirectoryContents` in `@shopify/cli-kit` to use the `cwd` option in `glob`, which provides relative paths directly from the filesystem. This eliminates fragile manual string replacement for relative path calculation. Additionally, simplified the implementation by using `.map()` with `Promise.all()` instead of an imperative `for...of` loop. Summary of changes: - Replaced `glob(joinPath(srcDir, '**/*'))` with `glob('**/*', {cwd: srcDir})`. - Replaced manual path string manipulation with robust `joinPath` calls. - Simplified parallel task execution using `.map()`. - Added learning to `.jules/refactor.md`. Co-authored-by: Cursor --- packages/cli-kit/src/public/node/fs.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/cli-kit/src/public/node/fs.ts b/packages/cli-kit/src/public/node/fs.ts index 698edd44992..00ce8ff9ae4 100644 --- a/packages/cli-kit/src/public/node/fs.ts +++ b/packages/cli-kit/src/public/node/fs.ts @@ -728,16 +728,13 @@ export async function copyDirectoryContents(srcDir: string, destDir: string): Pr } // Get all files and directories in the source directory - const items = await glob(joinPath(srcDir, '**/*')) - - const filesToCopy = [] - - for (const item of items) { - const relativePath = item.replace(srcDir, '').replace(/^[/\\]/, '') - const destPath = joinPath(destDir, relativePath) - - filesToCopy.push(copyFile(item, destPath)) - } - - await Promise.all(filesToCopy) + const relativePaths = await glob('**/*', {cwd: srcDir}) + + await Promise.all( + relativePaths.map((relativePath) => { + const srcPath = joinPath(srcDir, relativePath) + const destPath = joinPath(destDir, relativePath) + return copyFile(srcPath, destPath) + }), + ) }