start.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. echo -e "${YELLOW}Run '$0 deploy' to copy to nginx${NC}"
  60. ;;
  61. deploy)
  62. echo -e "${GREEN}Building and deploying to nginx...${NC}"
  63. echo ""
  64. # Build
  65. npm run build
  66. # Copy to nginx root
  67. NGINX_ROOT="/var/www/mybeacon/frontend"
  68. echo -e "${YELLOW}Copying to ${NGINX_ROOT}...${NC}"
  69. sudo rm -rf "${NGINX_ROOT}"/*
  70. sudo cp -r dist/* "${NGINX_ROOT}/"
  71. sudo chown -R www-data:www-data "${NGINX_ROOT}"
  72. echo -e "${GREEN}Deploy complete!${NC}"
  73. echo -e "Frontend: ${YELLOW}http://localhost:80${NC}"
  74. ;;
  75. preview)
  76. echo -e "${GREEN}Starting PREVIEW server...${NC}"
  77. echo -e "${YELLOW}Serving production build${NC}"
  78. echo ""
  79. npm run preview -- --host "$HOST" --port "$PORT"
  80. ;;
  81. background)
  82. echo -e "${GREEN}Starting in BACKGROUND mode...${NC}"
  83. echo -e "${YELLOW}Logs: /tmp/frontend_dev.log${NC}"
  84. echo ""
  85. # Kill existing frontend
  86. pkill -f "vite" 2>/dev/null || true
  87. sleep 1
  88. # Start in background
  89. npm run dev -- --host "$HOST" --port "$PORT" > /tmp/frontend_dev.log 2>&1 &
  90. PID=$!
  91. sleep 2
  92. if ps -p $PID > /dev/null; then
  93. echo -e "${GREEN}Frontend started with PID: ${PID}${NC}"
  94. echo -e "URL: ${YELLOW}http://${HOST}:${PORT}${NC}"
  95. echo -e "Check logs: ${YELLOW}tail -f /tmp/frontend_dev.log${NC}"
  96. echo -e "Stop: ${YELLOW}pkill -f vite${NC}"
  97. else
  98. echo -e "${RED}Failed to start frontend${NC}"
  99. cat /tmp/frontend_dev.log
  100. exit 1
  101. fi
  102. ;;
  103. stop)
  104. echo -e "${YELLOW}Stopping frontend...${NC}"
  105. pkill -f "vite" 2>/dev/null && echo -e "${GREEN}Frontend stopped${NC}" || echo -e "${YELLOW}Frontend not running${NC}"
  106. ;;
  107. restart)
  108. echo -e "${YELLOW}Restarting frontend...${NC}"
  109. pkill -f "vite" 2>/dev/null || true
  110. sleep 2
  111. exec "$0" background
  112. ;;
  113. status)
  114. if pgrep -f "vite" > /dev/null; then
  115. echo -e "${GREEN}Frontend is running${NC}"
  116. ps aux | grep "vite" | grep -v grep
  117. else
  118. echo -e "${YELLOW}Frontend is not running${NC}"
  119. fi
  120. ;;
  121. clean)
  122. echo -e "${YELLOW}Cleaning build artifacts...${NC}"
  123. rm -rf dist node_modules/.vite
  124. echo -e "${GREEN}Clean complete!${NC}"
  125. ;;
  126. install)
  127. echo -e "${YELLOW}Installing dependencies...${NC}"
  128. npm install
  129. echo -e "${GREEN}Install complete!${NC}"
  130. ;;
  131. *)
  132. echo "Usage: $0 {dev|build|deploy|preview|background|stop|restart|status|clean|install}"
  133. echo ""
  134. echo "Modes:"
  135. echo " dev - Development server with HMR"
  136. echo " build - Build for production (output: ./dist/)"
  137. echo " deploy - Build and copy to nginx (/var/www/mybeacon/frontend)"
  138. echo " preview - Preview production build"
  139. echo " background - Run dev server in background"
  140. echo " stop - Stop frontend"
  141. echo " restart - Restart frontend"
  142. echo " status - Check frontend status"
  143. echo " clean - Clean build artifacts"
  144. echo " install - Install/update dependencies"
  145. echo ""
  146. echo "Environment variables:"
  147. echo " HOST - Bind host (default: 0.0.0.0)"
  148. echo " PORT - Bind port (default: 5173)"
  149. echo ""
  150. echo "Examples:"
  151. echo " $0 dev # Development mode"
  152. echo " $0 deploy # Build + deploy to nginx"
  153. echo " $0 build # Build only (no deploy)"
  154. echo " PORT=3000 $0 dev # Custom port"
  155. exit 1
  156. ;;
  157. esac