Skip to content

Commit e4d11e3

Browse files
authored
fix: clean current dir by removing its files, closes #339 (#342)
* fix: clean current dir by removing its files, closes #339 * clippy
1 parent 33ba1de commit e4d11e3

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

.changes/clean-target-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+
Fix panic when creating a template in the current directory.
7+

packages/cli/src/lib.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,20 @@ where
261261
exit(1);
262262
}
263263

264+
// Remove the target dir contents before rendering the template
265+
// SAFETY: Upon reaching this line, the user already accepted to overwrite
264266
if target_dir.exists() {
265-
// safe to remove, because upon reaching this line, the user accepted to overwrite
266-
fs::remove_dir_all(&target_dir)?
267-
};
268-
fs::create_dir_all(&target_dir)?;
267+
for file in fs::read_dir(&target_dir)?.flatten() {
268+
let _ = fs::remove_file(file.path());
269+
}
270+
} else {
271+
let _ = fs::create_dir_all(&target_dir);
272+
}
269273

274+
// Render the template
270275
template.render(&target_dir, pkg_manager, &package_name, alpha, mobile)?;
271276

277+
// Print post-render instructions
272278
println!();
273279
println!(
274280
"{ITALIC}{DIM}Please follow{DIMRESET} {BLUE}https://tauri.app/v1/guides/getting-started/prerequisites{WHITE} {DIM}to install the needed prerequisites, if you haven't already.{DIMRESET}{RESET}",
@@ -278,14 +284,16 @@ where
278284
}
279285
println!();
280286
println!("Done, Now run:");
281-
println!(
282-
" cd {}",
283-
if project_name.contains(' ') {
284-
format!("\"{}\"", project_name)
285-
} else {
286-
project_name
287-
}
288-
);
287+
if target_dir != cwd {
288+
println!(
289+
" cd {}",
290+
if project_name.contains(' ') {
291+
format!("\"{}\"", project_name)
292+
} else {
293+
project_name
294+
}
295+
);
296+
}
289297
if let Some(cmd) = pkg_manager.install_cmd() {
290298
println!(" {}", cmd);
291299
}
@@ -303,6 +311,7 @@ fn is_valid_pkg_name(project_name: &str) -> bool {
303311
&& !project_name
304312
.chars()
305313
.any(|ch| !(ch.is_alphanumeric() || ch == '-' || ch == '_'))
314+
&& !project_name.is_empty()
306315
}
307316

308317
fn to_valid_pkg_name(project_name: &str) -> String {

0 commit comments

Comments
 (0)