|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Helper script to start Metro and deploy React Native app |
| 3 | +# Usage: start-with-metro.sh <platform> [device] |
| 4 | +# Platform: android, ios, web |
| 5 | +# Device: optional device name (for android/ios) |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +PLATFORM="${1:-}" |
| 10 | +DEVICE="${2:-}" |
| 11 | + |
| 12 | +if [ -z "$PLATFORM" ]; then |
| 13 | + echo "Usage: start-with-metro.sh <platform> [device]" |
| 14 | + echo "Platform: android, ios, web" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +# Source React Native lib |
| 19 | +. "${REACT_NATIVE_VIRTENV}/scripts/lib/lib.sh" |
| 20 | + |
| 21 | +echo "🚀 Starting React Native for $PLATFORM..." |
| 22 | + |
| 23 | +# Check if Metro is already running for this platform |
| 24 | +if metro.sh status "$PLATFORM" 2>&1 | grep -q "Running"; then |
| 25 | + echo "✓ Metro already running for $PLATFORM" |
| 26 | +else |
| 27 | + # Allocate port and start Metro |
| 28 | + echo "📡 Allocating Metro port..." |
| 29 | + metro_port=$(rn_allocate_metro_port "$PLATFORM") |
| 30 | + rn_save_metro_env "$PLATFORM" "$metro_port" |
| 31 | + |
| 32 | + echo "🎬 Starting Metro bundler on port $metro_port..." |
| 33 | + metro.sh start "$PLATFORM" & |
| 34 | + METRO_PID=$! |
| 35 | + |
| 36 | + # Wait for Metro to be healthy |
| 37 | + echo "⏳ Waiting for Metro to be ready..." |
| 38 | + max_attempts=30 |
| 39 | + attempt=0 |
| 40 | + while [ $attempt -lt $max_attempts ]; do |
| 41 | + if metro.sh health "$PLATFORM" "$PLATFORM" 2>/dev/null; then |
| 42 | + echo "✓ Metro is ready" |
| 43 | + break |
| 44 | + fi |
| 45 | + attempt=$((attempt + 1)) |
| 46 | + sleep 2 |
| 47 | + done |
| 48 | + |
| 49 | + if [ $attempt -ge $max_attempts ]; then |
| 50 | + echo "✗ Metro failed to start within timeout" |
| 51 | + kill $METRO_PID 2>/dev/null || true |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +fi |
| 55 | + |
| 56 | +# Source Metro environment |
| 57 | +. "${REACT_NATIVE_VIRTENV}/metro/env-${PLATFORM}.sh" |
| 58 | + |
| 59 | +# Deploy based on platform |
| 60 | +case "$PLATFORM" in |
| 61 | + android) |
| 62 | + echo "📲 Deploying to Android..." |
| 63 | + if [ -n "$DEVICE" ]; then |
| 64 | + # Start specific device |
| 65 | + android.sh emulator start "$DEVICE" |
| 66 | + fi |
| 67 | + npx react-native run-android --no-packager |
| 68 | + echo "✓ Android app deployed on port $METRO_PORT" |
| 69 | + ;; |
| 70 | + |
| 71 | + ios) |
| 72 | + echo "📲 Deploying to iOS..." |
| 73 | + if [ -n "$DEVICE" ]; then |
| 74 | + # Start specific device |
| 75 | + ios.sh simulator start "$DEVICE" |
| 76 | + fi |
| 77 | + npx react-native run-ios --no-packager |
| 78 | + echo "✓ iOS app deployed on port $METRO_PORT" |
| 79 | + ;; |
| 80 | + |
| 81 | + web) |
| 82 | + echo "🌐 Opening web browser..." |
| 83 | + # Create web build directory |
| 84 | + mkdir -p web/build |
| 85 | + |
| 86 | + # Open default browser |
| 87 | + if command -v open >/dev/null 2>&1; then |
| 88 | + open "http://localhost:$METRO_PORT" |
| 89 | + elif command -v xdg-open >/dev/null 2>&1; then |
| 90 | + xdg-open "http://localhost:$METRO_PORT" |
| 91 | + elif command -v start >/dev/null 2>&1; then |
| 92 | + start "http://localhost:$METRO_PORT" |
| 93 | + else |
| 94 | + echo "⚠ Could not detect browser launcher" |
| 95 | + echo " Please open: http://localhost:$METRO_PORT" |
| 96 | + fi |
| 97 | + echo "✓ Browser opened to http://localhost:$METRO_PORT" |
| 98 | + ;; |
| 99 | + |
| 100 | + *) |
| 101 | + echo "✗ Unknown platform: $PLATFORM" |
| 102 | + echo " Supported: android, ios, web" |
| 103 | + exit 1 |
| 104 | + ;; |
| 105 | +esac |
| 106 | + |
| 107 | +echo "" |
| 108 | +echo "✓ React Native $PLATFORM is running!" |
| 109 | +echo " Metro port: $METRO_PORT" |
| 110 | +echo "" |
| 111 | +echo "To stop Metro:" |
| 112 | +echo " metro.sh stop $PLATFORM" |
0 commit comments