Add workspace management and isolated worktree environments

This commit is contained in:
Jiayuan Zhang 2026-03-23 18:12:11 +08:00
parent e9555b8a22
commit 81e64e9fce
32 changed files with 1462 additions and 200 deletions

View file

@ -6,6 +6,24 @@ set -euo pipefail
# Usage: bash scripts/check.sh
# ==========================================================================
ENV_FILE="${ENV_FILE:-$(if [ -f .env ]; then echo .env; elif [ -f .env.worktree ]; then echo .env.worktree; else echo .env; fi)}"
if [ -f "$ENV_FILE" ]; then
set -a
# shellcheck disable=SC1090
. "$ENV_FILE"
set +a
fi
POSTGRES_DB="${POSTGRES_DB:-multica}"
POSTGRES_USER="${POSTGRES_USER:-multica}"
POSTGRES_PORT="${POSTGRES_PORT:-5432}"
PORT="${PORT:-8080}"
FRONTEND_PORT="${FRONTEND_PORT:-3000}"
PLAYWRIGHT_BASE_URL="${PLAYWRIGHT_BASE_URL:-http://localhost:${FRONTEND_PORT}}"
export PLAYWRIGHT_BASE_URL
COMPOSE_CMD=(docker compose --env-file "$ENV_FILE")
BACKEND_PID=""
FRONTEND_PID=""
STARTED_BACKEND=false
@ -57,13 +75,14 @@ wait_for_port() {
# --------------------------------------------------------------------------
# Step 0: Ensure DB
# --------------------------------------------------------------------------
echo "==> Using env file: $ENV_FILE"
echo "==> Checking PostgreSQL..."
if pg_isready -h localhost -p 5432 -U multica > /dev/null 2>&1; then
if pg_isready -h localhost -p "$POSTGRES_PORT" -U "$POSTGRES_USER" -d "$POSTGRES_DB" > /dev/null 2>&1; then
echo " Already running."
else
echo " Starting via docker compose..."
docker compose up -d
until docker compose exec -T postgres pg_isready -U multica > /dev/null 2>&1; do
"${COMPOSE_CMD[@]}" up -d
until "${COMPOSE_CMD[@]}" exec -T postgres pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" > /dev/null 2>&1; do
sleep 1
done
echo " PostgreSQL ready."
@ -96,24 +115,24 @@ echo "==> [3/5] Go tests..."
echo ""
echo "==> [4/5] Starting services for E2E..."
if curl -sf http://localhost:8080/health > /dev/null 2>&1; then
echo " Backend already running on :8080"
if curl -sf "http://localhost:${PORT}/health" > /dev/null 2>&1; then
echo " Backend already running on :$PORT"
else
echo " Starting backend..."
(cd server && go run ./cmd/server) > /tmp/multica-check-backend.log 2>&1 &
BACKEND_PID=$!
STARTED_BACKEND=true
wait_for_port 8080 "Backend" 90 "/health"
wait_for_port "$PORT" "Backend" 90 "/health"
fi
if curl -sf http://localhost:3000 > /dev/null 2>&1; then
echo " Frontend already running on :3000"
if curl -sf "http://localhost:${FRONTEND_PORT}" > /dev/null 2>&1; then
echo " Frontend already running on :$FRONTEND_PORT"
else
echo " Starting frontend..."
pnpm dev:web > /tmp/multica-check-frontend.log 2>&1 &
FRONTEND_PID=$!
STARTED_FRONTEND=true
wait_for_port 3000 "Frontend" 120 "/"
wait_for_port "$FRONTEND_PORT" "Frontend" 120 "/"
fi
# --------------------------------------------------------------------------

View file

@ -0,0 +1,56 @@
#!/usr/bin/env bash
set -euo pipefail
ENV_FILE="${1:-.env.worktree}"
if [ -f "$ENV_FILE" ] && [ "${FORCE:-0}" != "1" ]; then
echo "Refusing to overwrite existing $ENV_FILE. Re-run with FORCE=1 if you want to regenerate it."
exit 1
fi
worktree_name="${WORKTREE_NAME:-$(basename "$PWD")}"
slug="$(printf '%s' "$worktree_name" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/_/g; s/__*/_/g; s/^_//; s/_$//')"
if [ -z "$slug" ]; then
slug="multica"
fi
hash_value="$(printf '%s' "$PWD" | cksum | awk '{print $1}')"
offset=$((hash_value % 1000))
postgres_db="multica_${slug}"
postgres_port=$((15432 + offset))
backend_port=$((18080 + offset))
frontend_port=$((13000 + offset))
frontend_origin="http://localhost:${frontend_port}"
compose_project_name="multica_${slug}_${offset}"
cat > "$ENV_FILE" <<EOF
COMPOSE_PROJECT_NAME=${compose_project_name}
POSTGRES_DB=${postgres_db}
POSTGRES_USER=multica
POSTGRES_PASSWORD=multica
POSTGRES_PORT=${postgres_port}
DATABASE_URL=postgres://multica:multica@localhost:${postgres_port}/${postgres_db}?sslmode=disable
PORT=${backend_port}
JWT_SECRET=change-me-in-production
MULTICA_SERVER_URL=ws://localhost:${backend_port}/ws
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=${frontend_origin}/auth/callback
FRONTEND_PORT=${frontend_port}
FRONTEND_ORIGIN=${frontend_origin}
NEXT_PUBLIC_API_URL=http://localhost:${backend_port}
NEXT_PUBLIC_WS_URL=ws://localhost:${backend_port}/ws
EOF
echo "Generated $ENV_FILE for worktree '$worktree_name'"
echo " Postgres: ${postgres_db} on localhost:${postgres_port}"
echo " Backend: http://localhost:${backend_port}"
echo " Frontend: ${frontend_origin}"
echo ""
echo "Next steps:"
echo " make setup"
echo " make start"