#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Sync JS dependencies across the workspace via shared script.
sync_js_deps() {
  # Allow user to skip for speed
  if [ "${FC_SYNC_JS_SKIP:-0}" = "1" ]; then
    return 0
  fi
  if command -v node >/dev/null 2>&1; then
    if [ -n "${CI:-}" ]; then
      node "$ROOT_DIR/scripts/js/sync_pnpm.mjs" --frozen
    else
      node "$ROOT_DIR/scripts/js/sync_pnpm.mjs" --no-frozen
    fi
  else
    echo "[run_web] Node.js not found; skipping dependency sync. Set FC_SYNC_JS_SKIP=1 to silence." >&2
    if [ -n "${CI:-}" ]; then
      echo "[run_web] CI requires Node for dependency sync." >&2
      return 1
    fi
  fi
}

# Ensure JS deps are in sync before running the web checks
if ! sync_js_deps; then
log_test_fail "$name"
return 1
fi
