docs(quick-2): Update start.sh for network access and port config

- Dev/preview servers accessible from other machines (--host flag)
- Port configuration via menu or CLI args
- dev:8080 and dev:9090 shortcuts for kiosk/remote modes
- Shows connection info (local + network IP)
This commit is contained in:
2026-03-13 11:05:59 +08:00
parent b474f7067a
commit e327ab9ac8
4 changed files with 198 additions and 23 deletions

138
start.sh
View File

@@ -17,6 +17,10 @@ BOLD='\033[1m'
# Project directory
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/sample_interface" && pwd)"
# Default ports
DEV_PORT=5173
PREVIEW_PORT=4173
echo -e "${CYAN}${BOLD}"
echo "╔══════════════════════════════════════════════════════╗"
echo "║ TCK RTU - Development Menu ║"
@@ -26,60 +30,141 @@ echo -e "${NC}"
show_menu() {
echo -e "${BOLD}Select an option:${NC}"
echo ""
echo -e " ${GREEN}1)${NC} dev Start development server"
echo -e " ${GREEN}2)${NC} test Run tests"
echo -e " ${GREEN}3)${NC} test:watch Run tests in watch mode"
echo -e " ${GREEN}4)${NC} build Build for production"
echo -e " ${GREEN}5)${NC} preview Preview production build"
echo -e " ${GREEN}6)${NC} lint Run linter"
echo -e " ${GREEN}7)${NC} clean Clean build artifacts"
echo -e " ${GREEN}8)${NC} install Install dependencies"
echo -e " ${GREEN}1)${NC} dev Start development server (port $DEV_PORT)"
echo -e " ${GREEN}2)${NC} dev:8080 Start dev server on port 8080 (kiosk mode)"
echo -e " ${GREEN}3)${NC} dev:9090 Start dev server on port 9090 (remote mode)"
echo -e " ${GREEN}4)${NC} test Run tests"
echo -e " ${GREEN}5)${NC} test:watch Run tests in watch mode"
echo -e " ${GREEN}6)${NC} build Build for production"
echo -e " ${GREEN}7)${NC} preview Preview production (port $PREVIEW_PORT)"
echo -e " ${GREEN}8)${NC} lint Run linter"
echo -e " ${GREEN}9)${NC} clean Clean build artifacts"
echo -e " ${GREEN}10)${NC} install Install dependencies"
echo -e " ${GREEN}p)${NC} port Change default port"
echo ""
echo -e " ${RED}0)${NC} Exit"
echo ""
}
get_ip_address() {
# Try to get the primary IP address
if command -v ip &> /dev/null; then
IP=$(ip route get 1.1.1.1 2>/dev/null | grep -oP 'src \K[^ ]+' | head -1)
elif command -v ifconfig &> /dev/null; then
IP=$(ifconfig 2>/dev/null | grep -oP 'inet \K[\d.]+' | grep -v '127.0.0.1' | head -1)
fi
if [ -z "$IP" ]; then
IP="localhost"
fi
echo "$IP"
}
show_connection_info() {
local port=$1
local IP=$(get_ip_address)
echo ""
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN} Server running at:${NC}"
echo -e "${GREEN} - Local: http://localhost:$port${NC}"
echo -e "${GREEN} - Network: http://$IP:$port${NC}"
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
}
run_dev_server() {
local port=${1:-$DEV_PORT}
cd "$PROJECT_DIR"
echo -e "${BLUE}Starting development server on port $port...${NC}"
echo -e "${YELLOW}(Press Ctrl+C to stop)${NC}"
echo ""
VITE_PORT=$port npm run dev -- --host &
DEV_PID=$!
show_connection_info $port
wait $DEV_PID
}
run_preview_server() {
local port=${1:-$PREVIEW_PORT}
cd "$PROJECT_DIR"
echo -e "${BLUE}Starting preview server on port $port...${NC}"
echo -e "${YELLOW}(Press Ctrl+C to stop)${NC}"
echo ""
VITE_PORT=$port npm run preview -- --host &
PREVIEW_PID=$!
show_connection_info $port
wait $PREVIEW_PID
}
change_port() {
echo -e "${BOLD}Change Default Port${NC}"
echo ""
echo -e "Current: Dev=$DEV_PORT, Preview=$PREVIEW_PORT"
echo ""
echo -n "Enter new dev port (current: $DEV_PORT): "
read -r new_dev_port
if [ -n "$new_dev_port" ]; then
DEV_PORT=$new_dev_port
fi
echo -n "Enter new preview port (current: $PREVIEW_PORT): "
read -r new_preview_port
if [ -n "$new_preview_port" ]; then
PREVIEW_PORT=$new_preview_port
fi
echo -e "${GREEN}Ports updated! Dev=$DEV_PORT, Preview=$PREVIEW_PORT${NC}"
echo ""
}
run_command() {
local option=$1
cd "$PROJECT_DIR"
case $option in
1|dev)
echo -e "${BLUE}Starting development server...${NC}"
npm run dev
run_dev_server $DEV_PORT
;;
2|test)
2|dev:8080)
run_dev_server 8080
;;
3|dev:9090)
run_dev_server 9090
;;
4|test)
echo -e "${BLUE}Running tests...${NC}"
npm test
;;
3|test:watch)
5|test:watch)
echo -e "${BLUE}Running tests in watch mode...${NC}"
npm run test:watch
;;
4|build)
6|build)
echo -e "${BLUE}Building for production...${NC}"
npm run build
echo -e "${GREEN}Build complete!${NC}"
;;
5|preview)
echo -e "${BLUE}Previewing production build...${NC}"
npm run preview
7|preview)
run_preview_server $PREVIEW_PORT
;;
6|lint)
8|lint)
echo -e "${BLUE}Running linter...${NC}"
npm run lint
;;
7|clean)
9|clean)
echo -e "${YELLOW}Cleaning build artifacts...${NC}"
rm -rf "$PROJECT_DIR/dist"
rm -rf "$PROJECT_DIR/node_modules/.vite"
echo -e "${GREEN}Clean complete!${NC}"
;;
8|install)
10|install)
echo -e "${BLUE}Installing dependencies...${NC}"
npm install
echo -e "${GREEN}Dependencies installed!${NC}"
;;
p|port)
change_port
;;
0|exit)
echo -e "${GREEN}Goodbye!${NC}"
exit 0
@@ -97,16 +182,25 @@ if [ ! -d "$PROJECT_DIR/node_modules" ]; then
npm install
fi
# If arguments provided, run directly
# Parse command line arguments
if [ $# -gt 0 ]; then
run_command "$1"
# Check for port override
if [[ "$1" == dev:* ]]; then
port="${1#dev:}"
run_dev_server "$port"
elif [[ "$1" == preview:* ]]; then
port="${1#preview:}"
run_preview_server "$port"
else
run_command "$1"
fi
exit 0
fi
# Interactive menu
while true; do
show_menu
echo -n -e "${BOLD}Enter your choice (0-8): ${NC}"
echo -n -e "${BOLD}Enter your choice (0-10 or p): ${NC}"
read -r choice
echo ""
run_command "$choice"