Prechádzať zdrojové kódy

Export Error Handling + Improved CLI (#1359)

Alek Petuskey 1 rok pred
rodič
commit
be9120b251
1 zmenil súbory, kde vykonal 46 pridanie a 28 odobranie
  1. 46 28
      reflex/utils/build.py

+ 46 - 28
reflex/utils/build.py

@@ -9,10 +9,10 @@ import subprocess
 from pathlib import Path
 from typing import Optional, Union
 
-from rich.progress import Progress
+from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
 
 from reflex import constants
-from reflex.utils import path_ops, prerequisites
+from reflex.utils import console, path_ops, prerequisites
 from reflex.utils.processes import new_process
 
 
@@ -109,36 +109,54 @@ def export_app(
         generate_sitemap(deploy_url)
 
     # Create a progress object
-    progress = Progress()
+    progress = Progress(
+        *Progress.get_default_columns()[:-1],
+        MofNCompleteColumn(),
+        TimeElapsedColumn(),
+    )
+
+    checkpoints = [
+        "Linting and checking ",
+        "Compiled successfully",
+        "Route (pages)",
+        "Collecting page data",
+        "automatically rendered as static HTML",
+        'Copying "static build" directory',
+        'Copying "public" directory',
+        "Finalizing page optimization",
+        "Export successful",
+    ]
 
     # Add a single task to the progress object
-    task = progress.add_task("Building app... ", total=500)
+    task = progress.add_task("Creating Production Build: ", total=len(checkpoints))
 
     # Start the subprocess with the progress bar.
-    with progress, new_process(
-        [prerequisites.get_package_manager(), "run", "export"],
-        cwd=constants.WEB_DIR,
-    ) as export_process:
-        assert export_process.stdout is not None, "No stdout for export process."
-        for line in export_process.stdout:
-            # Print the line in debug mode.
-            if loglevel == constants.LogLevel.DEBUG:
-                print(line, end="")
-
-            # Check for special strings and update the progress bar.
-            if "Linting and checking " in line:
-                progress.update(task, advance=100)
-            elif "Compiled successfully" in line:
-                progress.update(task, advance=100)
-            elif "Route (pages)" in line:
-                progress.update(task, advance=100)
-            elif "automatically rendered as static HTML" in line:
-                progress.update(task, advance=100)
-            elif "Export successful" in line:
-                progress.update(task, completed=500)
-                break  # Exit the loop if the completion message is found
-
-        print("Export process completed.")
+    try:
+        with progress, new_process(
+            [prerequisites.get_package_manager(), "run", "export"],
+            cwd=constants.WEB_DIR,
+        ) as export_process:
+            assert export_process.stdout is not None, "No stdout for export process."
+            for line in export_process.stdout:
+                if loglevel == constants.LogLevel.DEBUG:
+                    print(line, end="")
+
+                # Check for special strings and update the progress bar.
+                for special_string in checkpoints:
+                    if special_string in line:
+                        if special_string == "Export successful":
+                            progress.update(task, completed=len(checkpoints))
+                            break  # Exit the loop if the completion message is found
+                        else:
+                            progress.update(task, advance=1)
+                            break
+
+    except Exception as e:
+        console.print(f"[red]Export process errored: {e}")
+        console.print(
+            "[red]Run in with [bold]--loglevel debug[/bold] to see the full error."
+        )
+        os._exit(1)
 
     # Zip up the app.
     if zip: