1+ # TODO : add a feature to automatically fix appropriate fastkit output console size
12# --------------------------------------------------------------------------
23# The Module defines main and core CLI operations for FastAPI-fastkit.
34#
1718
1819from fastapi_fastkit .backend .main import (
1920 add_new_route ,
21+ ask_create_project_folder ,
2022 create_venv_with_manager ,
23+ deploy_template_with_folder_option ,
2124 find_template_core_modules ,
2225 generate_dependency_file_with_manager ,
26+ get_deployment_success_message ,
2327 inject_project_metadata ,
2428 install_dependencies_with_manager ,
2529 read_template_stack ,
2630)
27- from fastapi_fastkit .backend .transducer import copy_and_convert_template
2831from fastapi_fastkit .core .exceptions import CLIExceptions
2932from fastapi_fastkit .core .settings import FastkitConfig
3033from fastapi_fastkit .utils .logging import get_logger , setup_logging
@@ -191,6 +194,7 @@ def startdemo(
191194 """
192195 Create a new FastAPI project from templates and inject metadata.
193196 """
197+ # TODO : add --template-name option to specify the template name
194198 settings = ctx .obj ["settings" ]
195199
196200 template_dir = settings .FASTKIT_TEMPLATE_ROOT
@@ -258,25 +262,27 @@ def startdemo(
258262 print_error ("Project creation aborted!" )
259263 return
260264
265+ # Ask user whether to create a new project folder
266+ create_project_folder = ask_create_project_folder (project_name )
267+
261268 try :
262269 user_local = settings .USER_WORKSPACE
263- project_dir = os .path .join (user_local , project_name )
264-
265- click .echo (f"FastAPI template project will deploy at '{ user_local } '" )
266270
267- copy_and_convert_template (target_template , user_local , project_name )
271+ project_dir , _ = deploy_template_with_folder_option (
272+ target_template , user_local , project_name , create_project_folder
273+ )
268274
269275 inject_project_metadata (
270276 project_dir , project_name , author , author_email , description
271277 )
272278
273- # Create virtual environment and install dependencies with selected package manager
274279 venv_path = create_venv_with_manager (project_dir , package_manager )
275280 install_dependencies_with_manager (project_dir , venv_path , package_manager )
276281
277- print_success (
278- f"FastAPI project ' { project_name } ' from ' { template } ' has been created and saved to { user_local } !"
282+ success_message = get_deployment_success_message (
283+ template , project_name , user_local , create_project_folder
279284 )
285+ print_success (success_message )
280286
281287 except Exception as e :
282288 if settings .DEBUG_MODE :
@@ -323,8 +329,11 @@ def init(
323329) -> None :
324330 """
325331 Start a empty FastAPI project setup.
332+
326333 This command will automatically create a new FastAPI project directory and a python virtual environment.
334+
327335 Dependencies will be automatically installed based on the selected stack at venv.
336+
328337 Project metadata will be injected to the project files.
329338 """
330339 settings = ctx .obj ["settings" ]
@@ -402,13 +411,15 @@ def init(
402411 print_error ("Project creation aborted!" )
403412 return
404413
414+ # Ask user whether to create a new project folder
415+ create_project_folder = ask_create_project_folder (project_name )
416+
405417 try :
406418 user_local = settings .USER_WORKSPACE
407- project_dir = os .path .join (user_local , project_name )
408-
409- click .echo (f"FastAPI project will deploy at '{ user_local } '" )
410419
411- copy_and_convert_template (target_template , user_local , project_name )
420+ project_dir , _ = deploy_template_with_folder_option (
421+ target_template , user_local , project_name , create_project_folder
422+ )
412423
413424 inject_project_metadata (
414425 project_dir , project_name , author , author_email , description
@@ -439,9 +450,10 @@ def init(
439450 venv_path = create_venv_with_manager (project_dir , package_manager )
440451 install_dependencies_with_manager (project_dir , venv_path , package_manager )
441452
442- print_success (
443- f"FastAPI project ' { project_name } ' has been created successfully and saved to { user_local } !"
453+ success_message = get_deployment_success_message (
454+ template , project_name , user_local , create_project_folder
444455 )
456+ print_success (success_message )
445457
446458 print_info (
447459 "To start your project, run 'fastkit runserver' at newly created FastAPI project directory"
@@ -457,25 +469,43 @@ def init(
457469
458470
459471@fastkit_cli .command ()
460- @click .argument ("project_name" )
461472@click .argument ("route_name" )
473+ @click .argument ("project_dir" , default = "." )
462474@click .pass_context
463- def addroute (ctx : Context , project_name : str , route_name : str ) -> None :
475+ def addroute (ctx : Context , route_name : str , project_dir : str ) -> None :
464476 """
465477 Add a new route to the FastAPI project.
478+
479+ Examples:\n
480+ fastkit addroute user . # Add 'user' route to current directory
481+ fastkit addroute user my_project # Add 'user' route to 'my_project' in workspace
466482 """
467483 settings = ctx .obj ["settings" ]
468- user_local = settings .USER_WORKSPACE
469- project_dir = os .path .join (user_local , project_name )
484+
485+ if project_dir == "." :
486+ actual_project_dir = os .getcwd ()
487+ project_name = os .path .basename (actual_project_dir )
488+ else :
489+ user_local = settings .USER_WORKSPACE
490+ actual_project_dir = os .path .join (user_local , project_dir )
491+ project_name = project_dir
470492
471493 # Check if project exists
472- if not os .path .exists (project_dir ):
473- print_error (f"Project '{ project_name } ' does not exist in '{ user_local } '." )
494+ if not os .path .exists (actual_project_dir ):
495+ if project_dir == "." :
496+ print_error ("Current directory is not a valid project directory." )
497+ else :
498+ print_error (
499+ f"Project '{ project_dir } ' does not exist in '{ settings .USER_WORKSPACE } '."
500+ )
474501 return
475502
476503 # Verify it's a fastkit project
477- if not is_fastkit_project (project_dir ):
478- print_error (f"'{ project_name } ' is not a FastAPI-fastkit project." )
504+ if not is_fastkit_project (actual_project_dir ):
505+ if project_dir == "." :
506+ print_error ("Current directory is not a FastAPI-fastkit project." )
507+ else :
508+ print_error (f"'{ project_dir } ' is not a FastAPI-fastkit project." )
479509 return
480510
481511 # Validate route name
@@ -499,26 +529,34 @@ def addroute(ctx: Context, project_name: str, route_name: str) -> None:
499529 {
500530 "Project" : project_name ,
501531 "Route Name" : route_name ,
502- "Target Directory" : project_dir ,
532+ "Target Directory" : actual_project_dir ,
503533 },
504534 )
505535 console .print (table )
506536
507- # Confirm before proceeding
508- confirm = click .confirm (
509- f"\n Do you want to add route '{ route_name } ' to project '{ project_name } '?" ,
510- default = True ,
511- )
537+ if project_dir == "." :
538+ confirm_message = (
539+ f"\n Do you want to add route '{ route_name } ' to the current project?"
540+ )
541+ else :
542+ confirm_message = f"\n Do you want to add route '{ route_name } ' to project '{ project_name } '?"
543+
544+ confirm = click .confirm (confirm_message , default = True )
512545 if not confirm :
513546 print_error ("Operation cancelled!" )
514547 return
515548
516549 # Add the new route
517- add_new_route (project_dir , route_name )
550+ add_new_route (actual_project_dir , route_name )
518551
519- print_success (
520- f"Successfully added new route '{ route_name } ' to project `{ project_name } `"
521- )
552+ if project_dir == "." :
553+ print_success (
554+ f"Successfully added new route '{ route_name } ' to the current project!"
555+ )
556+ else :
557+ print_success (
558+ f"Successfully added new route '{ route_name } ' to project '{ project_name } '!"
559+ )
522560
523561 except Exception as e :
524562 logger = get_logger ()
0 commit comments