Skip to content
Merged
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
53 changes: 39 additions & 14 deletions fletx/cli/commands/newproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def add_arguments(self, parser: CommandParser) -> None:
help="Template to use for the project (default: project)",
)
parser.add_argument(
"-D",
"--directory",
help="Directory where the project should be created (default: current directory)",
"directory",
nargs="?",
help="Directory where the project should be created. If not provided, uses current directory. Use '.' to scaffold in current directory without creating a project subdirectory.",
)
parser.add_argument("--author", help="Author name for the project")
parser.add_argument("-d", "--description", help="Project description")
Expand All @@ -60,6 +60,11 @@ def add_arguments(self, parser: CommandParser) -> None:
action="store_true",
help="Don't install dependencies after creating the project",
)
parser.add_argument(
"--no-project-dir",
action="store_true",
help="Generate project files in the target directory instead of creating a subdirectory with the project name. Also implied when directory is '.'.",
)

def handle(self, **kwargs) -> None:
"""Handle the new project command."""
Expand All @@ -78,13 +83,22 @@ def handle(self, **kwargs) -> None:
self.validate_name(name)

# Determine target directory
if directory:
target_dir = Path(directory) / name
no_project_dir = kwargs.get("no_project_dir", False)
# Special case: if directory is ".", treat as no-project-dir (scaffold in current directory)
if directory == ".":
no_project_dir = True
target_dir = Path.cwd()
else:
target_dir = Path.cwd() / name

# Check if project directory already exists
if target_dir.exists() and not overwrite:
if no_project_dir:
target_dir = Path(directory) if directory else Path.cwd()
else:
if directory:
target_dir = Path(directory) / name
else:
target_dir = Path.cwd() / name

# Check if project directory already exists (unless we are scaffolding in place)
if not no_project_dir and target_dir.exists() and not overwrite:
if any(target_dir.iterdir()):
raise CommandExecutionError(
f"Directory '{target_dir}' already exists and is not empty. "
Expand Down Expand Up @@ -120,7 +134,12 @@ def handle(self, **kwargs) -> None:
template, target_dir, context, overwrite
)

print(f"\nProject '{name}' created successfully at: {target_dir}")
if no_project_dir:
print(
f"\nProject '{name}' created successfully in the current directory: {target_dir}"
)
else:
print(f"\nProject '{name}' created successfully at: {target_dir}")

# Create project configuration
self._create_project_config(target_dir, context)
Expand All @@ -130,8 +149,7 @@ def handle(self, **kwargs) -> None:
self._install_dependencies(target_dir)

# Print next steps
self._print_next_steps(name, target_dir, no_install)

self._print_next_steps(name, target_dir, no_install, no_project_dir)
except Exception as e:
raise CommandExecutionError(f"Failed to create project: {e}")

Expand Down Expand Up @@ -187,15 +205,22 @@ def _install_dependencies(self, project_dir: Path) -> None:
)

def _print_next_steps(
self, project_name: str, project_dir: Path, no_install: bool
self,
project_name: str,
project_dir: Path,
no_install: bool,
no_project_dir: bool = False,
) -> None:
"""Print next steps for the user."""

print("\n" + "=" * 50)
print("🎉 Project created successfully!")
print("=" * 50)
print("\nNext steps:")
print(" 1. cd {project_dir.name}")
if no_project_dir:
print(" 1. The files have been generated in the current directory.")
else:
print(f" 1. cd {project_dir.name}")

if no_install:
print(" 2. pip install -r requirements.txt")
Expand Down
Loading