diff --git a/src/render_engine_cli/cli.py b/src/render_engine_cli/cli.py index 363ff4d..b7db02b 100644 --- a/src/render_engine_cli/cli.py +++ b/src/render_engine_cli/cli.py @@ -27,6 +27,7 @@ ) MODULE_SITE_HELP = "The module (python file) and site (the Site object) for your site in the format module:site" +console = Console() try: # Get the RE version for display. If it's not set it means we're working locally. @@ -140,7 +141,7 @@ def build(module_site: str, clean: bool): module, site_name = split_module_site(module_site) site = get_site(module, site_name) if clean: - remove_output_folder(Path(site.output_path)) + remove_output_folder(Path(site.output_path), console=console) site.render() @@ -154,7 +155,7 @@ def build(module_site: str, clean: bool): @click.option( "-c", "--clean", - help="Clean the output folder prior to building.", + help="Clean the output folder prior to building. If `--reload` is set this will clean on each reload.", is_flag=True, default=False, ) @@ -165,7 +166,13 @@ def build(module_site: str, clean: bool): is_flag=True, default=False, ) -@click.option("-p", "--port", type=click.IntRange(0, 65534), help="Port to serve on", default=8000.0) +@click.option( + "-p", + "--port", + type=click.IntRange(0, 65534), + help="Port to serve on", + default=8000.0, +) def serve(module_site: str, clean: bool, reload: bool, port: int): """ Create an HTTP server to serve the site at `localhost`. @@ -186,7 +193,7 @@ def serve(module_site: str, clean: bool, reload: bool, port: int): site = get_site(module, site_name) if clean: - remove_output_folder(Path(site.output_path)) + remove_output_folder(Path(site.output_path), console=console) site.render() server_address = ("127.0.0.1", port) @@ -199,6 +206,7 @@ def serve(module_site: str, clean: bool, reload: bool, port: int): output_path=site.output_path, patterns=None, ignore_patterns=[r".*output\\*.+$", r"\.\\\..+$", r".*__.*$"], + clean=clean, ) with handler: @@ -378,7 +386,6 @@ def templates(module_site: str, theme_name: str, filter_value: str): """ module, site_name = split_module_site(module_site) site = get_site(module, site_name) - console = Console() if theme_name: available_themes = get_available_themes(console, site, theme_name) diff --git a/src/render_engine_cli/event.py b/src/render_engine_cli/event.py index 531ba59..b967e75 100644 --- a/src/render_engine_cli/event.py +++ b/src/render_engine_cli/event.py @@ -2,13 +2,12 @@ import time import traceback from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path import watchfiles from rich.console import Console -from render_engine_cli.utils import get_site - -console = Console() +from render_engine_cli.utils import get_site, remove_output_folder def spawn_server(server_address: tuple[str, int], directory: str) -> ThreadingHTTPServer: @@ -59,6 +58,7 @@ def __init__( dirs_to_watch: str | None = None, patterns: list[str] | None = None, ignore_patterns: list[str] | None = None, + clean: bool = False, *args, **kwargs, ) -> None: @@ -70,10 +70,12 @@ def __init__( self.dirs_to_watch = dirs_to_watch self.patterns = patterns self.ignore_patterns = ignore_patterns + self.clean = clean + self.console = Console() def start_server(self) -> None: if not getattr(self, "server", False): - console.print( + self.console.print( f"[bold green]Spawning server on http://{self.server_address[0]}:{self.server_address[1]}[/bold green]" ) self.server = spawn_server(self.server_address, self.output_path) @@ -81,18 +83,20 @@ def start_server(self) -> None: self._thread.start() def stop_server(self) -> None: - console.print("[bold red]Stopping server[/bold red]") + self.console.print("[bold red]Stopping server[/bold red]") self.server.shutdown() self._thread.join() def rebuild(self) -> None: - console.print("[bold purple]Reloading and Rebuilding site...[/bold purple]") + self.console.print("[bold purple]Reloading and Rebuilding site...[/bold purple]") site = get_site(self.import_path, self.site, reload=True) + if self.clean: + remove_output_folder(Path(site.output_path), console=self.console) try: site.render() except Exception: - console.print("[bold red]Failed to render site[/bold red]") - console.print(traceback.format_exc()) + self.console.print("[bold red]Failed to render site[/bold red]") + self.console.print(traceback.format_exc()) pass def stop_watcher(self) -> bool: @@ -122,7 +126,7 @@ def watch(self) -> None: If a KeyboardInterrupt is raised, it stops the observer and server. """ - console.print(f"[yellow]Serving {self.output_path}[/yellow]") + self.console.print(f"[yellow]Serving {self.output_path}[/yellow]") while not self.stop_watcher(): try: if self.dirs_to_watch: @@ -144,5 +148,5 @@ def __exit__(self, exc_type, exc_value, traceback) -> None: """Stopping Context manager for the class""" self.stop_server() - console.print("[bold red]FIN![/bold red]") + self.console.print("[bold red]FIN![/bold red]") return None diff --git a/src/render_engine_cli/utils.py b/src/render_engine_cli/utils.py index 7769b21..9370ecb 100644 --- a/src/render_engine_cli/utils.py +++ b/src/render_engine_cli/utils.py @@ -102,9 +102,11 @@ def get_site_content_paths(site: Site) -> list[Path | str | None]: return list(filter(None, base_paths)) -def remove_output_folder(output_path: Path) -> None: +def remove_output_folder(output_path: Path, console: Console = None) -> None: """Remove the output folder""" + if console: + console.print(f"[bold yellow]Removing exisiting rendered content from {str(output_path)}") # TODO: #778 Should we check for Operating System if output_path.exists(): shutil.rmtree(output_path)