forked from element-hq/element-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·140 lines (120 loc) · 6.46 KB
/
Copy pathdev.sh
File metadata and controls
executable file
·140 lines (120 loc) · 6.46 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
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BOT_DIR="$ROOT_DIR/bot-server"
ROOT_ENV="$ROOT_DIR/.env"
ROOT_ENV_EXAMPLE="$ROOT_DIR/.env.example"
# ── Colours ──────────────────────────────────────────────────────────────────
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m'
log() { echo -e "${CYAN}[dev]${NC} $*"; }
warn() { echo -e "${YELLOW}[dev]${NC} $*"; }
ok() { echo -e "${GREEN}[dev]${NC} $*"; }
err() { echo -e "${RED}[dev]${NC} $*" >&2; }
# ── Prefixed process output ───────────────────────────────────────────────────
prefix_output() {
local prefix="$1"
local color="$2"
while IFS= read -r line; do
echo -e "${color}[${prefix}]${NC} $line"
done
}
# ── Cleanup on exit ───────────────────────────────────────────────────────────
BOT_PID=""
WEB_PID=""
cleanup() {
echo ""
log "Shutting down..."
[[ -n "$WEB_PID" ]] && kill "$WEB_PID" 2>/dev/null || true
[[ -n "$BOT_PID" ]] && kill "$BOT_PID" 2>/dev/null || true
wait 2>/dev/null || true
ok "Done."
}
trap cleanup INT TERM EXIT
# ── 0. Node compatibility ────────────────────────────────────────────────────
# The project wants Node >=22.18 for native TypeScript support. On older 22.x
# we enable the experimental flags so webpack can load .ts configs that use
# import attributes (e.g. `with { type: "json" }`).
NODE_MAJOR="$(node -e 'console.log(process.versions.node.split(".").map(Number).join(" "))')"
read -r MAJOR MINOR PATCH <<< "$NODE_MAJOR"
if (( MAJOR == 22 && MINOR < 18 )); then
warn "Node $(node -v) detected (project wants >=22.18) — enabling experimental TypeScript flags"
export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--experimental-strip-types --experimental-transform-types --disable-warning=ExperimentalWarning"
fi
# ── 0b. Kill stale processes on needed ports ─────────────────────────────────
kill_port() {
local port="$1"
local pids
pids="$(lsof -ti :"$port" 2>/dev/null || true)"
if [[ -n "$pids" ]]; then
warn "Killing existing processes on port $port (PIDs: $(echo $pids | tr '\n' ' '))"
echo "$pids" | xargs kill -9 2>/dev/null || true
sleep 0.5
fi
}
# Source .env early so we can read BUNDLE_PORT for kill_port and URL overrides
set -a && source "$ROOT_ENV" 2>/dev/null && set +a || true
BUNDLE_PORT="${BUNDLE_PORT:-3001}"
kill_port 8080
kill_port "$BUNDLE_PORT"
# Kill stale nx processes that can block the dev server
stale_nx="$(pgrep -f 'nx (start|run)' 2>/dev/null || true)"
if [[ -n "$stale_nx" ]]; then
warn "Killing stale nx processes (PIDs: $(echo $stale_nx | tr '\n' ' '))"
echo "$stale_nx" | xargs kill -9 2>/dev/null || true
sleep 0.5
fi
# ── 1. Install root dependencies ──────────────────────────────────────────────
# --ignore-scripts skips the postinstall (scripts/pnpm-link.ts) which requires
# Node >=22.18 for native TypeScript support. That script is only needed when
# using a .link-config for local SDK development.
log "Installing workspace dependencies..."
pnpm install --ignore-scripts
# ── 2. .env setup ────────────────────────────────────────────────────────────
if [[ ! -f "$ROOT_ENV" ]]; then
warn "No .env found — copying from .env.example"
cp "$ROOT_ENV_EXAMPLE" "$ROOT_ENV"
warn "Edit .env and set MATRIX_BOT_TOKEN and MATRIX_BOT_USER_ID, then re-run this script."
exit 0
fi
# Warn if token looks like a placeholder
if grep -qE 'MATRIX_BOT_TOKEN=$' "$ROOT_ENV"; then
warn ".env has an empty MATRIX_BOT_TOKEN — the bot may not connect."
warn "Edit .env and set MATRIX_BOT_TOKEN and MATRIX_BOT_USER_ID."
fi
# ── 3. Bot server: install dependencies ──────────────────────────────────────
log "Installing bot-server dependencies..."
(cd "$BOT_DIR" && npm install --silent)
# ── 3b. Detect local network IP and export service URLs ───────────────────────
LOCAL_IP="$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | head -1)"
if [[ -n "$LOCAL_IP" ]]; then
export BUNDLE_BASE_URL="http://${LOCAL_IP}:${BUNDLE_PORT}"
export GENERATE_API_URL="http://${LOCAL_IP}:${BUNDLE_PORT}/generate"
ok "Detected network IP: ${LOCAL_IP}"
ok "Services will be accessible to others on your network"
else
warn "Could not detect local network IP — services will only be accessible on this machine"
fi
# ── 4. Start services in parallel ────────────────────────────────────────────
ok "Starting services..."
echo ""
# Element Web (webpack dev server with HMR)
pnpm --filter element-web run start 2>&1 | prefix_output "element-web" '\033[0;34m' &
WEB_PID=$!
# Generation server — run directly with node (no tsx watch/restart).
# Load root .env, then re-apply the network-accessible URLs so .env doesn't override them.
(cd "$BOT_DIR" && set -a && source "$ROOT_ENV" && set +a && \
[[ -n "${LOCAL_IP:-}" ]] && export BUNDLE_BASE_URL="http://${LOCAL_IP}:${BUNDLE_PORT}" GENERATE_API_URL="http://${LOCAL_IP}:${BUNDLE_PORT}/generate"; \
node --import tsx/esm src/index.ts 2>&1) | prefix_output "gen-server" '\033[0;35m' &
BOT_PID=$!
ok "Element Web → http://127.0.0.1:8080 (localhost)"
[[ -n "$LOCAL_IP" ]] && ok "Element Web → http://${LOCAL_IP}:8080 (network)"
ok "Generation server → http://localhost:${BUNDLE_PORT}"
[[ -n "$LOCAL_IP" ]] && ok "Generation server → http://${LOCAL_IP}:${BUNDLE_PORT} (network)"
ok "Press Ctrl+C to stop both services."
echo ""
# Wait for either process to exit unexpectedly
wait -n 2>/dev/null || wait