Skip to content

Commit 88944c8

Browse files
authored
fix(cli): recusively clean dirty target dir (#430)
1 parent 44486bd commit 88944c8

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

.changes/cleaning-existing-dir.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"create-tauri-app": "patch"
3+
"create-tauri-app-js": "patch"
4+
---
5+
6+
Recursively clean the target directory if bootstrapping a template into a non-empty directory.
7+

packages/cli/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,19 @@ where
269269
// Remove the target dir contents before rendering the template
270270
// SAFETY: Upon reaching this line, the user already accepted to overwrite
271271
if target_dir.exists() {
272-
for file in fs::read_dir(&target_dir)?.flatten() {
273-
let _ = fs::remove_file(file.path());
272+
fn clean_dir(dir: &std::path::PathBuf) -> anyhow::Result<()> {
273+
for entry in fs::read_dir(dir)?.flatten() {
274+
let path = entry.path();
275+
if entry.file_type()?.is_dir() {
276+
clean_dir(&path)?;
277+
std::fs::remove_dir(path)?;
278+
} else {
279+
fs::remove_file(path)?;
280+
}
281+
}
282+
Ok(())
274283
}
284+
clean_dir(&target_dir)?;
275285
} else {
276286
let _ = fs::create_dir_all(&target_dir);
277287
}

0 commit comments

Comments
 (0)