Files

72 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
# StemEngine startup script.
# Builds the React frontend (full build, then watch mode) and starts the
# FastAPI backend. Both processes run in the same container so that
# .tsx/.ts edits on the host are picked up and served without a Docker rebuild.
set -e
FRONTEND=/app/frontend
BACKEND=/app/backend
# Runtime knobs (preserve previous behavior by default)
STEMENGINE_MODE="${STEMENGINE_MODE:-dev}"
SKIP_NPM_INSTALL="${SKIP_NPM_INSTALL:-0}"
FRONTEND_BUILD="${FRONTEND_BUILD:-1}"
FRONTEND_WATCH="${FRONTEND_WATCH:-1}"
UVICORN_RELOAD="${UVICORN_RELOAD:-1}"
UVICORN_LOG_LEVEL="${UVICORN_LOG_LEVEL:-debug}"
if [[ "$STEMENGINE_MODE" == "test" || "$STEMENGINE_MODE" == "runtime-test" ]]; then
# Resource-safe defaults for runtime checks/tests.
SKIP_NPM_INSTALL="1"
FRONTEND_BUILD="0"
FRONTEND_WATCH="0"
UVICORN_RELOAD="0"
UVICORN_LOG_LEVEL="info"
fi
echo "========================================="
echo " StemEngine startup"
echo "========================================="
# ── Frontend ──────────────────────────────────────────────────────────────────
cd "$FRONTEND"
# Install / sync packages. --prefer-offline uses the local cache first so
# subsequent starts are fast; falls back to the registry if needed.
if [[ "$SKIP_NPM_INSTALL" == "1" ]]; then
echo "[frontend] Skipping npm install (SKIP_NPM_INSTALL=1)"
else
echo "[frontend] Syncing npm packages..."
npm install --prefer-offline --no-audit --no-fund 2>&1 | grep -v "^npm warn" || true
fi
# Initial full build so backend/static is populated before uvicorn starts.
# Subsequent hot-rebuilds are handled by --watch below.
if [[ "$FRONTEND_BUILD" == "1" ]]; then
echo "[frontend] Building..."
npx vite build 2>&1
else
echo "[frontend] Skipping initial build (FRONTEND_BUILD=0)"
fi
if [[ "$FRONTEND_WATCH" == "1" ]]; then
echo "[frontend] Starting watch mode (rebuilds on .tsx/.ts changes)..."
# CHOKIDAR_USEPOLLING=1: WSL2→Docker bind-mounts don't propagate inotify events,
# so chokidar falls back to stat-based polling to detect host-side edits.
CHOKIDAR_USEPOLLING=1 CHOKIDAR_INTERVAL=800 npx vite build --watch 2>&1 &
else
echo "[frontend] Watch mode disabled (FRONTEND_WATCH=0)"
fi
# ── Backend ───────────────────────────────────────────────────────────────────
cd /app
echo "[backend] Starting uvicorn..."
UVICORN_ARGS=(--host 0.0.0.0 --port 8000 --log-level "$UVICORN_LOG_LEVEL")
if [[ "$UVICORN_RELOAD" == "1" ]]; then
UVICORN_ARGS+=(--reload)
fi
exec python3.11 -m uvicorn backend.main:app "${UVICORN_ARGS[@]}"