Files
JesebelGPT/run.sh
2026-01-27 02:01:40 +01:00

79 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
cmd="${1:-dev}"
if [[ ! -f ".env" ]]; then
if [[ -f ".env.example" ]]; then
cp .env.example .env
echo "Created .env from .env.example. Please edit it (FIRMWARE_API_KEY, ADMIN_TOKEN)." >&2
else
echo "Missing .env (and .env.example)." >&2
fi
fi
if [[ -f ".env" ]]; then
# Load .env without overriding already-exported vars.
# Intentionally skip FIRMWARE_API_KEY (should come from shell env).
while IFS= read -r line || [[ -n "$line" ]]; do
[[ -z "$line" ]] && continue
[[ "$line" =~ ^[[:space:]]*# ]] && continue
if [[ "$line" =~ ^[[:space:]]*([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
key="${BASH_REMATCH[1]}"
val="${BASH_REMATCH[2]}"
if [[ "$key" == "FIRMWARE_API_KEY" ]]; then
continue
fi
# Strip optional surrounding quotes
if [[ "$val" =~ ^\"(.*)\"$ ]]; then
val="${BASH_REMATCH[1]}"
elif [[ "$val" =~ ^\'(.*)\'$ ]]; then
val="${BASH_REMATCH[1]}"
fi
if [[ -z "${!key:-}" ]]; then
export "$key=$val"
fi
fi
done < .env
fi
if [[ -z "${FIRMWARE_API_KEY:-}" ]]; then
echo "FIRMWARE_API_KEY must be exported in your shell." >&2
echo "Example: export FIRMWARE_API_KEY=..." >&2
exit 1
fi
if [[ ! -d "node_modules" ]]; then
npm install
fi
case "$cmd" in
build)
echo "Building web..." >&2
npm -w web run build
;;
dev)
echo "Starting dev (web + server)..." >&2
echo "Web: http://localhost:5173" >&2
echo "API: http://localhost:${PORT:-8787}" >&2
echo "Admin: add header x-admin-token: \$ADMIN_TOKEN" >&2
npm run dev
;;
server)
echo "Starting server only..." >&2
echo "Binding: http://${HOST:-0.0.0.0}:${PORT:-8787}" >&2
npm -w server run start
;;
web)
echo "Starting web only..." >&2
npm -w web run dev
;;
*)
echo "Usage: ./run.sh [dev|server|web|build]" >&2
exit 2
;;
esac