Update commands to automatically run pnpm for relevant commands

Signed-off-by: Tobias O <tobias.olenyi@tum.de>
This commit is contained in:
Tobias O
2025-10-02 15:51:23 +13:00
committed by Tobias O
parent be5ff369e0
commit 2fa0be52ef
2 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,81 @@
"""
Custom runserver command that automatically starts CSS watcher.
Overrides Django's default runserver to include pnpm dev.
"""
import signal
import subprocess
import sys
from django.conf import settings
from django.contrib.staticfiles.management.commands.runserver import (
Command as RunserverCommand,
)
class Command(RunserverCommand):
help = "Run development server with automatic CSS building"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.css_process = None
def handle(self, *args, **options):
"""Start CSS watcher before running Django dev server."""
self.stdout.write(self.style.SUCCESS("Starting CSS watcher (pnpm dev)..."))
# Start pnpm dev in background
try:
self.css_process = subprocess.Popen(
["pnpm", "run", "dev"],
cwd=settings.BASE_DIR,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
self.stdout.write(self.style.SUCCESS("✓ CSS watcher started\n"))
except FileNotFoundError:
self.stdout.write(
self.style.WARNING(
"Warning: pnpm not found. CSS will not be rebuilt automatically.\n"
'Install pnpm or run "pnpm run dev" manually in another terminal.\n'
)
)
except Exception as e:
self.stdout.write(
self.style.WARNING(f"Warning: Could not start CSS watcher: {e}\n")
)
# Register cleanup handler
original_sigint = signal.getsignal(signal.SIGINT)
original_sigterm = signal.getsignal(signal.SIGTERM)
def cleanup(signum, frame):
self.stdout.write("\nShutting down...")
if self.css_process:
self.css_process.terminate()
try:
self.css_process.wait(timeout=5)
self.stdout.write(self.style.SUCCESS("✓ CSS watcher stopped"))
except subprocess.TimeoutExpired:
self.css_process.kill()
# Call original handler
if signum == signal.SIGINT and callable(original_sigint):
original_sigint(signum, frame)
elif signum == signal.SIGTERM and callable(original_sigterm):
original_sigterm(signum, frame)
sys.exit(0)
signal.signal(signal.SIGINT, cleanup)
signal.signal(signal.SIGTERM, cleanup)
# Run Django dev server
try:
super().handle(*args, **options)
finally:
# Cleanup on normal exit
if self.css_process:
self.css_process.terminate()
try:
self.css_process.wait(timeout=5)
except subprocess.TimeoutExpired:
self.css_process.kill()