-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.py
More file actions
122 lines (96 loc) · 4.08 KB
/
Copy pathdev.py
File metadata and controls
122 lines (96 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Dev launcher — starts backend (FastAPI) and frontend (Vite) together.
Usage:
python dev.py
Ctrl+C stops both servers.
"""
import subprocess
import threading
import sys
import os
import signal
# ── Paths ─────────────────────────────────────────────────────────────────────
ROOT = os.path.dirname(os.path.abspath(__file__))
BACKEND = os.path.join(ROOT, "backend")
FRONTEND = os.path.join(ROOT, "frontend")
# Run uvicorn as a Python module to avoid Application Control blocks on .exe files
UVICORN = [sys.executable, "-m", "uvicorn"]
# ── ANSI colour helpers ───────────────────────────────────────────────────────
# Enable VT processing on Windows 10+
if sys.platform == "win32":
import ctypes
ctypes.windll.kernel32.SetConsoleMode(
ctypes.windll.kernel32.GetStdHandle(-11), 7
)
CYAN = "\033[96m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RESET = "\033[0m"
BOLD = "\033[1m"
def tag(label: str, colour: str) -> str:
return f"{colour}{BOLD}[{label}]{RESET} "
def stream_output(proc: subprocess.Popen, label: str, colour: str) -> None:
"""Read a process's stdout/stderr and print with a coloured prefix."""
prefix = tag(label, colour)
for line in iter(proc.stdout.readline, b""):
text = line.decode("utf-8", errors="replace").rstrip()
print(f"{prefix}{text}", flush=True)
# ── Process registry ──────────────────────────────────────────────────────────
processes: list[subprocess.Popen] = []
def shutdown(*_) -> None:
print(f"\n{YELLOW}Shutting down both servers...{RESET}")
for p in processes:
try:
if sys.platform == "win32":
p.terminate()
else:
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
except Exception:
pass
sys.exit(0)
signal.signal(signal.SIGINT, shutdown)
signal.signal(signal.SIGTERM, shutdown)
def start_process(cmd: list[str], cwd: str) -> subprocess.Popen:
kwargs: dict = dict(
cwd = cwd,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
)
if sys.platform != "win32":
kwargs["start_new_session"] = True
return subprocess.Popen(cmd, **kwargs)
# ── Main ──────────────────────────────────────────────────────────────────────
def main() -> None:
print(f"\n{BOLD}Starting Elevra dev servers...{RESET}\n")
# Backend
backend_proc = start_process(
UVICORN + ["app.main:app", "--reload", "--host", "0.0.0.0", "--port", "8000"],
cwd=BACKEND,
)
processes.append(backend_proc)
threading.Thread(
target=stream_output, args=(backend_proc, "BACKEND", CYAN), daemon=True
).start()
print(f"{tag('BACKEND', CYAN)}FastAPI → http://localhost:8000")
# Frontend — npm.cmd on Windows, npm everywhere else
npm = "npm.cmd" if sys.platform == "win32" else "npm"
frontend_proc = start_process(
[npm, "run", "dev"],
cwd=FRONTEND,
)
processes.append(frontend_proc)
threading.Thread(
target=stream_output, args=(frontend_proc, "FRONTEND", GREEN), daemon=True
).start()
print(f"{tag('FRONTEND', GREEN)}Vite → http://localhost:5173")
print(f"\n{YELLOW}Press Ctrl+C to stop both servers.{RESET}\n")
# Wait for either process to exit (crash / manual stop)
while True:
for p in processes:
if p.poll() is not None:
label = "BACKEND" if p is backend_proc else "FRONTEND"
print(f"\n{YELLOW}{label} exited (code {p.returncode}). Stopping all.{RESET}")
shutdown()
threading.Event().wait(1)
if __name__ == "__main__":
main()