Fix pc.py frontend setup (#1219)
This commit is contained in:
parent
d7aeb753d5
commit
9330999eb7
@ -129,26 +129,36 @@ def run(
|
|||||||
prerequisites.check_admin_settings()
|
prerequisites.check_admin_settings()
|
||||||
|
|
||||||
# Get the frontend and backend commands, based on the environment.
|
# Get the frontend and backend commands, based on the environment.
|
||||||
frontend_cmd = backend_cmd = None
|
setup_frontend = frontend_cmd = backend_cmd = None
|
||||||
if env == constants.Env.DEV:
|
if env == constants.Env.DEV:
|
||||||
frontend_cmd, backend_cmd = exec.run_frontend, exec.run_backend
|
setup_frontend, frontend_cmd, backend_cmd = (
|
||||||
|
build.setup_frontend,
|
||||||
|
exec.run_frontend,
|
||||||
|
exec.run_backend,
|
||||||
|
)
|
||||||
if env == constants.Env.PROD:
|
if env == constants.Env.PROD:
|
||||||
frontend_cmd, backend_cmd = exec.run_frontend_prod, exec.run_backend_prod
|
setup_frontend, frontend_cmd, backend_cmd = (
|
||||||
assert frontend_cmd and backend_cmd, "Invalid env"
|
build.setup_frontend_prod,
|
||||||
|
exec.run_frontend_prod,
|
||||||
|
exec.run_backend_prod,
|
||||||
|
)
|
||||||
|
assert setup_frontend and frontend_cmd and backend_cmd, "Invalid env"
|
||||||
|
|
||||||
# Post a telemetry event.
|
# Post a telemetry event.
|
||||||
telemetry.send(f"run-{env.value}", get_config().telemetry_enabled)
|
telemetry.send(f"run-{env.value}", get_config().telemetry_enabled)
|
||||||
|
|
||||||
# Run the frontend and backend.
|
# Run the frontend and backend.
|
||||||
|
if frontend:
|
||||||
|
setup_frontend(Path.cwd(), loglevel)
|
||||||
|
threading.Thread(
|
||||||
|
target=frontend_cmd, args=(Path.cwd(), frontend_port, loglevel)
|
||||||
|
).start()
|
||||||
if backend:
|
if backend:
|
||||||
|
build.setup_backend()
|
||||||
threading.Thread(
|
threading.Thread(
|
||||||
target=backend_cmd,
|
target=backend_cmd,
|
||||||
args=(app.__name__, backend_host, backend_port, loglevel),
|
args=(app.__name__, backend_host, backend_port, loglevel),
|
||||||
).start()
|
).start()
|
||||||
if frontend:
|
|
||||||
threading.Thread(
|
|
||||||
target=frontend_cmd, args=(app.app, Path.cwd(), frontend_port)
|
|
||||||
).start()
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@ -217,9 +227,7 @@ def export(
|
|||||||
if frontend:
|
if frontend:
|
||||||
build.setup_frontend(Path.cwd())
|
build.setup_frontend(Path.cwd())
|
||||||
|
|
||||||
app = prerequisites.get_app().app
|
|
||||||
build.export_app(
|
build.export_app(
|
||||||
app,
|
|
||||||
backend=backend,
|
backend=backend,
|
||||||
frontend=frontend,
|
frontend=frontend,
|
||||||
zip=zipping,
|
zip=zipping,
|
||||||
|
@ -7,7 +7,7 @@ import os
|
|||||||
import random
|
import random
|
||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING, Optional, Union
|
from typing import Optional, Union
|
||||||
|
|
||||||
from rich.progress import Progress
|
from rich.progress import Progress
|
||||||
|
|
||||||
@ -15,9 +15,6 @@ from pynecone import constants
|
|||||||
from pynecone.config import get_config
|
from pynecone.config import get_config
|
||||||
from pynecone.utils import path_ops, prerequisites
|
from pynecone.utils import path_ops, prerequisites
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from pynecone.app import App
|
|
||||||
|
|
||||||
|
|
||||||
def update_json_file(file_path: str, update_dict: dict[str, Union[int, str]]):
|
def update_json_file(file_path: str, update_dict: dict[str, Union[int, str]]):
|
||||||
"""Update the contents of a json file.
|
"""Update the contents of a json file.
|
||||||
@ -91,7 +88,6 @@ def generate_sitemap(deploy_url: str):
|
|||||||
|
|
||||||
|
|
||||||
def export_app(
|
def export_app(
|
||||||
app: App,
|
|
||||||
backend: bool = True,
|
backend: bool = True,
|
||||||
frontend: bool = True,
|
frontend: bool = True,
|
||||||
zip: bool = False,
|
zip: bool = False,
|
||||||
@ -101,7 +97,6 @@ def export_app(
|
|||||||
"""Zip up the app for deployment.
|
"""Zip up the app for deployment.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
app: The app.
|
|
||||||
backend: Whether to zip up the backend app.
|
backend: Whether to zip up the backend app.
|
||||||
frontend: Whether to zip up the frontend app.
|
frontend: Whether to zip up the frontend app.
|
||||||
zip: Whether to zip the app.
|
zip: Whether to zip the app.
|
||||||
@ -121,20 +116,22 @@ def export_app(
|
|||||||
# Add a single task to the progress object
|
# Add a single task to the progress object
|
||||||
task = progress.add_task("Building app... ", total=500)
|
task = progress.add_task("Building app... ", total=500)
|
||||||
|
|
||||||
# Start the progress bar
|
# Start the subprocess with the progress bar.
|
||||||
with progress:
|
with progress, subprocess.Popen(
|
||||||
# Run the subprocess command
|
|
||||||
export_process = subprocess.Popen(
|
|
||||||
[prerequisites.get_package_manager(), "run", "export"],
|
[prerequisites.get_package_manager(), "run", "export"],
|
||||||
cwd=constants.WEB_DIR,
|
cwd=constants.WEB_DIR,
|
||||||
env=os.environ,
|
env=os.environ,
|
||||||
stderr=subprocess.STDOUT,
|
stderr=subprocess.STDOUT,
|
||||||
stdout=subprocess.PIPE, # Redirect stdout to a pipe
|
stdout=subprocess.PIPE, # Redirect stdout to a pipe
|
||||||
universal_newlines=True, # Set universal_newlines to True for text mode
|
universal_newlines=True, # Set universal_newlines to True for text mode
|
||||||
)
|
) 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="")
|
||||||
|
|
||||||
if export_process.stdout:
|
# Check for special strings and update the progress bar.
|
||||||
for line in iter(export_process.stdout.readline, ""):
|
|
||||||
if "Linting and checking " in line:
|
if "Linting and checking " in line:
|
||||||
progress.update(task, advance=100)
|
progress.update(task, advance=100)
|
||||||
elif "Compiled successfully" in line:
|
elif "Compiled successfully" in line:
|
||||||
@ -144,14 +141,9 @@ def export_app(
|
|||||||
elif "automatically rendered as static HTML" in line:
|
elif "automatically rendered as static HTML" in line:
|
||||||
progress.update(task, advance=100)
|
progress.update(task, advance=100)
|
||||||
elif "Export successful" in line:
|
elif "Export successful" in line:
|
||||||
print("DOOE")
|
|
||||||
progress.update(task, completed=500)
|
progress.update(task, completed=500)
|
||||||
break # Exit the loop if the completion message is found
|
break # Exit the loop if the completion message is found
|
||||||
elif loglevel == constants.LogLevel.DEBUG:
|
|
||||||
print(line, end="")
|
|
||||||
|
|
||||||
# Wait for the subprocess to complete
|
|
||||||
export_process.wait()
|
|
||||||
print("Export process completed.")
|
print("Export process completed.")
|
||||||
|
|
||||||
# Zip up the app.
|
# Zip up the app.
|
||||||
@ -194,13 +186,21 @@ def posix_export(backend: bool = True, frontend: bool = True):
|
|||||||
os.system(cmd)
|
os.system(cmd)
|
||||||
|
|
||||||
|
|
||||||
def setup_frontend(root: Path, disable_telemetry: bool = True):
|
def setup_frontend(
|
||||||
|
root: Path,
|
||||||
|
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
|
||||||
|
disable_telemetry: bool = True,
|
||||||
|
):
|
||||||
"""Set up the frontend.
|
"""Set up the frontend.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
root: root path of the project.
|
root: The root path of the project.
|
||||||
|
loglevel: The log level to use.
|
||||||
disable_telemetry: Whether to disable the Next telemetry.
|
disable_telemetry: Whether to disable the Next telemetry.
|
||||||
"""
|
"""
|
||||||
|
# Validate bun version.
|
||||||
|
prerequisites.validate_and_install_bun(initialize=False)
|
||||||
|
|
||||||
# Initialize the web directory if it doesn't exist.
|
# Initialize the web directory if it doesn't exist.
|
||||||
web_dir = prerequisites.create_web_directory(root)
|
web_dir = prerequisites.create_web_directory(root)
|
||||||
|
|
||||||
@ -233,6 +233,22 @@ def setup_frontend(root: Path, disable_telemetry: bool = True):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def setup_frontend_prod(
|
||||||
|
root: Path,
|
||||||
|
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
|
||||||
|
disable_telemetry: bool = True,
|
||||||
|
):
|
||||||
|
"""Set up the frontend for prod mode.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
root: The root path of the project.
|
||||||
|
loglevel: The log level to use.
|
||||||
|
disable_telemetry: Whether to disable the Next telemetry.
|
||||||
|
"""
|
||||||
|
setup_frontend(root, loglevel, disable_telemetry)
|
||||||
|
export_app(loglevel=loglevel)
|
||||||
|
|
||||||
|
|
||||||
def setup_backend():
|
def setup_backend():
|
||||||
"""Set up backend.
|
"""Set up backend.
|
||||||
|
|
||||||
|
@ -7,19 +7,14 @@ import platform
|
|||||||
import subprocess
|
import subprocess
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
from rich import print
|
from rich import print
|
||||||
|
|
||||||
from pynecone import constants
|
from pynecone import constants
|
||||||
from pynecone.config import get_config
|
from pynecone.config import get_config
|
||||||
from pynecone.utils import console, prerequisites, processes
|
from pynecone.utils import console, prerequisites, processes
|
||||||
from pynecone.utils.build import export_app, setup_backend, setup_frontend
|
|
||||||
from pynecone.utils.watch import AssetFolderWatch
|
from pynecone.utils.watch import AssetFolderWatch
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from pynecone.app import App
|
|
||||||
|
|
||||||
|
|
||||||
def start_watching_assets_folder(root):
|
def start_watching_assets_folder(root):
|
||||||
"""Start watching assets folder.
|
"""Start watching assets folder.
|
||||||
@ -33,14 +28,12 @@ def start_watching_assets_folder(root):
|
|||||||
|
|
||||||
def run_process_and_launch_url(
|
def run_process_and_launch_url(
|
||||||
run_command: list[str],
|
run_command: list[str],
|
||||||
root: Path,
|
|
||||||
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
|
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
|
||||||
):
|
):
|
||||||
"""Run the process and launch the URL.
|
"""Run the process and launch the URL.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
run_command: The command to run.
|
run_command: The command to run.
|
||||||
root: root path of the project.
|
|
||||||
loglevel: The log level to use.
|
loglevel: The log level to use.
|
||||||
"""
|
"""
|
||||||
process = subprocess.Popen(
|
process = subprocess.Popen(
|
||||||
@ -72,7 +65,6 @@ def run_process_and_launch_url(
|
|||||||
|
|
||||||
|
|
||||||
def run_frontend(
|
def run_frontend(
|
||||||
app: App,
|
|
||||||
root: Path,
|
root: Path,
|
||||||
port: str,
|
port: str,
|
||||||
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
|
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
|
||||||
@ -80,17 +72,10 @@ def run_frontend(
|
|||||||
"""Run the frontend.
|
"""Run the frontend.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
app: The app.
|
root: The root path of the project.
|
||||||
root: root path of the project.
|
port: The port to run the frontend on.
|
||||||
port: port of the app.
|
|
||||||
loglevel: The log level to use.
|
loglevel: The log level to use.
|
||||||
"""
|
"""
|
||||||
# validate bun version
|
|
||||||
prerequisites.validate_and_install_bun(initialize=False)
|
|
||||||
|
|
||||||
# Set up the frontend.
|
|
||||||
setup_frontend(root)
|
|
||||||
|
|
||||||
# Start watching asset folder.
|
# Start watching asset folder.
|
||||||
start_watching_assets_folder(root)
|
start_watching_assets_folder(root)
|
||||||
|
|
||||||
@ -98,12 +83,11 @@ def run_frontend(
|
|||||||
console.rule("[bold green]App Running")
|
console.rule("[bold green]App Running")
|
||||||
os.environ["PORT"] = get_config().frontend_port if port is None else port
|
os.environ["PORT"] = get_config().frontend_port if port is None else port
|
||||||
run_process_and_launch_url(
|
run_process_and_launch_url(
|
||||||
[prerequisites.get_package_manager(), "run", "dev"], root, loglevel
|
[prerequisites.get_package_manager(), "run", "dev"], loglevel
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def run_frontend_prod(
|
def run_frontend_prod(
|
||||||
app: App,
|
|
||||||
root: Path,
|
root: Path,
|
||||||
port: str,
|
port: str,
|
||||||
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
|
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
|
||||||
@ -111,24 +95,17 @@ def run_frontend_prod(
|
|||||||
"""Run the frontend.
|
"""Run the frontend.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
app: The app.
|
root: The root path of the project (to keep same API as run_frontend).
|
||||||
root: root path of the project.
|
port: The port to run the frontend on.
|
||||||
port: port of the app.
|
|
||||||
loglevel: The log level to use.
|
loglevel: The log level to use.
|
||||||
"""
|
"""
|
||||||
# Set up the frontend.
|
|
||||||
setup_frontend(root)
|
|
||||||
|
|
||||||
# Export the app.
|
|
||||||
export_app(app, loglevel=loglevel)
|
|
||||||
|
|
||||||
# Set the port.
|
# Set the port.
|
||||||
os.environ["PORT"] = get_config().frontend_port if port is None else port
|
os.environ["PORT"] = get_config().frontend_port if port is None else port
|
||||||
|
|
||||||
# Run the frontend in production mode.
|
# Run the frontend in production mode.
|
||||||
console.rule("[bold green]App Running")
|
console.rule("[bold green]App Running")
|
||||||
run_process_and_launch_url(
|
run_process_and_launch_url(
|
||||||
[prerequisites.get_package_manager(), "run", "prod"], root, loglevel
|
[prerequisites.get_package_manager(), "run", "prod"], loglevel
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -146,8 +123,6 @@ def run_backend(
|
|||||||
port: The app port
|
port: The app port
|
||||||
loglevel: The log level.
|
loglevel: The log level.
|
||||||
"""
|
"""
|
||||||
setup_backend()
|
|
||||||
|
|
||||||
cmd = [
|
cmd = [
|
||||||
"uvicorn",
|
"uvicorn",
|
||||||
f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}",
|
f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}",
|
||||||
@ -181,8 +156,6 @@ def run_backend_prod(
|
|||||||
port: The app port
|
port: The app port
|
||||||
loglevel: The log level.
|
loglevel: The log level.
|
||||||
"""
|
"""
|
||||||
setup_backend()
|
|
||||||
|
|
||||||
num_workers = processes.get_num_workers()
|
num_workers = processes.get_num_workers()
|
||||||
command = (
|
command = (
|
||||||
[
|
[
|
||||||
|
Loading…
Reference in New Issue
Block a user