77 lines
2.5 KiB
Bash
77 lines
2.5 KiB
Bash
#!/usr/bin/env bash
|
||
set -euo pipefail
|
||
|
||
# This script prepares the dev environment and (when sourced) exports env vars.
|
||
# Usage:
|
||
# source ./scripts/environment.sh # export env vars to current shell
|
||
# ./scripts/environment.sh # runs setup checks; prints sourcing hint
|
||
|
||
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||
REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
|
||
cd "$REPO_ROOT"
|
||
|
||
# --- Helper: print next steps -------------------------------------------------
|
||
print_next_steps() {
|
||
echo ""
|
||
echo "Next steps:"
|
||
echo " 1) Start server (in ../server): cargo run -- --from-env --verbose"
|
||
echo " 2) Start portal: ./scripts/start.sh (or ./scripts/start.sh --port 8088)"
|
||
echo " 3) Dev (Trunk): set -a; source .env; set +a; trunk serve"
|
||
}
|
||
|
||
# --- Ensure .env exists (key=value style) -------------------------------------
|
||
if [ ! -f ".env" ]; then
|
||
echo "📝 Creating .env file..."
|
||
cat > .env << EOF
|
||
# Portal Client Configuration
|
||
# This file configures the frontend portal app
|
||
|
||
## Export-style so that 'source .env' exports to current shell
|
||
|
||
# API Key for server authentication (must match one of the API_KEYS in the server .env)
|
||
export API_KEY=dev_key_123
|
||
|
||
# Optional: Override server API base URL (defaults to http://127.0.0.1:3001/api)
|
||
# Example: API_URL=http://localhost:3001/api
|
||
# export API_URL=
|
||
EOF
|
||
echo "✅ Created .env file with default API key"
|
||
else
|
||
echo "✅ .env file already exists"
|
||
fi
|
||
|
||
# --- Install prerequisites ----------------------------------------------------
|
||
if ! command -v trunk >/dev/null 2>&1; then
|
||
echo "📦 Installing trunk..."
|
||
cargo install trunk
|
||
else
|
||
echo "✅ trunk is installed"
|
||
fi
|
||
|
||
if ! rustup target list --installed | grep -q "wasm32-unknown-unknown"; then
|
||
echo "🔧 Adding wasm32-unknown-unknown target..."
|
||
rustup target add wasm32-unknown-unknown
|
||
else
|
||
echo "✅ wasm32-unknown-unknown target present"
|
||
fi
|
||
|
||
# --- Detect if sourced vs executed --------------------------------------------
|
||
# Works for bash and zsh
|
||
is_sourced=false
|
||
# shellcheck disable=SC2296
|
||
if [ -n "${ZSH_EVAL_CONTEXT:-}" ]; then
|
||
case $ZSH_EVAL_CONTEXT in *:file:*) is_sourced=true;; esac
|
||
elif [ -n "${BASH_SOURCE:-}" ] && [ "${BASH_SOURCE[0]}" != "$0" ]; then
|
||
is_sourced=true
|
||
fi
|
||
|
||
if $is_sourced; then
|
||
echo "🔐 Sourcing .env (export-style) into current shell..."
|
||
# shellcheck disable=SC1091
|
||
source .env
|
||
echo "✅ Environment exported (API_KEY, optional API_URL)"
|
||
else
|
||
echo "ℹ️ Run 'source ./scripts/environment.sh' or 'source .env' to export env vars to your shell."
|
||
print_next_steps
|
||
fi
|