Reflex run automatically inits when needed (#3011)

This commit is contained in:
Nikhil Rao 2024-04-04 14:34:12 -07:00 committed by GitHub
parent ef5c5bea2b
commit 44d6c997dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 21 deletions

View File

@ -165,7 +165,8 @@ def _run(
_skip_compile() _skip_compile()
# Check that the app is initialized. # Check that the app is initialized.
prerequisites.check_initialized(frontend=frontend) if prerequisites.needs_reinit(frontend=frontend):
_init(name=config.app_name, loglevel=loglevel)
# If something is running on the ports, ask the user if they want to kill or change it. # If something is running on the ports, ask the user if they want to kill or change it.
if frontend and processes.is_process_on_port(frontend_port): if frontend and processes.is_process_on_port(frontend_port):
@ -294,6 +295,10 @@ def export(
): ):
"""Export the app to a zip file.""" """Export the app to a zip file."""
from reflex.utils import export as export_utils from reflex.utils import export as export_utils
from reflex.utils import prerequisites
if prerequisites.needs_reinit(frontend=True):
_init(name=config.app_name, loglevel=loglevel)
export_utils.export( export_utils.export(
zipping=zipping, zipping=zipping,
@ -529,7 +534,8 @@ def deploy(
dependency.check_requirements() dependency.check_requirements()
# Check if we are set up. # Check if we are set up.
prerequisites.check_initialized(frontend=True) if prerequisites.needs_reinit(frontend=True):
_init(name=config.app_name, loglevel=loglevel)
prerequisites.check_latest_package_version(constants.ReflexHostingCLI.MODULE_NAME) prerequisites.check_latest_package_version(constants.ReflexHostingCLI.MODULE_NAME)
hosting_cli.deploy( hosting_cli.deploy(

View File

@ -46,9 +46,6 @@ def export(
# Show system info # Show system info
exec.output_system_info() exec.output_system_info()
# Check that the app is initialized.
prerequisites.check_initialized(frontend=frontend)
# Compile the app in production mode and export it. # Compile the app in production mode and export it.
console.rule("[bold]Compiling production app and preparing for export.") console.rule("[bold]Compiling production app and preparing for export.")

View File

@ -802,38 +802,38 @@ def install_frontend_packages(packages: set[str], config: Config):
) )
def check_initialized(frontend: bool = True): def needs_reinit(frontend: bool = True) -> bool:
"""Check that the app is initialized. """Check if an app needs to be reinitialized.
Args: Args:
frontend: Whether to check if the frontend is initialized. frontend: Whether to check if the frontend is initialized.
Returns:
Whether the app needs to be reinitialized.
Raises: Raises:
Exit: If the app is not initialized. Exit: If the app is not initialized.
""" """
has_config = os.path.exists(constants.Config.FILE) if not os.path.exists(constants.Config.FILE):
has_reflex_dir = not frontend or os.path.exists(constants.Reflex.DIR)
has_web_dir = not frontend or os.path.exists(constants.Dirs.WEB)
# Check if the app is initialized.
if not (has_config and has_reflex_dir and has_web_dir):
console.error( console.error(
f"The app is not initialized. Run [bold]{constants.Reflex.MODULE_NAME} init[/bold] first." f"{constants.Config.FILE} not found. Run [bold]{constants.Reflex.MODULE_NAME} init[/bold] first."
) )
raise typer.Exit(1) raise typer.Exit(1)
# Check that the template is up to date. # Make sure the .reflex directory exists.
if frontend and not is_latest_template(): if not os.path.exists(constants.Reflex.DIR):
console.error( return True
"The base app template has updated. Run [bold]reflex init[/bold] again."
) # Make sure the .web directory exists in frontend mode.
raise typer.Exit(1) if frontend and not os.path.exists(constants.Dirs.WEB):
return True
# Print a warning for Windows users.
if constants.IS_WINDOWS: if constants.IS_WINDOWS:
console.warn( console.warn(
"""Windows Subsystem for Linux (WSL) is recommended for improving initial install times.""" """Windows Subsystem for Linux (WSL) is recommended for improving initial install times."""
) )
# No need to reinitialize if the app is already initialized.
return False
def is_latest_template() -> bool: def is_latest_template() -> bool: