-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·147 lines (121 loc) · 4.22 KB
/
dev.sh
File metadata and controls
executable file
·147 lines (121 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
set -e
# ─── eggroll-pos development launcher ───
# Usage:
# ./dev.sh # PostgreSQL via Docker (default)
# ./dev.sh --sqlite # SQLite, no Docker required
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() { echo -e "${CYAN}[dev]${NC} $1"; }
ok() { echo -e "${GREEN}[dev] ✓${NC} $1"; }
warn() { echo -e "${YELLOW}[dev] !${NC} $1"; }
err() { echo -e "${RED}[dev] ✗${NC} $1"; }
USE_SQLITE=false
if [ "$1" = "--sqlite" ]; then
USE_SQLITE=true
fi
cleanup() {
log "Shutting down..."
kill $EXPRESS_PID $VITE_PID 2>/dev/null || true
wait $EXPRESS_PID $VITE_PID 2>/dev/null || true
if [ "$USE_SQLITE" = true ]; then
log "Done. SQLite database is at db/eggrollpos.db"
else
log "Done. Docker Postgres is still running — stop with: docker compose down"
fi
}
trap cleanup EXIT INT TERM
# ─── 1. Check prerequisites ───
if ! command -v pnpm &>/dev/null; then
err "pnpm is not installed. Install it with: npm install -g pnpm"
exit 1
fi
if [ "$USE_SQLITE" = false ] && ! command -v docker &>/dev/null; then
warn "Docker not found. Falling back to SQLite mode."
USE_SQLITE=true
fi
# ─── 2. Install dependencies ───
if [ ! -d node_modules ]; then
log "Installing dependencies..."
pnpm install
ok "Dependencies installed"
else
log "node_modules exists, skipping install (run 'pnpm install' manually if needed)"
fi
# ─── 3. Database setup ───
if [ "$USE_SQLITE" = true ]; then
export DB_CLIENT=sqlite3
log "Using SQLite (db/eggrollpos.db)"
else
log "Starting PostgreSQL..."
docker compose up -d --wait postgres 2>/dev/null || docker-compose up -d postgres 2>/dev/null
for i in {1..20}; do
if docker compose exec -T postgres pg_isready -U postgres &>/dev/null; then
ok "PostgreSQL is ready on port 5432"
break
fi
if [ $i -eq 20 ]; then
err "PostgreSQL failed to start"
exit 1
fi
sleep 1
done
export DB_PASSWORD=postgres
fi
# ─── 4. Run migrations ───
log "Running database migrations..."
npx knex migrate:latest --knexfile db/knexfile.js 2>&1 | tail -1
ok "Migrations complete"
# ─── 5. Seed data (only if tables are empty) ───
if [ "$USE_SQLITE" = true ]; then
ROW_COUNT=$(node -e "
const knex = require('./db/knex');
knex('merchants').count('* as c').first().then(r => {
console.log(r.c);
knex.destroy();
}).catch(() => { console.log('0'); knex.destroy(); });
" 2>/dev/null || echo "0")
else
ROW_COUNT=$(docker compose exec -T postgres psql -U postgres -d eggrollpos -tAc "SELECT count(*) FROM merchants" 2>/dev/null || echo "0")
fi
if [ "$ROW_COUNT" = "0" ] || [ "$ROW_COUNT" = "" ]; then
log "Seeding development data..."
npx knex seed:run --knexfile db/knexfile.js 2>&1 | tail -1
ok "Seed data loaded"
else
log "Database already has data, skipping seeds"
fi
# ─── 6. Start Express backend ───
log "Starting Express API server on port 3000..."
if [ "$USE_SQLITE" = true ]; then
NODE_ENV=development DB_CLIENT=sqlite3 npx tsx ./bin/www &
else
NODE_ENV=development DB_PASSWORD=postgres npx tsx ./bin/www &
fi
EXPRESS_PID=$!
# ─── 7. Start Vite dev server ───
log "Starting Vite dev server on port 3001..."
npx vite &
VITE_PID=$!
# ─── 8. Wait for servers ───
sleep 3
echo ""
echo -e "${GREEN}════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN} eggroll-pos is running!${NC}"
echo -e "${GREEN}════════════════════════════════════════════════════════${NC}"
echo ""
echo -e " Frontend: ${CYAN}http://localhost:3001${NC}"
echo -e " API: ${CYAN}http://localhost:3000${NC}"
echo -e " Merchant: ${CYAN}http://localhost:3001/merchant${NC}"
if [ "$USE_SQLITE" = true ]; then
echo -e " Database: ${CYAN}SQLite → db/eggrollpos.db${NC}"
else
echo -e " Database: ${CYAN}postgres://postgres:postgres@localhost:5432/eggrollpos${NC}"
fi
echo ""
echo -e " Press ${YELLOW}Ctrl+C${NC} to stop dev servers."
echo ""
wait