Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import logging
import os
import shutil
import signal
from rich.console import Console
from rich.logging import RichHandler
Expand Down Expand Up @@ -42,6 +43,22 @@
MAGMA_PATH = "./plugins/magma"


def _resolve_npm():
"""Resolve the full path to the npm executable.

On Windows, ``npm`` is a batch script (``npm.cmd``) that
``subprocess`` cannot find without ``shell=True``. Using
``shutil.which`` resolves the correct path on every platform.
Comment on lines +47 to +51
"""
npm = shutil.which("npm")
if npm is None:
raise FileNotFoundError(
"npm is not installed or not on PATH. "
"Install Node.js (https://nodejs.org/) and ensure npm is available."
)
return npm
Comment on lines +53 to +59


def setup_logger(level=logging.DEBUG):
format = "%(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
Expand Down Expand Up @@ -187,9 +204,18 @@ async def enable_cors(request, response):


async def start_vue_dev_server():
proc = await asyncio.create_subprocess_exec(
"npm", "run", "dev", stdout=sys.stdout, stderr=sys.stderr, cwd=MAGMA_PATH
)
npm = _resolve_npm()
if sys.platform == "win32":
# On Windows shutil.which resolves to npm.cmd; batch files cannot
# be executed directly by create_subprocess_exec, so use shell.
cmd = subprocess.list2cmdline([npm, "run", "dev"])
proc = await asyncio.create_subprocess_shell(
cmd, stdout=sys.stdout, stderr=sys.stderr, cwd=MAGMA_PATH,
Comment on lines +210 to +213
)
else:
proc = await asyncio.create_subprocess_exec(
npm, "run", "dev", stdout=sys.stdout, stderr=sys.stderr, cwd=MAGMA_PATH,
)
logging.info("VueJS development server started (PID %s).", proc.pid)


Expand Down Expand Up @@ -314,7 +340,9 @@ def _get_size_mb(config_key, default):
if args.uiDevHost:
if not os.path.exists(f"{MAGMA_PATH}/dist") and (os.path.exists(f"{MAGMA_PATH}") and len(os.listdir(MAGMA_PATH)) > 0):
logging.info("Building VueJS front-end.")
subprocess.run(["npm", "run", "build"], cwd=MAGMA_PATH, check=True)
npm = _resolve_npm()
subprocess.run([npm, "run", "build"], cwd=MAGMA_PATH, check=True,
shell=(sys.platform == "win32"))
Comment on lines +344 to +345
logging.info("VueJS front-end build complete.")
else:
logging.warning(
Expand All @@ -328,8 +356,11 @@ def _get_size_mb(config_key, default):
if args.build:
if os.path.exists(f"{MAGMA_PATH}") and len(os.listdir(MAGMA_PATH)) > 0:
logging.info("Building VueJS front-end.")
subprocess.run(["npm", "install"], cwd=MAGMA_PATH, check=True)
subprocess.run(["npm", "run", "build"], cwd=MAGMA_PATH, check=True)
npm = _resolve_npm()
subprocess.run([npm, "install"], cwd=MAGMA_PATH, check=True,
shell=(sys.platform == "win32"))
subprocess.run([npm, "run", "build"], cwd=MAGMA_PATH, check=True,
shell=(sys.platform == "win32"))
Comment on lines +360 to +363
logging.info("VueJS front-end build complete.")
else:
logging.warning(
Expand Down
Loading