From 7304351a66fad5e34cc3ef4df0293da3475a0e28 Mon Sep 17 00:00:00 2001 From: Nikhil Rao Date: Fri, 28 Jul 2023 16:29:50 -0700 Subject: [PATCH] Improve prerequisites check (#1454) --- reflex/reflex.py | 62 +++++++++++------------------------ reflex/utils/prerequisites.py | 35 +++++++++++++++++--- 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/reflex/reflex.py b/reflex/reflex.py index d8937c2cc..6cbbfb270 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -1,7 +1,6 @@ """Reflex CLI to create, run, and deploy apps.""" import os -import platform import signal import threading from pathlib import Path @@ -66,13 +65,14 @@ def init( prerequisites.migrate_to_reflex() # Set up the app directory, only if the config doesn't exist. + config = get_config() if not os.path.exists(constants.CONFIG_FILE): prerequisites.create_config(app_name) prerequisites.initialize_app_directory(app_name, template) build.set_reflex_project_hash() - telemetry.send("init", get_config().telemetry_enabled) + telemetry.send("init", config.telemetry_enabled) else: - telemetry.send("reinit", get_config().telemetry_enabled) + telemetry.send("reinit", config.telemetry_enabled) # Initialize the .gitignore. prerequisites.initialize_gitignore() @@ -98,10 +98,9 @@ def run( backend_host: str = typer.Option(None, help="Specify the backend host."), ): """Run the app in the current directory.""" - if platform.system() == "Windows": - console.print( - "[yellow][WARNING] We strongly advise using Windows Subsystem for Linux (WSL) for optimal performance with reflex." - ) + # Check that the app is initialized. + prerequisites.check_initialized(frontend=frontend) + # Set ports as os env variables to take precedence over config and # .env variables(if override_os_envs flag in config is set to False). build.set_os_env( @@ -110,11 +109,11 @@ def run( backend_host=backend_host, ) - frontend_port = ( - get_config().frontend_port if frontend_port is None else frontend_port - ) - backend_port = get_config().backend_port if backend_port is None else backend_port - backend_host = get_config().backend_host if backend_host is None else backend_host + # Get the ports from the config. + config = get_config() + frontend_port = config.frontend_port if frontend_port is None else frontend_port + backend_port = config.backend_port if backend_port is None else backend_port + backend_host = config.backend_host if backend_host is None else backend_host # If no --frontend-only and no --backend-only, then turn on frontend and backend both if not frontend and not backend: @@ -128,20 +127,6 @@ def run( if backend and processes.is_process_on_port(backend_port): backend_port = processes.change_or_terminate_port(backend_port, "backend") - # Check that the app is initialized. - if frontend and not prerequisites.is_initialized(): - console.print( - "[red]The app is not initialized. Run [bold]reflex init[/bold] first." - ) - raise typer.Exit() - - # Check that the template is up to date. - if frontend and not prerequisites.is_latest_template(): - console.print( - "[red]The base app template has updated. Run [bold]reflex init[/bold] again." - ) - raise typer.Exit() - # Get the app module. console.rule("[bold]Starting Reflex App") app = prerequisites.get_app() @@ -169,7 +154,7 @@ def run( assert setup_frontend and frontend_cmd and backend_cmd, "Invalid env" # Post a telemetry event. - telemetry.send(f"run-{env.value}", get_config().telemetry_enabled) + telemetry.send(f"run-{env.value}", config.telemetry_enabled) # Run the frontend and backend. if frontend: @@ -192,7 +177,6 @@ def deploy(dry_run: bool = typer.Option(False, help="Whether to run a dry run.") """Deploy the app to the Reflex hosting service.""" # Get the app config. config = get_config() - config.api_url = prerequisites.get_production_backend_url() # Check if the deploy url is set. if config.rxdeploy_url is None: @@ -201,7 +185,7 @@ def deploy(dry_run: bool = typer.Option(False, help="Whether to run a dry run.") # Compile the app in production mode. typer.echo("Compiling production app") - export(for_reflex_deploy=True) + export() # Exit early if this is a dry run. if dry_run: @@ -233,28 +217,22 @@ def export( backend: bool = typer.Option( True, "--frontend-only", help="Export only frontend.", show_default=False ), - for_reflex_deploy: bool = typer.Option( - False, - "--for-reflex-deploy", - help="Whether export the app for Reflex Deploy Service.", - ), ): """Export the app to a zip file.""" - config = get_config() - - if for_reflex_deploy: - # Get the app config and modify the api_url base on username and app_name. - config.api_url = prerequisites.get_production_backend_url() + # Check that the app is initialized. + prerequisites.check_initialized(frontend=frontend) # Compile the app in production mode and export it. console.rule("[bold]Compiling production app and preparing for export.") if frontend: - # ensure module can be imported and app.compile() is called + # Ensure module can be imported and app.compile() is called. prerequisites.get_app() - # set up .web directory and install frontend dependencies + # Set up .web directory and install frontend dependencies. build.setup_frontend(Path.cwd()) + # Export the app. + config = get_config() build.export_app( backend=backend, frontend=frontend, @@ -263,7 +241,7 @@ def export( ) # Post a telemetry event. - telemetry.send("export", get_config().telemetry_enabled) + telemetry.send("export", config.telemetry_enabled) if zipping: console.rule( diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index cdb87ce1e..a69c47449 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -364,13 +364,38 @@ def install_frontend_packages(): ) -def is_initialized() -> bool: - """Check whether the app is initialized. +def check_initialized(frontend: bool = True): + """Check that the app is initialized. - Returns: - Whether the app is initialized in the current directory. + Args: + frontend: Whether to check if the frontend is initialized. + + Raises: + Exit: If the app is not initialized. """ - return os.path.exists(constants.CONFIG_FILE) and os.path.exists(constants.WEB_DIR) + has_config = os.path.exists(constants.CONFIG_FILE) + has_reflex_dir = IS_WINDOWS or os.path.exists(constants.REFLEX_DIR) + has_web_dir = not frontend or os.path.exists(constants.WEB_DIR) + + # Check if the app is initialized. + if not (has_config and has_reflex_dir and has_web_dir): + console.print( + f"[red]The app is not initialized. Run [bold]{constants.MODULE_NAME} init[/bold] first." + ) + raise typer.Exit() + + # Check that the template is up to date. + if frontend and not is_latest_template(): + console.print( + "[red]The base app template has updated. Run [bold]reflex init[/bold] again." + ) + raise typer.Exit() + + # Print a warning for Windows users. + if IS_WINDOWS: + console.print( + "[yellow][WARNING] We strongly advise using Windows Subsystem for Linux (WSL) for optimal performance with reflex." + ) def is_latest_template() -> bool: