diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..3d23e5d801 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,5 @@ +## 2025-05-15 - [Performance] Optimize copyDirectoryContents using glob cwd + +**Learning:** 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. It reduces manual string manipulation and allows the globbing engine to work with shorter paths. + +**Action:** Prefer `cwd` option in `glob` calls when relative paths from a base directory are needed. diff --git a/packages/cli-kit/src/public/node/fs.ts b/packages/cli-kit/src/public/node/fs.ts index 698edd4499..051e8ea400 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)