|
| 1 | +#!/usr/bin/env bash |
| 2 | +# PropChain Developer Environment Setup Script |
| 3 | +# Issue #926 - Single-command developer onboarding |
| 4 | +# Usage: bash scripts/setup.sh |
| 5 | + |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +BOLD='\033[1m' |
| 9 | +GREEN='\033[0;32m' |
| 10 | +YELLOW='\033[1;33m' |
| 11 | +RED='\033[0;31m' |
| 12 | +NC='\033[0m' # No Color |
| 13 | + |
| 14 | +log_info() { echo -e "${GREEN}[INFO]${NC} $*"; } |
| 15 | +log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } |
| 16 | +log_error() { echo -e "${RED}[ERROR]${NC} $*"; } |
| 17 | +log_step() { echo -e "\n${BOLD}==> $*${NC}"; } |
| 18 | + |
| 19 | +# ─── Prerequisites check ────────────────────────────────────────────────────── |
| 20 | +log_step "Checking prerequisites" |
| 21 | + |
| 22 | +check_cmd() { |
| 23 | + local cmd=$1 |
| 24 | + local min_version=${2:-} |
| 25 | + if command -v "$cmd" &>/dev/null; then |
| 26 | + log_info "$cmd found: $(${cmd} --version 2>&1 | head -1)" |
| 27 | + else |
| 28 | + log_error "$cmd is not installed. Please install it and re-run this script." |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | +} |
| 32 | + |
| 33 | +check_cmd node |
| 34 | +check_cmd npm |
| 35 | +check_cmd psql |
| 36 | +check_cmd redis-cli |
| 37 | + |
| 38 | +# Node.js version check (>= 18) |
| 39 | +NODE_MAJOR=$(node -e "process.stdout.write(process.versions.node.split('.')[0])") |
| 40 | +if [ "$NODE_MAJOR" -lt 18 ]; then |
| 41 | + log_error "Node.js >= 18 is required (found $NODE_MAJOR). Please upgrade." |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | +log_info "Node.js version OK (>= 18)" |
| 45 | + |
| 46 | +# npm version check (>= 8) |
| 47 | +NPM_MAJOR=$(npm --version | cut -d. -f1) |
| 48 | +if [ "$NPM_MAJOR" -lt 8 ]; then |
| 49 | + log_error "npm >= 8 is required (found $NPM_MAJOR). Please upgrade." |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | +log_info "npm version OK (>= 8)" |
| 53 | + |
| 54 | +# ─── Environment file ───────────────────────────────────────────────────────── |
| 55 | +log_step "Setting up environment" |
| 56 | + |
| 57 | +if [ ! -f .env ]; then |
| 58 | + if [ -f .env.example ]; then |
| 59 | + cp .env.example .env |
| 60 | + log_info "Created .env from .env.example" |
| 61 | + log_warn "Please review .env and set the required values (DATABASE_URL, JWT_SECRET, etc.)" |
| 62 | + else |
| 63 | + log_warn ".env.example not found – creating minimal .env with defaults" |
| 64 | + cat > .env <<'EOF' |
| 65 | +DATABASE_URL=postgresql://postgres:postgres@localhost:5432/propchain |
| 66 | +PORT=3000 |
| 67 | +NODE_ENV=development |
| 68 | +JWT_SECRET=dev-jwt-secret-change-in-production |
| 69 | +JWT_REFRESH_SECRET=dev-refresh-secret-change-in-production |
| 70 | +JWT_ACCESS_EXPIRES_IN=15m |
| 71 | +JWT_REFRESH_EXPIRES_IN=7d |
| 72 | +BCRYPT_ROUNDS=10 |
| 73 | +REDIS_HOST=localhost |
| 74 | +REDIS_PORT=6379 |
| 75 | +FRONTEND_URL=http://localhost:3000 |
| 76 | +BASE_URL=http://localhost:3000 |
| 77 | +API_URL=http://localhost:3000/api |
| 78 | +EOF |
| 79 | + log_info "Created .env with development defaults" |
| 80 | + fi |
| 81 | +else |
| 82 | + log_info ".env already exists – skipping" |
| 83 | +fi |
| 84 | + |
| 85 | +# ─── Install dependencies ───────────────────────────────────────────────────── |
| 86 | +log_step "Installing Node.js dependencies" |
| 87 | +npm ci |
| 88 | +log_info "Dependencies installed" |
| 89 | + |
| 90 | +# ─── Database setup ─────────────────────────────────────────────────────────── |
| 91 | +log_step "Setting up the database" |
| 92 | + |
| 93 | +# Source the DATABASE_URL from .env (simple extraction, no subshell needed) |
| 94 | +DATABASE_URL=$(grep -E '^DATABASE_URL=' .env | head -1 | cut -d= -f2-) |
| 95 | +export DATABASE_URL |
| 96 | + |
| 97 | +# Generate Prisma client |
| 98 | +log_info "Generating Prisma client..." |
| 99 | +npm run db:generate |
| 100 | + |
| 101 | +# Run migrations |
| 102 | +log_info "Running database migrations..." |
| 103 | +if npm run migrate 2>&1; then |
| 104 | + log_info "Migrations applied successfully" |
| 105 | +else |
| 106 | + log_warn "Migrations failed – attempting db push (dev mode)..." |
| 107 | + npx prisma db push --accept-data-loss || true |
| 108 | +fi |
| 109 | + |
| 110 | +# Optional seed |
| 111 | +if [ "${SKIP_SEED:-false}" != "true" ]; then |
| 112 | + log_info "Seeding the database..." |
| 113 | + npm run db:seed 2>/dev/null && log_info "Database seeded" || log_warn "Seed script not found or failed – skipping" |
| 114 | +fi |
| 115 | + |
| 116 | +# ─── Done ───────────────────────────────────────────────────────────────────── |
| 117 | +log_step "Setup complete!" |
| 118 | +echo -e "" |
| 119 | +echo -e " ${GREEN}Start the development server:${NC}" |
| 120 | +echo -e " npm run start:dev" |
| 121 | +echo -e "" |
| 122 | +echo -e " ${GREEN}Useful commands:${NC}" |
| 123 | +echo -e " npm test – run unit tests" |
| 124 | +echo -e " npm run lint – lint the codebase" |
| 125 | +echo -e " npm run db:studio – open Prisma Studio" |
| 126 | +echo -e " npm run build – production build" |
| 127 | +echo -e "" |
0 commit comments