Skip to content

Commit ee877a0

Browse files
committed
feat: add a bunch of safety logic to generate-demo-project.py to avoid something bad happening with shutil rmtree on accident
1 parent 32f3bef commit ee877a0

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

scripts/generate-demo-project.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
def generate_demo_project(repo_folder: Path, demos_cache_folder: Path, demo_name: str) -> Path:
2020
"""Generates a demo project and returns its root path."""
2121
demos_cache_folder.mkdir(exist_ok=True)
22-
shutil.rmtree(path=demos_cache_folder / demo_name)
22+
_remove_existing_demo(demo_path=demos_cache_folder / demo_name)
2323
cookiecutter(
2424
template=str(repo_folder),
2525
no_input=True,
@@ -30,6 +30,23 @@ def generate_demo_project(repo_folder: Path, demos_cache_folder: Path, demo_name
3030
return demos_cache_folder / demo_name
3131

3232

33+
def _remove_existing_demo(demo_path: Path) -> None:
34+
"""Removes the existing demo if present."""
35+
if demo_path.exists() and demo_path.is_dir():
36+
previous_demo_pyproject: Path = Path(demo_path, "pyproject.toml")
37+
if not previous_demo_pyproject.exists():
38+
typer.secho(f"No pyproject.toml found at {previous_demo_pyproject=}.", fg="red")
39+
typer.confirm(
40+
"This folder may not be a demo, are you sure you would like to continue?",
41+
default=False,
42+
abort=True,
43+
show_default=True
44+
)
45+
46+
typer.secho(f"Removing existing demo project at {demo_path=}.", fg="yellow")
47+
shutil.rmtree(demo_path)
48+
49+
3350
cli: typer.Typer = typer.Typer()
3451

3552

0 commit comments

Comments
 (0)