prospector/scripts/lib.sh
Natalie c8eb57108f feat(install): tray app + MCP step + detached run; fix local prod token proxy
./run install now also builds the Prospector.app menu-bar tray (~/Applications:
❖ Open/Stop/Quit, AppleScript-ObjC + sips/iconutil, no Swift/deps) and registers
the prospector MCP in Claude (Desktop + global, non-invasive). New ./run stop and
./run tray; SKIP_DB/SKIP_TRAY/SKIP_MCP escape hatches.

app.sh: --detach (background, for the tray), reuse an already-running instance
instead of colliding, fail loudly if a process we started dies, pidfile in .run/.

Fix the local front door: a prod build calls /prospector/* (api.ts API_BASE), so
vite preview now proxies /prospector (passthrough) while dev keeps /api (rewrite),
both injecting the bearer token. find_psql handles keg-only postgresql@N.

Verified end-to-end: ./run install (deps/env/DB+5 migrations/build/tray/MCP),
./run app serves markets through the token proxy (200; 401 without token), tray
launches resident, web tsc + 160 backend tests green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-29 08:49:53 -04:00

54 lines
1.8 KiB
Bash
Executable file

#!/usr/bin/env bash
# Shared helpers for the prospector local scripts (install / migrate / app).
# Sourced, not executed. Provides: logging, env loading, psql locating.
# Colors (best effort; degrade to empty on dumb terminals).
RED=$'\e[31m' YELLOW=$'\e[33m' BLUE=$'\e[34m' GREEN=$'\e[32m' NC=$'\e[0m' || true
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
info() { echo -e "${BLUE}==>${NC} $*"; }
ok() { echo -e "${GREEN}${NC} $*"; }
warn() { echo -e "${YELLOW}⚠️ ${NC} $*" >&2; }
die() { echo -e "${RED}error:${NC} $*" >&2; exit 1; }
# Load KEY=VALUE pairs from .env.local (if present) into the environment.
load_env() {
local f="$REPO_ROOT/.env.local"
if [[ -f "$f" ]]; then
set -a; . "$f"; set +a
fi
}
# Locate a usable psql, honoring $PSQL, then PATH, then common macOS installs.
# Echoes the path; empty if none found.
find_psql() {
if [[ -n "${PSQL:-}" && -x "${PSQL}" ]]; then echo "$PSQL"; return; fi
if command -v psql >/dev/null 2>&1; then command -v psql; return; fi
local c
for c in \
/opt/homebrew/bin/psql \
/opt/homebrew/opt/postgresql@*/bin/psql \
/usr/local/bin/psql \
/usr/local/opt/postgresql@*/bin/psql \
/Applications/Postgres.app/Contents/Versions/latest/bin/psql \
/Library/PostgreSQL/*/bin/psql; do
[[ -x "$c" ]] && { echo "$c"; return; }
done
echo ""
}
# Build a psql connection string from PROSPECTOR_DB_* env (defaults to a local
# database named in PROSPECTOR_DB_NAME). Pass the target database as $1.
pg_uri() {
local db="$1"
local host="${PROSPECTOR_DB_HOST:-127.0.0.1}"
local port="${PROSPECTOR_DB_PORT:-5432}"
local user="${PROSPECTOR_DB_USER:-$USER}"
local pass="${PROSPECTOR_DB_PASSWORD:-}"
if [[ -n "$pass" ]]; then
echo "postgresql://${user}:${pass}@${host}:${port}/${db}"
else
echo "postgresql://${user}@${host}:${port}/${db}"
fi
}