Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .jules/refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## 2025-05-15 - Refactor `copyDirectoryContents` to use robust path handling

**Learning:** Manual string manipulation for calculating relative paths (e.g., using `.replace(srcDir, '')`) is fragile and prone to errors with special characters or complex directory structures.
**Action:** Use the `cwd` option in `glob` utilities to retrieve relative paths directly from the filesystem, and use standardized path-joining utilities (like `joinPath`) to construct destination paths.
**Code Smell:** Imperative loops for parallelizable tasks.
**Solution:** Use `.map()` with `Promise.all()` for cleaner, more expressive parallel execution.
21 changes: 9 additions & 12 deletions packages/cli-kit/src/public/node/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}),
)
}
Loading