diff --git a/src/fastapi_new/new.py b/src/fastapi_new/new.py index 7ac32ac..e7a3776 100644 --- a/src/fastapi_new/new.py +++ b/src/fastapi_new/new.py @@ -107,7 +107,7 @@ def _setup(toolkit: RichToolkit, config: ProjectConfig) -> None: if config.path == pathlib.Path.cwd(): init_cmd = ["uv", "init", "--bare"] else: - init_cmd = ["uv", "init", "--bare", config.name] + init_cmd = ["uv", "init", "--bare", str(config.path)] if config.python: init_cmd.extend(["--python", config.python]) @@ -147,10 +147,10 @@ def _write_template_files(toolkit: RichToolkit, config: ProjectConfig) -> None: def new( ctx: typer.Context, - project_name: Annotated[ + project: Annotated[ str | None, typer.Argument( - help="The name of the new FastAPI project. If not provided, initializes in the current directory.", + help="The name/path of the new FastAPI project. If not provided, initializes in the current directory.", ), ] = None, python: Annotated[ @@ -162,15 +162,15 @@ def new( ), ] = None, ) -> None: - if project_name: - name = project_name - path = pathlib.Path.cwd() / project_name + if project: + path = (pathlib.Path.cwd() / project).resolve() else: - name = pathlib.Path.cwd().name path = pathlib.Path.cwd() + current_dir = path == pathlib.Path.cwd() + config = ProjectConfig( - name=name, + name=path.name, path=path, python=python, ) @@ -180,16 +180,16 @@ def new( toolkit.print_line() - if not project_name: + if current_dir: toolkit.print( - f"[yellow]⚠️ No project name provided. Initializing in current directory: {path}[/yellow]", + f"[yellow]⚠️ Initializing in current directory: {config.path}[/yellow]", tag="warning", ) toolkit.print_line() # Check if project directory already exists (only for new subdirectory) - if project_name and config.path.exists(): - _exit_with_error(toolkit, f"Directory '{project_name}' already exists.") + if not current_dir and config.path.exists(): + _exit_with_error(toolkit, f"Directory '{config.name}' already exists.") if shutil.which("uv") is None: _exit_with_error( @@ -210,16 +210,16 @@ def new( toolkit.print_line() # Print success message - if project_name: + if not current_dir: toolkit.print( - f"[bold green]✨ Success![/bold green] Created FastAPI project: [cyan]{project_name}[/cyan]", + f"[bold green]✨ Success![/bold green] Created FastAPI project: [cyan]{config.name}[/cyan]", tag="success", ) toolkit.print_line() toolkit.print("[bold]Next steps:[/bold]") - toolkit.print(f" [dim]$[/dim] cd {project_name}") + toolkit.print(f" [dim]$[/dim] cd {config.name}") toolkit.print(" [dim]$[/dim] uv run fastapi dev") else: toolkit.print( diff --git a/tests/test_new.py b/tests/test_new.py index 5103b70..d3d13da 100644 --- a/tests/test_new.py +++ b/tests/test_new.py @@ -74,11 +74,21 @@ def test_initializes_in_current_directory(temp_project_dir: Path) -> None: result = runner.invoke(app, []) assert result.exit_code == 0 - assert "No project name provided" in result.output assert "Initializing in current directory" in result.output _assert_project_created(temp_project_dir) +def test_initializes_in_current_directory_with_dot(temp_project_dir: Path) -> None: + result = runner.invoke(app, ["."]) + + assert result.exit_code == 0 + assert "Initializing in current directory" in result.output + _assert_project_created(temp_project_dir) + + readme_content = (temp_project_dir / "README.md").read_text() + assert f"# {temp_project_dir.name}" in readme_content + + def test_rejects_existing_directory(temp_project_dir: Path) -> None: existing_dir = temp_project_dir / "existing_project" existing_dir.mkdir()