Add yarn dev command from #7183 to manage.py

This commit is contained in:
thatmattlove 2022-02-22 15:37:41 -07:00
parent c725696d41
commit de0f4157ce
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,22 @@
import typing as t
import threading
from django.core.management import call_command
from django.contrib.staticfiles.management.commands import runserver
class Command(runserver.Command):
"""Run the NetBox development server.
Builds UI source files, runs `collectstatic`, starts the Django development web server, and
watches UI source files for changes.
"""
def handle(self, *args: t.Any, **options: t.Any) -> t.Optional[str]:
# Run the UI development server in a separate thread so file watching doesn't block the
# Django development server running in the main thread.
ui_dev = threading.Thread(name="uidev", target=lambda: call_command("uidev"))
ui_dev.start()
# Run `collectstatic`.
call_command("collectstatic", interactive=False)
# Run the original `runserver` command.
return super().handle(*args, **options)

View File

@ -0,0 +1,37 @@
import shutil
import subprocess
import typing as t
from pathlib import Path
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
"""Provide access to the underlying UI development server."""
def handle(self, *args: t.Any, **options: t.Any) -> t.Optional[str]:
"""Run the `npm run dev` command via a separate process and write the output."""
project_root = Path(__file__).parent.parent.parent.parent
project_static = project_root / "project-static"
npm = shutil.which("npm")
npm_args = tuple(str(i) for i in (npm, "--prefix", project_static, "run", "--silent", "dev"))
try:
self.stdout.write(self.style.SUCCESS("Watching UI files..."))
proc = subprocess.Popen(
args=npm_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=project_static,
)
while True:
out = proc.stdout.readline().decode()
if out == '' and proc.poll() is not None:
break
if out:
self.stdout.write(out.strip())
except BaseException as exc:
proc.kill()
raise CommandError("Error while running command '{}'".format(" ".join(npm_args))) from exc