#!/usr/bin/env sh set -eu app_env="$(printf '%s' "${APP_ENV:-development}" | tr '[:upper:]' '[:lower:]')" if [ -z "${VITE_ORGFRONT_PUBLIC_URL:-}" ] && [ -n "${ORGFRONT_URL:-}" ]; then export VITE_ORGFRONT_PUBLIC_URL="$ORGFRONT_URL" fi if [ -z "${VITE_ORGFRONT_PUBLIC_URL:-}" ] && [ -n "${ORGFRONT_CALLBACK_URLS:-}" ]; then first_orgfront_callback="${ORGFRONT_CALLBACK_URLS%%,*}" case "$first_orgfront_callback" in http://*/auth/callback | https://*/auth/callback) export VITE_ORGFRONT_PUBLIC_URL="${first_orgfront_callback%/auth/callback}" ;; esac fi case "$app_env" in production|prod|stage|staging) mode="production" ;; *) mode="development" ;; esac if [ "${1:-}" = "--print-public-url" ]; then printf '%s\n' "${VITE_ORGFRONT_PUBLIC_URL:-}" exit 0 fi if [ "${1:-}" = "--print-mode" ]; then printf '%s\n' "$mode" exit 0 fi ensure_frontend_dependencies() { APP_WORKSPACE_FILTER="../orgfront" # If common workspace exists, manage dependencies from the real workspace tree. if [ -d /workspace/common ] && [ -f /workspace/common/package.json ]; then WORKSPACE_DIR="/workspace/common" LOCK_FILE="/workspace/common/pnpm-lock.yaml" else WORKSPACE_DIR="." LOCK_FILE="package-lock.json" fi if [ ! -f "$WORKSPACE_DIR/package.json" ]; then return 0 fi acquire_install_lock() { lock_file="$WORKSPACE_DIR/.baron-deps-install.lock" if command -v flock >/dev/null 2>&1; then exec 9>"$lock_file" flock 9 return 0 fi while ! mkdir "$lock_file" 2>/dev/null; do sleep 1 done trap 'rmdir "$lock_file" >/dev/null 2>&1 || true' EXIT INT TERM } if command -v sha256sum >/dev/null 2>&1; then deps_hash="$(sha256sum "$WORKSPACE_DIR/package.json" "$LOCK_FILE" 2>/dev/null | sha256sum | awk '{print $1}')" else deps_hash="$(cksum "$WORKSPACE_DIR/package.json" "$LOCK_FILE" 2>/dev/null | cksum | awk '{print $1}')" fi deps_stamp="node_modules/.baron-deps-hash" installed_hash="$(cat "$deps_stamp" 2>/dev/null || true)" if [ "$installed_hash" != "$deps_hash" ]; then echo "Installing frontend dependencies..." acquire_install_lock if [ "$WORKSPACE_DIR" = "/workspace/common" ]; then (cd /workspace/common && pnpm install --filter "${APP_WORKSPACE_FILTER}..." --frozen-lockfile --ignore-scripts) else npm ci fi mkdir -p node_modules printf '%s\n' "$deps_hash" > "$deps_stamp" fi } ensure_frontend_dependencies if [ "$mode" = "production" ]; then echo "Running in production mode with Vite preview..." exec sh -c "npm run build && npm run preview -- --host 0.0.0.0 --port 5175" fi echo "Running in development mode..." exec npm run dev -- --host 0.0.0.0 --port 5175