|
| 1 | +import typer |
| 2 | +import shutil |
| 3 | +from pathlib import Path |
| 4 | +import subprocess |
| 5 | +import importlib.resources as resources |
| 6 | +import os |
| 7 | + |
| 8 | +frontend = typer.Typer(help="Manage Django apps.") |
| 9 | + |
| 10 | + |
| 11 | +@frontend.command("create") |
| 12 | +def add_frontend( |
| 13 | + project_name: str, |
| 14 | + directory: Path = Path("."), |
| 15 | +): |
| 16 | + """ |
| 17 | + Create a new Django app inside an existing project using bundled templates. |
| 18 | + """ |
| 19 | + project_path = directory / project_name |
| 20 | + name = "frontend" |
| 21 | + if not project_path.exists() or not project_path.is_dir(): |
| 22 | + typer.echo(f"❌ Project '{project_name}' not found at {project_path}", err=True) |
| 23 | + raise typer.Exit(code=1) |
| 24 | + # Destination for new app |
| 25 | + app_path = project_path / name |
| 26 | + if app_path.exists(): |
| 27 | + typer.echo( |
| 28 | + f"❌ App '{name}' already exists in project '{project_name}'", err=True |
| 29 | + ) |
| 30 | + raise typer.Exit(code=1) |
| 31 | + typer.echo(f"📦 Creating app '{name}' in project '{project_name}'") |
| 32 | + # Locate the Django app template directory in package resources |
| 33 | + with resources.path( |
| 34 | + "django_mongodb_cli.templates", "frontend_template" |
| 35 | + ) as template_path: |
| 36 | + cmd = [ |
| 37 | + "django-admin", |
| 38 | + "startapp", |
| 39 | + "--template", |
| 40 | + str(template_path), |
| 41 | + name, |
| 42 | + directory, |
| 43 | + ] |
| 44 | + subprocess.run(cmd, check=True) |
| 45 | + |
| 46 | + |
| 47 | +@frontend.command("remove") |
| 48 | +def remove_frontend(project_name: str, directory: Path = Path(".")): |
| 49 | + """ |
| 50 | + Remove a Django app from a project. |
| 51 | + """ |
| 52 | + name = "frontend" |
| 53 | + target = directory / project_name / name |
| 54 | + if target.exists() and target.is_dir(): |
| 55 | + shutil.rmtree(target) |
| 56 | + typer.echo(f"🗑️ Removed app '{name}' from project '{project_name}'") |
| 57 | + else: |
| 58 | + typer.echo(f"❌ App '{name}' does not exist in '{project_name}'", err=True) |
| 59 | + |
| 60 | + |
| 61 | +@frontend.command("install") |
| 62 | +def install_npm( |
| 63 | + project_name: str, |
| 64 | + frontend_dir: str = "frontend", |
| 65 | + directory: Path = Path("."), |
| 66 | + clean: bool = typer.Option( |
| 67 | + False, |
| 68 | + "--clean", |
| 69 | + help="Remove node_modules and package-lock.json before installing", |
| 70 | + ), |
| 71 | +): |
| 72 | + """ |
| 73 | + Install npm dependencies in the frontend directory. |
| 74 | + """ |
| 75 | + project_path = directory / project_name |
| 76 | + if not project_path.exists(): |
| 77 | + typer.echo( |
| 78 | + f"❌ Project '{project_name}' does not exist at {project_path}", err=True |
| 79 | + ) |
| 80 | + raise typer.Exit(code=1) |
| 81 | + |
| 82 | + frontend_path = project_path / frontend_dir |
| 83 | + if not frontend_path.exists(): |
| 84 | + typer.echo( |
| 85 | + f"❌ Frontend directory '{frontend_dir}' not found at {frontend_path}", |
| 86 | + err=True, |
| 87 | + ) |
| 88 | + raise typer.Exit(code=1) |
| 89 | + |
| 90 | + package_json = frontend_path / "package.json" |
| 91 | + if not package_json.exists(): |
| 92 | + typer.echo(f"❌ package.json not found in {frontend_path}", err=True) |
| 93 | + raise typer.Exit(code=1) |
| 94 | + |
| 95 | + if clean: |
| 96 | + typer.echo(f"🧹 Cleaning node_modules and package-lock.json in {frontend_path}") |
| 97 | + node_modules = frontend_path / "node_modules" |
| 98 | + package_lock = frontend_path / "package-lock.json" |
| 99 | + |
| 100 | + if node_modules.exists(): |
| 101 | + shutil.rmtree(node_modules) |
| 102 | + typer.echo(" ✓ Removed node_modules") |
| 103 | + |
| 104 | + if package_lock.exists(): |
| 105 | + package_lock.unlink() |
| 106 | + typer.echo(" ✓ Removed package-lock.json") |
| 107 | + |
| 108 | + typer.echo(f"📦 Installing npm dependencies in {frontend_path}") |
| 109 | + |
| 110 | + try: |
| 111 | + subprocess.run(["npm", "install"], cwd=frontend_path, check=True) |
| 112 | + typer.echo("✅ Dependencies installed successfully") |
| 113 | + except subprocess.CalledProcessError as e: |
| 114 | + typer.echo(f"❌ npm install failed with exit code {e.returncode}", err=True) |
| 115 | + raise typer.Exit(code=e.returncode) |
| 116 | + except FileNotFoundError: |
| 117 | + typer.echo( |
| 118 | + "❌ npm not found. Please ensure Node.js and npm are installed.", err=True |
| 119 | + ) |
| 120 | + raise typer.Exit(code=1) |
| 121 | + |
| 122 | + |
| 123 | +@frontend.command("run") |
| 124 | +def run_npm( |
| 125 | + project_name: str, |
| 126 | + frontend_dir: str = "frontend", |
| 127 | + directory: Path = Path("."), |
| 128 | + script: str = typer.Option("watch", help="NPM script to run (default: watch)"), |
| 129 | +): |
| 130 | + """ |
| 131 | + Run npm script in the frontend directory. |
| 132 | + """ |
| 133 | + project_path = directory / project_name |
| 134 | + if not project_path.exists(): |
| 135 | + typer.echo( |
| 136 | + f"❌ Project '{project_name}' does not exist at {project_path}", err=True |
| 137 | + ) |
| 138 | + raise typer.Exit(code=1) |
| 139 | + |
| 140 | + frontend_path = project_path / frontend_dir |
| 141 | + if not frontend_path.exists(): |
| 142 | + typer.echo( |
| 143 | + f"❌ Frontend directory '{frontend_dir}' not found at {frontend_path}", |
| 144 | + err=True, |
| 145 | + ) |
| 146 | + raise typer.Exit(code=1) |
| 147 | + |
| 148 | + package_json = frontend_path / "package.json" |
| 149 | + if not package_json.exists(): |
| 150 | + typer.echo(f"❌ package.json not found in {frontend_path}", err=True) |
| 151 | + raise typer.Exit(code=1) |
| 152 | + |
| 153 | + typer.echo(f"🚀 Running 'npm run {script}' in {frontend_path}") |
| 154 | + |
| 155 | + try: |
| 156 | + subprocess.run(["npm", "run", script], cwd=frontend_path, check=True) |
| 157 | + except subprocess.CalledProcessError as e: |
| 158 | + typer.echo(f"❌ npm command failed with exit code {e.returncode}", err=True) |
| 159 | + raise typer.Exit(code=e.returncode) |
| 160 | + except FileNotFoundError: |
| 161 | + typer.echo( |
| 162 | + "❌ npm not found. Please ensure Node.js and npm are installed.", err=True |
| 163 | + ) |
| 164 | + raise typer.Exit(code=1) |
| 165 | + |
| 166 | + |
| 167 | +def django_admin_cmd(project_name: str, directory: Path, *args: str): |
| 168 | + """ |
| 169 | + Internal helper to run `django-admin` with project's DJANGO_SETTINGS_MODULE. |
| 170 | + """ |
| 171 | + project_path = directory / project_name |
| 172 | + if not project_path.exists(): |
| 173 | + typer.echo( |
| 174 | + f"❌ Project '{project_name}' does not exist at {project_path}", err=True |
| 175 | + ) |
| 176 | + raise typer.Exit(code=1) |
| 177 | + parent_dir = project_path.parent.resolve() |
| 178 | + env = os.environ.copy() |
| 179 | + env["DJANGO_SETTINGS_MODULE"] = f"{project_name}.settings.base" |
| 180 | + env["PYTHONPATH"] = str(parent_dir) + os.pathsep + env.get("PYTHONPATH", "") |
| 181 | + subprocess.run(["django-admin", *args], cwd=parent_dir, env=env, check=True) |
0 commit comments