-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminitor
More file actions
executable file
·270 lines (245 loc) · 6.99 KB
/
Copy pathminitor
File metadata and controls
executable file
·270 lines (245 loc) · 6.99 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env bash
#
# ./minitor — one-command launcher.
#
# ./minitor start the dev server (default)
# ./minitor build production build
# ./minitor start production server (after build)
# ./minitor migrate run DB migrations only
# ./minitor doctor print environment diagnostics
# ./minitor reset wipe the local PGlite data dir
#
# Bootstraps node_modules, .env.local, and the local PGlite database the first
# time it runs. Re-running with no args just starts the dev server.
set -euo pipefail
cd "$(dirname "$0")"
# ---- pretty output --------------------------------------------------------
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
C_DIM=$'\033[2m'; C_BOLD=$'\033[1m'; C_GREEN=$'\033[32m'
C_YELLOW=$'\033[33m'; C_RED=$'\033[31m'; C_RESET=$'\033[0m'
else
C_DIM=""; C_BOLD=""; C_GREEN=""; C_YELLOW=""; C_RED=""; C_RESET=""
fi
step() { printf "%s▸%s %s\n" "$C_BOLD" "$C_RESET" "$1"; }
ok() { printf " %s✓%s %s\n" "$C_GREEN" "$C_RESET" "$1"; }
warn() { printf " %s!%s %s\n" "$C_YELLOW" "$C_RESET" "$1" >&2; }
die() { printf " %s✗%s %s\n" "$C_RED" "$C_RESET" "$1" >&2; exit 1; }
muted(){ printf " %s%s%s\n" "$C_DIM" "$1" "$C_RESET"; }
# Run a command quietly — only show output if it fails.
run_quiet() {
local log_file rc=0
log_file=$(mktemp -t minitor.XXXXXX)
if "$@" >"$log_file" 2>&1; then
rm -f "$log_file"
else
rc=$?
cat "$log_file" >&2
rm -f "$log_file"
return "$rc"
fi
}
# Print the dev server URL as a bold, clickable hyperlink (OSC 8).
print_url_banner() {
local url="$1"
local link="$url"
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
link=$(printf '\033]8;;%s\033\\%s\033]8;;\033\\' "$url" "$url")
fi
printf "\n"
printf " %sMinitor is ready →%s %s%s%s\n" "$C_BOLD" "$C_RESET" "$C_BOLD$C_GREEN" "$link" "$C_RESET"
printf " %sCtrl-C to stop%s\n\n" "$C_DIM" "$C_RESET"
}
# ---- requirements ---------------------------------------------------------
require_node() {
if ! command -v node >/dev/null 2>&1; then
die "Node.js is not installed. Install Node 20+ from https://nodejs.org"
fi
local major
major=$(node -p "process.versions.node.split('.')[0]")
if [ "$major" -lt 20 ]; then
die "Node $major detected — Minitor needs Node 20 or newer."
fi
}
# ---- pick a package manager ----------------------------------------------
PKG=""
pick_pkg_manager() {
if [ -f pnpm-lock.yaml ] && command -v pnpm >/dev/null 2>&1; then
PKG="pnpm"
elif [ -f yarn.lock ] && command -v yarn >/dev/null 2>&1; then
PKG="yarn"
elif [ -f bun.lockb ] && command -v bun >/dev/null 2>&1; then
PKG="bun"
else
PKG="npm"
fi
}
pkg_install() {
case "$PKG" in
pnpm) pnpm install --reporter=append-only --loglevel=error ;;
yarn) yarn install --silent ;;
bun) bun install --silent ;;
*) npm install --no-fund --no-audit --loglevel=error ;;
esac
}
pkg_run() {
case "$PKG" in
pnpm) pnpm "$@" ;;
yarn) yarn "$@" ;;
bun) bun run "$@" ;;
*) npm run "$@" ;;
esac
}
pkg_exec() {
case "$PKG" in
pnpm) pnpm exec "$@" ;;
yarn) yarn "$@" ;;
bun) bunx "$@" ;;
*) npx --no-install "$@" || npx "$@" ;;
esac
}
# ---- bootstrap steps ------------------------------------------------------
pkg_lockfile() {
case "$PKG" in
pnpm) printf "pnpm-lock.yaml" ;;
yarn) printf "yarn.lock" ;;
bun) printf "bun.lockb" ;;
*) printf "package-lock.json" ;;
esac
}
ensure_deps() {
local lockfile
lockfile=$(pkg_lockfile)
if [ ! -d node_modules ]; then
step "Installing dependencies"
pkg_install
elif [ package.json -nt node_modules ]; then
step "package.json changed — reinstalling dependencies"
pkg_install
elif [ -f "$lockfile" ] && [ "$lockfile" -nt node_modules ]; then
step "Lockfile changed — reinstalling dependencies"
pkg_install
fi
}
ensure_env() {
if [ ! -f .env.local ]; then
if [ -f .env.example ]; then
cp .env.example .env.local
warn "Set XAI_API_KEY in .env.local for X / News / Web / Grok columns."
muted "Other keys (Neynar, YouTube, GitHub) are optional — public fallbacks work."
else
: > .env.local
fi
fi
}
ensure_db() {
if ! run_quiet pkg_run db:migrate; then
die "database migration failed"
fi
}
# ---- subcommands ----------------------------------------------------------
cmd_dev() {
require_node
pick_pkg_manager
ensure_deps
ensure_env
ensure_db
print_url_banner "http://localhost:3000"
exec_dev
}
exec_dev() {
case "$PKG" in
pnpm) exec pnpm -s dev ;;
yarn) exec yarn -s dev ;;
bun) exec bun run --silent dev ;;
*) exec npm run dev --silent ;;
esac
}
cmd_build() {
require_node
pick_pkg_manager
ensure_deps
ensure_env
ensure_db
step "Building for production"
pkg_run build
}
cmd_start() {
require_node
pick_pkg_manager
ensure_deps
ensure_env
ensure_db
step "Starting production server"
case "$PKG" in
pnpm) exec pnpm start ;;
yarn) exec yarn start ;;
bun) exec bun run start ;;
*) exec npm run start ;;
esac
}
cmd_migrate() {
require_node
pick_pkg_manager
ensure_deps
ensure_env
step "Running database migrations"
pkg_run db:migrate
}
cmd_doctor() {
step "Diagnostics"
printf " os %s\n" "$(uname -s) $(uname -r)"
if command -v node >/dev/null 2>&1; then
printf " node %s\n" "$(node -v)"
else
printf " node %snot installed%s\n" "$C_RED" "$C_RESET"
fi
pick_pkg_manager >/dev/null 2>&1 || true
printf " pkg mgr %s\n" "$PKG"
if [ -f .env.local ]; then
if grep -q '^XAI_API_KEY=xai-[^.]' .env.local 2>/dev/null; then
printf " XAI_API_KEY %sset%s\n" "$C_GREEN" "$C_RESET"
else
printf " XAI_API_KEY %smissing or placeholder%s\n" "$C_YELLOW" "$C_RESET"
fi
else
printf " .env.local %smissing%s\n" "$C_YELLOW" "$C_RESET"
fi
if [ -d .minitor/pgdata ]; then
printf " database PGlite at .minitor/pgdata\n"
else
printf " database not initialized yet\n"
fi
}
cmd_reset() {
step "Resetting local PGlite database"
if [ -d .minitor/pgdata ]; then
rm -rf .minitor/pgdata
ok "Wiped .minitor/pgdata"
else
muted "Nothing to wipe."
fi
}
cmd_help() {
cat <<'USAGE'
Minitor launcher
Usage:
./minitor start the dev server (default)
./minitor build production build
./minitor start production server (after build)
./minitor migrate run DB migrations only
./minitor doctor print environment diagnostics
./minitor reset wipe the local PGlite data dir
./minitor help show this message
USAGE
}
# ---- entry point ----------------------------------------------------------
case "${1:-dev}" in
""|dev|run|up|start-dev) cmd_dev ;;
build) cmd_build ;;
start|prod) cmd_start ;;
migrate|db|db:migrate) cmd_migrate ;;
doctor|status) cmd_doctor ;;
reset|clean) cmd_reset ;;
help|-h|--help) cmd_help ;;
*) printf "Unknown command: %s\n\n" "$1" >&2; cmd_help; exit 2 ;;
esac