start.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. #
  3. # MyBeacon Frontend Startup Script
  4. #
  5. set -e
  6. # Change to frontend directory
  7. cd "$(dirname "$0")"
  8. # Colors
  9. RED='\033[0;31m'
  10. GREEN='\033[0;32m'
  11. YELLOW='\033[1;33m'
  12. NC='\033[0m' # No Color
  13. # Default values
  14. MODE="${1:-dev}"
  15. HOST="${HOST:-0.0.0.0}"
  16. PORT="${PORT:-5173}"
  17. echo -e "${GREEN}=== MyBeacon Frontend Startup ===${NC}"
  18. echo -e "Mode: ${YELLOW}${MODE}${NC}"
  19. echo -e "Host: ${YELLOW}${HOST}${NC}"
  20. echo -e "Port: ${YELLOW}${PORT}${NC}"
  21. echo ""
  22. # Check if package.json exists
  23. if [ ! -f "package.json" ]; then
  24. echo -e "${RED}Error: package.json not found!${NC}"
  25. exit 1
  26. fi
  27. # Check if npm is installed
  28. if ! command -v npm &> /dev/null; then
  29. echo -e "${RED}Error: npm not found!${NC}"
  30. echo "Install Node.js and npm first"
  31. exit 1
  32. fi
  33. # Install dependencies if needed
  34. if [ ! -d "node_modules" ]; then
  35. echo -e "${YELLOW}Installing dependencies...${NC}"
  36. npm install
  37. fi
  38. # Function to stop frontend
  39. cleanup() {
  40. echo -e "\n${YELLOW}Stopping frontend...${NC}"
  41. pkill -f "vite" 2>/dev/null || true
  42. exit 0
  43. }
  44. trap cleanup SIGINT SIGTERM
  45. # Start frontend based on mode
  46. case "$MODE" in
  47. dev)
  48. echo -e "${GREEN}Starting DEVELOPMENT server...${NC}"
  49. echo -e "${YELLOW}URL: http://${HOST}:${PORT}${NC}"
  50. echo ""
  51. npm run dev -- --host "$HOST" --port "$PORT"
  52. ;;
  53. build)
  54. echo -e "${GREEN}Building for PRODUCTION...${NC}"
  55. echo -e "${YELLOW}Output: ./dist/${NC}"
  56. echo ""
  57. npm run build
  58. echo -e "${GREEN}Build complete!${NC}"
  59. ;;
  60. preview)
  61. echo -e "${GREEN}Starting PREVIEW server...${NC}"
  62. echo -e "${YELLOW}Serving production build${NC}"
  63. echo ""
  64. npm run preview -- --host "$HOST" --port "$PORT"
  65. ;;
  66. background)
  67. echo -e "${GREEN}Starting in BACKGROUND mode...${NC}"
  68. echo -e "${YELLOW}Logs: /tmp/frontend_dev.log${NC}"
  69. echo ""
  70. # Kill existing frontend
  71. pkill -f "vite" 2>/dev/null || true
  72. sleep 1
  73. # Start in background
  74. npm run dev -- --host "$HOST" --port "$PORT" > /tmp/frontend_dev.log 2>&1 &
  75. PID=$!
  76. sleep 2
  77. if ps -p $PID > /dev/null; then
  78. echo -e "${GREEN}Frontend started with PID: ${PID}${NC}"
  79. echo -e "URL: ${YELLOW}http://${HOST}:${PORT}${NC}"
  80. echo -e "Check logs: ${YELLOW}tail -f /tmp/frontend_dev.log${NC}"
  81. echo -e "Stop: ${YELLOW}pkill -f vite${NC}"
  82. else
  83. echo -e "${RED}Failed to start frontend${NC}"
  84. cat /tmp/frontend_dev.log
  85. exit 1
  86. fi
  87. ;;
  88. stop)
  89. echo -e "${YELLOW}Stopping frontend...${NC}"
  90. pkill -f "vite" 2>/dev/null && echo -e "${GREEN}Frontend stopped${NC}" || echo -e "${YELLOW}Frontend not running${NC}"
  91. ;;
  92. restart)
  93. echo -e "${YELLOW}Restarting frontend...${NC}"
  94. pkill -f "vite" 2>/dev/null || true
  95. sleep 2
  96. exec "$0" background
  97. ;;
  98. status)
  99. if pgrep -f "vite" > /dev/null; then
  100. echo -e "${GREEN}Frontend is running${NC}"
  101. ps aux | grep "vite" | grep -v grep
  102. else
  103. echo -e "${YELLOW}Frontend is not running${NC}"
  104. fi
  105. ;;
  106. clean)
  107. echo -e "${YELLOW}Cleaning build artifacts...${NC}"
  108. rm -rf dist node_modules/.vite
  109. echo -e "${GREEN}Clean complete!${NC}"
  110. ;;
  111. install)
  112. echo -e "${YELLOW}Installing dependencies...${NC}"
  113. npm install
  114. echo -e "${GREEN}Install complete!${NC}"
  115. ;;
  116. *)
  117. echo "Usage: $0 {dev|build|preview|background|stop|restart|status|clean|install}"
  118. echo ""
  119. echo "Modes:"
  120. echo " dev - Development server with HMR"
  121. echo " build - Build for production"
  122. echo " preview - Preview production build"
  123. echo " background - Run dev server in background"
  124. echo " stop - Stop frontend"
  125. echo " restart - Restart frontend"
  126. echo " status - Check frontend status"
  127. echo " clean - Clean build artifacts"
  128. echo " install - Install/update dependencies"
  129. echo ""
  130. echo "Environment variables:"
  131. echo " HOST - Bind host (default: 0.0.0.0)"
  132. echo " PORT - Bind port (default: 5173)"
  133. echo ""
  134. echo "Examples:"
  135. echo " $0 dev # Development mode"
  136. echo " $0 background # Background mode"
  137. echo " $0 build # Build production"
  138. echo " PORT=3000 $0 dev # Custom port"
  139. exit 1
  140. ;;
  141. esac