Selaa lähdekoodia

allow setting run mode via env, add helpers to determine it (#4168)

benedikt-bartscher 7 kuukautta sitten
vanhempi
säilyke
7560bf6429
4 muutettua tiedostoa jossa 39 lisäystä ja 2 poistoa
  1. 2 0
      reflex/constants/__init__.py
  2. 3 0
      reflex/constants/base.py
  3. 16 2
      reflex/reflex.py
  4. 18 0
      reflex/utils/exec.py

+ 2 - 0
reflex/constants/__init__.py

@@ -2,6 +2,8 @@
 
 from .base import (
     COOKIES,
+    ENV_BACKEND_ONLY_ENV_VAR,
+    ENV_FRONTEND_ONLY_ENV_VAR,
     ENV_MODE_ENV_VAR,
     IS_WINDOWS,
     LOCAL_STORAGE,

+ 3 - 0
reflex/constants/base.py

@@ -226,6 +226,9 @@ SKIP_COMPILE_ENV_VAR = "__REFLEX_SKIP_COMPILE"
 # This env var stores the execution mode of the app
 ENV_MODE_ENV_VAR = "REFLEX_ENV_MODE"
 
+ENV_BACKEND_ONLY_ENV_VAR = "REFLEX_BACKEND_ONLY"
+ENV_FRONTEND_ONLY_ENV_VAR = "REFLEX_FRONTEND_ONLY"
+
 # Testing variables.
 # Testing os env set by pytest when running a test case.
 PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"

+ 16 - 2
reflex/reflex.py

@@ -274,9 +274,17 @@ def run(
         constants.Env.DEV, help="The environment to run the app in."
     ),
     frontend: bool = typer.Option(
-        False, "--frontend-only", help="Execute only frontend."
+        False,
+        "--frontend-only",
+        help="Execute only frontend.",
+        envvar=constants.ENV_FRONTEND_ONLY_ENV_VAR,
+    ),
+    backend: bool = typer.Option(
+        False,
+        "--backend-only",
+        help="Execute only backend.",
+        envvar=constants.ENV_BACKEND_ONLY_ENV_VAR,
     ),
-    backend: bool = typer.Option(False, "--backend-only", help="Execute only backend."),
     frontend_port: str = typer.Option(
         config.frontend_port, help="Specify a different frontend port."
     ),
@@ -291,6 +299,12 @@ def run(
     ),
 ):
     """Run the app in the current directory."""
+    if frontend and backend:
+        console.error("Cannot use both --frontend-only and --backend-only options.")
+        raise typer.Exit(1)
+    os.environ[constants.ENV_BACKEND_ONLY_ENV_VAR] = str(backend).lower()
+    os.environ[constants.ENV_FRONTEND_ONLY_ENV_VAR] = str(frontend).lower()
+
     _run(env, frontend, backend, frontend_port, backend_port, backend_host, loglevel)
 
 

+ 18 - 0
reflex/utils/exec.py

@@ -496,6 +496,24 @@ def is_prod_mode() -> bool:
     return current_mode == constants.Env.PROD.value
 
 
+def is_frontend_only() -> bool:
+    """Check if the app is running in frontend-only mode.
+
+    Returns:
+        True if the app is running in frontend-only mode.
+    """
+    return os.environ.get(constants.ENV_FRONTEND_ONLY_ENV_VAR, "").lower() == "true"
+
+
+def is_backend_only() -> bool:
+    """Check if the app is running in backend-only mode.
+
+    Returns:
+        True if the app is running in backend-only mode.
+    """
+    return os.environ.get(constants.ENV_BACKEND_ONLY_ENV_VAR, "").lower() == "true"
+
+
 def should_skip_compile() -> bool:
     """Whether the app should skip compile.