|
| 1 | +#!/bin/bash |
| 2 | +# Example: run_dev hook |
| 3 | +# |
| 4 | +# This hook starts a dev server and optionally creates a tunnel for sharing. |
| 5 | +# It reports the server URL back to dmux via the HTTP API. |
| 6 | + |
| 7 | +set -e |
| 8 | + |
| 9 | +echo "[Hook] Starting dev server for $DMUX_SLUG" |
| 10 | + |
| 11 | +cd "$DMUX_WORKTREE_PATH" |
| 12 | +API_URL="http://localhost:$DMUX_SERVER_PORT/api/panes/$DMUX_PANE_ID/dev" |
| 13 | + |
| 14 | +# Update status: starting |
| 15 | +curl -s -X PUT "$API_URL" \ |
| 16 | + -H "Content-Type: application/json" \ |
| 17 | + -d '{"status": "running"}' > /dev/null |
| 18 | + |
| 19 | +# Start dev server in background |
| 20 | +# Adjust the command for your project (pnpm dev, npm run dev, vite, etc.) |
| 21 | +LOG_FILE="/tmp/dmux-dev-$DMUX_PANE_ID.log" |
| 22 | +pnpm dev > "$LOG_FILE" 2>&1 & |
| 23 | +DEV_PID=$! |
| 24 | + |
| 25 | +# Wait for server to be ready |
| 26 | +echo "[Hook] Waiting for dev server to start..." |
| 27 | +sleep 5 |
| 28 | + |
| 29 | +# Detect port from log output |
| 30 | +# Adjust the grep pattern for your dev server's output format |
| 31 | +PORT=$(grep -oP '(?<=localhost:)\d+' "$LOG_FILE" | head -1) |
| 32 | + |
| 33 | +if [ -z "$PORT" ]; then |
| 34 | + echo "[Hook] Warning: Could not detect port from logs, using default 3000" |
| 35 | + PORT=3000 |
| 36 | +fi |
| 37 | + |
| 38 | +LOCAL_URL="http://localhost:$PORT" |
| 39 | +echo "[Hook] Dev server running at $LOCAL_URL" |
| 40 | + |
| 41 | +# Optional: Create a public tunnel (uncomment to enable) |
| 42 | +# Requires ngrok, cloudflared, or another tunneling tool |
| 43 | + |
| 44 | +# Example with cloudflared: |
| 45 | +# TUNNEL_URL=$(cloudflared tunnel --url "$LOCAL_URL" 2>&1 | \ |
| 46 | +# grep -oP 'https://[a-z0-9-]+\.trycloudflare\.com' | head -1) |
| 47 | + |
| 48 | +# Example with ngrok: |
| 49 | +# TUNNEL_URL=$(ngrok http $PORT --log=stdout 2>&1 | \ |
| 50 | +# grep -oP 'url=https://[^"]+' | head -1 | cut -d= -f2) |
| 51 | + |
| 52 | +# For now, just use local URL (uncomment tunnel code above to enable) |
| 53 | +FINAL_URL="$LOCAL_URL" |
| 54 | + |
| 55 | +# Report status back to dmux |
| 56 | +curl -s -X PUT "$API_URL" \ |
| 57 | + -H "Content-Type: application/json" \ |
| 58 | + -d "{\"status\": \"running\", \"url\": \"$FINAL_URL\"}" > /dev/null |
| 59 | + |
| 60 | +echo "[Hook] Dev server ready at: $FINAL_URL" |
| 61 | +echo "[Hook] Dev server PID: $DEV_PID" |
| 62 | +echo "[Hook] Log file: $LOG_FILE" |
0 commit comments