#!/usr/bin/env bash # # One-command local setup. Idempotent: safe to re-run at any time. # # pnpm setup # set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" cd "$ROOT" blue() { printf "\033[34m%s\033[0m\n" "$1"; } green() { printf "\033[32m%s\033[0m\n" "$1"; } warn() { printf "\033[33m%s\033[0m\n" "$1"; } # --- 1. Environment files --------------------------------------------------- blue "→ Preparing environment files" for example in .env.example apps/api/.env.example apps/storefront/.env.example apps/admin/.env.example; do target="${example%.example}" if [ -f "$target" ]; then echo " · $target already exists, leaving it alone" else cp "$example" "$target" echo " · created $target" fi done # --- 2. Real JWT secrets ---------------------------------------------------- # The committed examples contain obvious placeholders. Replace them with real # random values so that no environment — not even a laptop — runs on a secret # that exists in the repository. if grep -q "dev-only-access-secret-change-me" apps/api/.env 2>/dev/null; then blue "→ Generating JWT secrets" access_secret="$(openssl rand -base64 48 | tr -d '\n/+=' | cut -c1-48)" refresh_secret="$(openssl rand -base64 48 | tr -d '\n/+=' | cut -c1-48)" # BSD and GNU sed disagree about -i; write through a temp file instead. tmp="$(mktemp)" sed -e "s|^JWT_ACCESS_SECRET=.*|JWT_ACCESS_SECRET=${access_secret}|" \ -e "s|^JWT_REFRESH_SECRET=.*|JWT_REFRESH_SECRET=${refresh_secret}|" \ apps/api/.env > "$tmp" && mv "$tmp" apps/api/.env echo " · wrote fresh secrets to apps/api/.env" fi # --- 3. Dependencies -------------------------------------------------------- blue "→ Installing dependencies" pnpm install # --- 4. Backing services ---------------------------------------------------- blue "→ Starting PostgreSQL, Redis, MinIO and Mailpit" docker compose up -d postgres redis minio minio-init mailpit blue "→ Waiting for PostgreSQL" for _ in $(seq 1 30); do if docker compose exec -T postgres pg_isready -q 2>/dev/null; then break; fi sleep 1 done # --- 5. Database ------------------------------------------------------------ blue "→ Applying migrations and seeding" pnpm --filter @sport/api run db:generate if [ -d apps/api/prisma/migrations ]; then pnpm --filter @sport/api exec prisma migrate deploy else warn " · no migrations yet — run: pnpm db:migrate --name init" pnpm --filter @sport/api exec prisma db push fi pnpm --filter @sport/api run db:seed green "" green "Ready. Start everything with: pnpm dev" green "" echo " Storefront http://localhost:3000" echo " Admin http://localhost:3001" echo " API http://localhost:4000/api/v1/health" echo " API docs http://localhost:4000/docs" echo " MinIO http://localhost:9001" echo " Mailpit http://localhost:8025"