Skip to content
Merged
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
32 changes: 24 additions & 8 deletions hatch_build.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Custom hatch build hook to build the web frontend."""

import os
import shutil
import subprocess
import sys
from pathlib import Path
Expand All @@ -15,20 +16,35 @@ class WebUIBuildHook(BuildHookInterface):

def initialize(self, version: str, build_data: dict) -> None:
"""Run npm build to compile the frontend."""
# Skip on ReadTheDocs - npm is not available there
if os.environ.get("READTHEDOCS"):
return

root = Path(self.root)
app_dir = root / "app"
output_dir = root / "src" / "experimaestro" / "webui" / "data"

def _handle_missing_webui():
"""Create an empty directory to satisfy the force-include directive"""
# if the web UI is not built.
output_dir.mkdir(parents=True, exist_ok=True)

# Skip on ReadTheDocs - npm is not available there
if os.environ.get("READTHEDOCS"):
_handle_missing_webui()
return

# Skip if output directory already exists and has content
if output_dir.exists() and any(output_dir.iterdir()):
return

if not app_dir.exists():
# No app directory - skip silently (might be an sdist without app)
_handle_missing_webui()
return

if not shutil.which("npm"):
print(
"warning: npm not found. Skipping web UI build.",
file=sys.stderr,
)
_handle_missing_webui()
return

print("Building web frontend...", file=sys.stderr) # noqa: T201
Expand All @@ -41,8 +57,8 @@ def initialize(self, version: str, build_data: dict) -> None:
check=True,
capture_output=True,
)
except subprocess.CalledProcessError as e:
print(f"npm install failed: {e.stderr.decode()}", file=sys.stderr) # noqa: T201
except (subprocess.CalledProcessError, FileNotFoundError) as e:
print(f"npm install failed: {e!s}", file=sys.stderr) # noqa: T201
raise

# Run npm build
Expand All @@ -56,8 +72,8 @@ def initialize(self, version: str, build_data: dict) -> None:
capture_output=True,
env=env,
)
except subprocess.CalledProcessError as e:
print(f"npm build failed: {e.stderr.decode()}", file=sys.stderr) # noqa: T201
except (subprocess.CalledProcessError, FileNotFoundError) as e:
print(f"npm build failed: {e!s}", file=sys.stderr) # noqa: T201
raise

print("Web frontend built successfully.", file=sys.stderr) # noqa: T201