|
| 1 | +"""Frontend CLI commands for myfy.""" |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +import typer |
| 6 | +from rich.console import Console |
| 7 | + |
| 8 | +# Check if frontend module is available |
| 9 | +try: |
| 10 | + from myfy.core.config import load_settings |
| 11 | + from myfy.frontend.config import FrontendSettings |
| 12 | + from myfy.frontend.scaffold import check_frontend_initialized, scaffold_frontend |
| 13 | + |
| 14 | + HAS_FRONTEND = True |
| 15 | +except ImportError: |
| 16 | + HAS_FRONTEND = False |
| 17 | + |
| 18 | +frontend_app = typer.Typer(help="Frontend development commands") |
| 19 | +console = Console() |
| 20 | + |
| 21 | + |
| 22 | +def _show_missing_module_error() -> None: |
| 23 | + """Display error message when frontend module is not installed.""" |
| 24 | + console.print("[red]✗ Frontend module not installed[/red]") |
| 25 | + console.print("") |
| 26 | + console.print("The myfy-frontend package is required for this command.") |
| 27 | + console.print("") |
| 28 | + console.print("[green]Install it with:[/green]") |
| 29 | + console.print(" pip install myfy-frontend") |
| 30 | + console.print("") |
| 31 | + console.print("[green]Or install all optional modules:[/green]") |
| 32 | + console.print(" pip install myfy[all]") |
| 33 | + |
| 34 | + |
| 35 | +def _prompt_interactive_config( |
| 36 | + settings: "FrontendSettings", |
| 37 | + templates_dir: str | None, |
| 38 | + static_dir: str | None, |
| 39 | +) -> tuple[str, str]: |
| 40 | + """Prompt user for configuration in interactive mode.""" |
| 41 | + console.print("[bold cyan]🎨 Frontend Initialization[/bold cyan]") |
| 42 | + console.print("") |
| 43 | + |
| 44 | + # Prompt for templates directory |
| 45 | + if templates_dir is None: |
| 46 | + templates_dir = typer.prompt( |
| 47 | + "Templates directory", |
| 48 | + default=settings.templates_dir, |
| 49 | + show_default=True, |
| 50 | + ) |
| 51 | + |
| 52 | + # Prompt for static directory |
| 53 | + if static_dir is None: |
| 54 | + static_dir = typer.prompt( |
| 55 | + "Static files directory", |
| 56 | + default=settings.static_dir, |
| 57 | + show_default=True, |
| 58 | + ) |
| 59 | + |
| 60 | + # Show summary and confirm |
| 61 | + console.print("") |
| 62 | + console.print("[bold]Configuration:[/bold]") |
| 63 | + console.print(f" Templates: {templates_dir}") |
| 64 | + console.print(f" Static: {static_dir}") |
| 65 | + console.print("") |
| 66 | + |
| 67 | + if not typer.confirm("Proceed with initialization?", default=True): |
| 68 | + console.print("Cancelled.") |
| 69 | + raise typer.Exit(0) |
| 70 | + |
| 71 | + console.print("") |
| 72 | + return templates_dir, static_dir |
| 73 | + |
| 74 | + |
| 75 | +def _check_already_initialized(templates_dir: str, interactive: bool) -> None: |
| 76 | + """Check if frontend is already initialized and prompt if needed.""" |
| 77 | + if check_frontend_initialized(templates_dir): |
| 78 | + console.print("[yellow]⚠️ Frontend appears to be already initialized.[/yellow]") |
| 79 | + console.print(f"Found existing files in: {templates_dir}") |
| 80 | + console.print("") |
| 81 | + |
| 82 | + if interactive or typer.confirm("Continue anyway?", default=False): |
| 83 | + console.print("") |
| 84 | + else: |
| 85 | + console.print("Cancelled.") |
| 86 | + raise typer.Exit(0) |
| 87 | + |
| 88 | + |
| 89 | +def _show_success_message() -> None: |
| 90 | + """Display success message and next steps.""" |
| 91 | + console.print("") |
| 92 | + console.print("[green]✨ Frontend initialized successfully![/green]") |
| 93 | + console.print("") |
| 94 | + console.print("[bold]Next steps:[/bold]") |
| 95 | + console.print(" 1. Add FrontendModule to your app:") |
| 96 | + console.print("") |
| 97 | + console.print(" [dim]from myfy.frontend import FrontendModule[/dim]") |
| 98 | + console.print(" [dim]app.add_module(FrontendModule())[/dim]") |
| 99 | + console.print("") |
| 100 | + console.print(" 2. Run development server:") |
| 101 | + console.print("") |
| 102 | + console.print(" [dim]myfy run[/dim]") |
| 103 | + console.print("") |
| 104 | + console.print(" 3. Edit templates in [cyan]frontend/templates/[/cyan]") |
| 105 | + console.print("") |
| 106 | + |
| 107 | + |
| 108 | +@frontend_app.command(name="init") |
| 109 | +def init( |
| 110 | + interactive: bool = typer.Option( |
| 111 | + False, |
| 112 | + "--interactive", |
| 113 | + "-i", |
| 114 | + help="Interactive mode with prompts for configuration", |
| 115 | + ), |
| 116 | + templates_dir: str | None = typer.Option( |
| 117 | + None, |
| 118 | + "--templates-dir", |
| 119 | + help="Templates directory path (default: frontend/templates)", |
| 120 | + ), |
| 121 | + static_dir: str | None = typer.Option( |
| 122 | + None, |
| 123 | + "--static-dir", |
| 124 | + help="Static files directory path (default: frontend/static)", |
| 125 | + ), |
| 126 | +) -> None: |
| 127 | + """ |
| 128 | + Initialize frontend structure with Vite, Tailwind 4, and DaisyUI 5. |
| 129 | +
|
| 130 | + Creates: |
| 131 | + - package.json (Vite + Tailwind + DaisyUI) |
| 132 | + - vite.config.js |
| 133 | + - frontend/templates/ (Jinja2 templates) |
| 134 | + - frontend/css/ (Tailwind styles) |
| 135 | + - frontend/js/ (JavaScript modules) |
| 136 | + - .gitignore |
| 137 | +
|
| 138 | + Examples: |
| 139 | + myfy frontend init # Use defaults |
| 140 | + myfy frontend init -i # Interactive mode |
| 141 | + myfy frontend init --templates-dir my/templates |
| 142 | + """ |
| 143 | + # Check if frontend module is installed |
| 144 | + if not HAS_FRONTEND: |
| 145 | + _show_missing_module_error() |
| 146 | + sys.exit(1) |
| 147 | + |
| 148 | + # Load settings (respects MYFY_FRONTEND_* env vars per ADR-0002) |
| 149 | + settings = load_settings(FrontendSettings) |
| 150 | + |
| 151 | + # Interactive mode: prompt for configuration |
| 152 | + if interactive: |
| 153 | + templates_dir, static_dir = _prompt_interactive_config(settings, templates_dir, static_dir) |
| 154 | + |
| 155 | + # Use defaults from settings if not provided |
| 156 | + templates_dir = templates_dir or settings.templates_dir |
| 157 | + static_dir = static_dir or settings.static_dir |
| 158 | + |
| 159 | + # Check if already initialized |
| 160 | + _check_already_initialized(templates_dir, interactive) |
| 161 | + |
| 162 | + # Run scaffolding |
| 163 | + console.print("[cyan]🎨 Initializing myfy frontend...[/cyan]") |
| 164 | + console.print("") |
| 165 | + |
| 166 | + try: |
| 167 | + scaffold_frontend(_templates_dir=templates_dir, _static_dir=static_dir) |
| 168 | + _show_success_message() |
| 169 | + except Exception as e: |
| 170 | + console.print(f"[red]✗ Error initializing frontend: {e}[/red]") |
| 171 | + sys.exit(1) |
0 commit comments