Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/fastapi_new/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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[
Expand All @@ -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,
)
Expand All @@ -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(
Expand All @@ -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(
Expand Down
12 changes: 11 additions & 1 deletion tests/test_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down