#!/bin/sh set -eu repo_path="${1:-$(pwd)}" socket_host="${TOOLKIT_SOCKET_HOST:-0.0.0.0}" socket_port="${TOOLKIT_SOCKET_PORT:-5174}" socket_cors_origin="${TOOLKIT_SOCKET_CORS_ORIGIN:-http://127.0.0.1:5173,http://localhost:5173,http://bone-kingdom.tabitha.tealthrone}" pid_file="$repo_path/.toolkit-socket.pid" log_directory="$repo_path/logs" log_file="$log_directory/toolkit-socket.log" find_executable() { executable_name="$1" for executable_path in \ "$executable_name" \ "/opt/homebrew/bin/$executable_name" \ "/usr/local/bin/$executable_name" \ "/usr/bin/$executable_name" \ "$HOME/.nvm/versions/node"/*"/bin/$executable_name"; do if command -v "$executable_path" >/dev/null 2>&1; then command -v "$executable_path" return 0 fi if [ -x "$executable_path" ]; then printf '%s\n' "$executable_path" return 0 fi done return 1 } cd "$repo_path" node_bin="$(find_executable node || true)" npm_bin="$(find_executable npm || true)" if [ -z "$node_bin" ]; then echo "[deploy:toolkit] node was not found. Install Node or add it to a standard deploy PATH." exit 1 fi if [ ! -f package.json ] || [ ! -f socket/server.mjs ]; then echo "[deploy:toolkit] $repo_path is not a Bone Kingdom repo checkout." exit 1 fi mkdir -p "$log_directory" \ "$repo_path/src/assets/images/uploads" \ "$repo_path/src/assets/music/uploads" if [ ! -d "$repo_path/node_modules/socket.io" ]; then if [ -z "$npm_bin" ]; then echo "[deploy:toolkit] npm was not found and dependencies are missing." exit 1 fi echo "[deploy:toolkit] node_modules missing socket.io; installing workspace dependencies." "$npm_bin" install --no-audit --no-fund fi if [ -f "$pid_file" ]; then existing_pid="$(cat "$pid_file")" if [ -n "$existing_pid" ] && kill -0 "$existing_pid" 2>/dev/null; then echo "[deploy:toolkit] stopping existing socket process $existing_pid" kill -TERM "$existing_pid" sleep 1 fi fi echo "[deploy:toolkit] starting socket on $socket_host:$socket_port" TOOLKIT_SOCKET_HOST="$socket_host" \ TOOLKIT_SOCKET_PORT="$socket_port" \ TOOLKIT_SOCKET_CORS_ORIGIN="$socket_cors_origin" \ nohup "$node_bin" socket/server.mjs >"$log_file" 2>&1 & socket_pid="$!" printf '%s\n' "$socket_pid" > "$pid_file" sleep 1 if ! kill -0 "$socket_pid" 2>/dev/null; then echo "[deploy:toolkit] socket failed to start; recent log output follows." tail -n 80 "$log_file" || true exit 1 fi if command -v curl >/dev/null 2>&1; then socket_ready="false" for _ in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do if curl -fsS "http://127.0.0.1:$socket_port/health" >/dev/null; then socket_ready="true" break fi sleep 1 done if [ "$socket_ready" != "true" ]; then echo "[deploy:toolkit] socket did not become healthy; recent log output follows." tail -n 80 "$log_file" || true exit 1 fi fi echo "[deploy:toolkit] socket running as $socket_pid"