Skip to content

Commit e86ae6e

Browse files
authored
feat(newproject): add support for '.' directory and --no-project-dir flag to scaffold in current directory like django-admin startproject
feat(newproject): add support for '.' directory and --no-project-dir flag to scaffold in current directory like django-admin startproject
2 parents 751e47d + 2623bdb commit e86ae6e

1 file changed

Lines changed: 39 additions & 14 deletions

File tree

fletx/cli/commands/newproject.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def add_arguments(self, parser: CommandParser) -> None:
3131
help="Template to use for the project (default: project)",
3232
)
3333
parser.add_argument(
34-
"-D",
35-
"--directory",
36-
help="Directory where the project should be created (default: current directory)",
34+
"directory",
35+
nargs="?",
36+
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.",
3737
)
3838
parser.add_argument("--author", help="Author name for the project")
3939
parser.add_argument("-d", "--description", help="Project description")
@@ -60,6 +60,11 @@ def add_arguments(self, parser: CommandParser) -> None:
6060
action="store_true",
6161
help="Don't install dependencies after creating the project",
6262
)
63+
parser.add_argument(
64+
"--no-project-dir",
65+
action="store_true",
66+
help="Generate project files in the target directory instead of creating a subdirectory with the project name. Also implied when directory is '.'.",
67+
)
6368

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

8085
# Determine target directory
81-
if directory:
82-
target_dir = Path(directory) / name
86+
no_project_dir = kwargs.get("no_project_dir", False)
87+
# Special case: if directory is ".", treat as no-project-dir (scaffold in current directory)
88+
if directory == ".":
89+
no_project_dir = True
90+
target_dir = Path.cwd()
8391
else:
84-
target_dir = Path.cwd() / name
85-
86-
# Check if project directory already exists
87-
if target_dir.exists() and not overwrite:
92+
if no_project_dir:
93+
target_dir = Path(directory) if directory else Path.cwd()
94+
else:
95+
if directory:
96+
target_dir = Path(directory) / name
97+
else:
98+
target_dir = Path.cwd() / name
99+
100+
# Check if project directory already exists (unless we are scaffolding in place)
101+
if not no_project_dir and target_dir.exists() and not overwrite:
88102
if any(target_dir.iterdir()):
89103
raise CommandExecutionError(
90104
f"Directory '{target_dir}' already exists and is not empty. "
@@ -120,7 +134,12 @@ def handle(self, **kwargs) -> None:
120134
template, target_dir, context, overwrite
121135
)
122136

123-
print(f"\nProject '{name}' created successfully at: {target_dir}")
137+
if no_project_dir:
138+
print(
139+
f"\nProject '{name}' created successfully in the current directory: {target_dir}"
140+
)
141+
else:
142+
print(f"\nProject '{name}' created successfully at: {target_dir}")
124143

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

132151
# Print next steps
133-
self._print_next_steps(name, target_dir, no_install)
134-
152+
self._print_next_steps(name, target_dir, no_install, no_project_dir)
135153
except Exception as e:
136154
raise CommandExecutionError(f"Failed to create project: {e}")
137155

@@ -187,15 +205,22 @@ def _install_dependencies(self, project_dir: Path) -> None:
187205
)
188206

189207
def _print_next_steps(
190-
self, project_name: str, project_dir: Path, no_install: bool
208+
self,
209+
project_name: str,
210+
project_dir: Path,
211+
no_install: bool,
212+
no_project_dir: bool = False,
191213
) -> None:
192214
"""Print next steps for the user."""
193215

194216
print("\n" + "=" * 50)
195217
print("🎉 Project created successfully!")
196218
print("=" * 50)
197219
print("\nNext steps:")
198-
print(" 1. cd {project_dir.name}")
220+
if no_project_dir:
221+
print(" 1. The files have been generated in the current directory.")
222+
else:
223+
print(f" 1. cd {project_dir.name}")
199224

200225
if no_install:
201226
print(" 2. pip install -r requirements.txt")

0 commit comments

Comments
 (0)