-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-stack.sh
More file actions
executable file
·196 lines (159 loc) · 4.05 KB
/
Copy pathdev-stack.sh
File metadata and controls
executable file
·196 lines (159 loc) · 4.05 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env bash
set -Eeuo pipefail
ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT_DIR"
SCRIPT_PATH="dev-stack.sh"
COMMAND="${1:-up}"
SEED=false
ENSURE_USERS=false
print_help() {
cat <<EOF
Usage:
./$SCRIPT_PATH up [--seed]
./$SCRIPT_PATH down
./$SCRIPT_PATH help
Commands:
up Start PostgreSQL via Docker Compose, apply Prisma migrations, then launch frontend + API.
down Stop the Docker Compose database services started for local development.
help Show this help message.
Options:
--seed Run \`npm run db:seed\` after migrations. Warning: this resets seeded data.
--dev-users Run \`npm run db:ensure-users\` after migrations.
EOF
}
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Missing required command: $1" >&2
exit 1
fi
}
load_env_file() {
local env_file
if [ -f ".env" ]; then
env_file=".env"
elif [ -f ".env.example" ]; then
env_file=".env.example"
else
echo "Neither .env nor .env.example was found." >&2
exit 1
fi
echo "Using environment file: $env_file"
set -a
# shellcheck disable=SC1090
source "$env_file"
set +a
}
resolve_compose_cmd() {
if docker compose version >/dev/null 2>&1; then
COMPOSE_CMD=("docker" "compose")
return
fi
if command -v docker-compose >/dev/null 2>&1; then
COMPOSE_CMD=("docker-compose")
return
fi
echo "Docker Compose is required but was not found." >&2
exit 1
}
compose() {
"${COMPOSE_CMD[@]}" "$@"
}
ensure_port_available() {
local port="$1"
local service_name="$2"
if ! command -v lsof >/dev/null 2>&1; then
return
fi
if lsof -nP -iTCP:"$port" -sTCP:LISTEN >/dev/null 2>&1; then
echo "Port $port is already in use, so $service_name cannot start." >&2
lsof -nP -iTCP:"$port" -sTCP:LISTEN >&2 || true
echo "Stop the process above or change the port before rerunning ./$SCRIPT_PATH up." >&2
exit 1
fi
}
wait_for_database() {
local attempt=0
local max_attempts=60
echo "Waiting for PostgreSQL to become ready..."
while ! compose exec -T db pg_isready -U postgres -d openhackathon >/dev/null 2>&1; do
attempt=$((attempt + 1))
if [ "$attempt" -ge "$max_attempts" ]; then
echo "PostgreSQL did not become ready within ${max_attempts} seconds." >&2
exit 1
fi
sleep 1
done
}
start_stack() {
require_command docker
require_command npm
require_command npx
load_env_file
resolve_compose_cmd
echo "Starting PostgreSQL container..."
compose up -d db
wait_for_database
echo "Applying Prisma migrations..."
npx prisma migrate deploy
if [ "$SEED" = true ]; then
echo "Running seed data reset..."
npm run db:seed
elif [ "$ENSURE_USERS" = true ]; then
echo "Ensuring development accounts..."
npm run db:ensure-users
else
echo "Skipping default user bootstrap. Use Setup Wizard to create the first admin account."
fi
ensure_port_available "${PORT:-3001}" "the API server"
ensure_port_available "5173" "the Vite dev server"
echo
echo "Starting frontend and API:"
echo " Frontend: http://localhost:5173"
echo " API: http://localhost:${PORT:-3001}"
echo " Database: postgres://postgres:postgrespassword@localhost:5432/openhackathon"
echo
echo "Press Ctrl+C to stop the frontend/API processes."
echo "Run 'npm run dev:down' to stop the Docker database container."
echo
exec npm run dev
}
stop_stack() {
require_command docker
resolve_compose_cmd
echo "Stopping PostgreSQL container..."
compose stop db >/dev/null 2>&1 || true
}
case "$COMMAND" in
up)
if [ "$#" -gt 0 ]; then
shift
fi
for arg in "$@"; do
case "$arg" in
--seed)
SEED=true
;;
--dev-users)
ENSURE_USERS=true
;;
*)
echo "Unknown option for 'up': $arg" >&2
print_help >&2
exit 1
;;
esac
done
start_stack
;;
down)
stop_stack
;;
help|-h|--help)
print_help
;;
*)
echo "Unknown command: $COMMAND" >&2
print_help >&2
exit 1
;;
esac