214 lines
4.7 KiB
Bash
214 lines
4.7 KiB
Bash
#!/bin/bash
|
|
|
|
# 药箱启动脚本 (Bash)
|
|
# 用法: ./start.sh [命令]
|
|
|
|
set -e
|
|
|
|
# 颜色定义
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
CYAN='\033[0;36m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# 颜色函数
|
|
write_success() { echo -e "${GREEN}$1${NC}"; }
|
|
write_error() { echo -e "${RED}$1${NC}"; }
|
|
write_info() { echo -e "${BLUE}$1${NC}"; }
|
|
write_warning() { echo -e "${YELLOW}$1${NC}"; }
|
|
write_header() { echo -e "${CYAN}$1${NC}"; }
|
|
|
|
# 检查 Node.js
|
|
check_node() {
|
|
if command -v node &> /dev/null; then
|
|
local version=$(node --version)
|
|
write_success "Node.js $version 已安装"
|
|
return 0
|
|
else
|
|
write_error "未安装 Node.js,请先安装"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# 检查 Python
|
|
check_python() {
|
|
if command -v python3 &> /dev/null; then
|
|
local version=$(python3 --version)
|
|
write_success "Python $version 已安装"
|
|
return 0
|
|
elif command -v python &> /dev/null; then
|
|
local version=$(python --version)
|
|
write_success "Python $version 已安装"
|
|
return 0
|
|
else
|
|
write_error "未安装 Python,请先安装"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# 获取 Python 命令
|
|
get_python() {
|
|
if command -v python3 &> /dev/null; then
|
|
echo "python3"
|
|
elif command -v python &> /dev/null; then
|
|
echo "python"
|
|
else
|
|
write_error "未找到 Python"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# 安装前端依赖
|
|
install_frontend() {
|
|
write_info "安装前端依赖..."
|
|
cd frontend
|
|
npm install
|
|
cd ..
|
|
write_success "前端依赖安装完成"
|
|
}
|
|
|
|
# 安装后端依赖
|
|
install_backend() {
|
|
write_info "安装后端依赖..."
|
|
cd backend
|
|
|
|
local python_cmd=$(get_python)
|
|
|
|
if [ ! -d "venv" ]; then
|
|
write_info "创建虚拟环境..."
|
|
$python_cmd -m venv venv
|
|
fi
|
|
|
|
source venv/bin/activate
|
|
pip install -r requirements.txt
|
|
cd ..
|
|
write_success "后端依赖安装完成"
|
|
}
|
|
|
|
# 安装所有依赖
|
|
install_all() {
|
|
install_frontend
|
|
install_backend
|
|
}
|
|
|
|
# 启动前端
|
|
start_frontend() {
|
|
write_info "启动前端开发服务器..."
|
|
cd frontend
|
|
npm run dev &
|
|
cd ..
|
|
write_success "前端服务器启动中: http://localhost:5173"
|
|
}
|
|
|
|
# 启动后端
|
|
start_backend() {
|
|
write_info "启动后端开发服务器..."
|
|
cd backend
|
|
|
|
if [ ! -d "venv" ]; then
|
|
install_backend
|
|
fi
|
|
|
|
source venv/bin/activate
|
|
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 &
|
|
cd ..
|
|
write_success "后端服务器启动中: http://localhost:8000"
|
|
}
|
|
|
|
# 启动所有服务
|
|
start_all() {
|
|
start_backend
|
|
sleep 2
|
|
start_frontend
|
|
write_success "所有服务已启动"
|
|
write_info "前端: http://localhost:5173"
|
|
write_info "后端: http://localhost:8000"
|
|
write_info "API 文档: http://localhost:8000/docs"
|
|
|
|
# 等待用户中断
|
|
echo ""
|
|
write_warning "按 Ctrl+C 停止所有服务"
|
|
wait
|
|
}
|
|
|
|
# 构建前端
|
|
build_frontend() {
|
|
write_info "构建前端生产版本..."
|
|
cd frontend
|
|
npm run build
|
|
cd ..
|
|
write_success "构建完成,输出目录: frontend/dist"
|
|
}
|
|
|
|
# 清理缓存
|
|
clean_cache() {
|
|
write_info "清理缓存..."
|
|
|
|
if [ -d "frontend/node_modules/.cache" ]; then
|
|
rm -rf frontend/node_modules/.cache
|
|
fi
|
|
|
|
write_success "缓存清理完成"
|
|
}
|
|
|
|
# 显示帮助
|
|
show_help() {
|
|
write_header "药箱启动脚本"
|
|
write_header "========================"
|
|
echo ""
|
|
write_info "可用命令:"
|
|
echo " install - 安装所有依赖"
|
|
echo " install:f - 安装前端依赖"
|
|
echo " install:b - 安装后端依赖"
|
|
echo " start - 启动所有服务"
|
|
echo " start:f - 启动前端服务"
|
|
echo " start:b - 启动后端服务"
|
|
echo " build - 构建前端生产版本"
|
|
echo " clean - 清理缓存"
|
|
echo " help - 显示帮助"
|
|
echo ""
|
|
write_info "示例:"
|
|
echo " ./start.sh install # 安装所有依赖"
|
|
echo " ./start.sh start # 启动所有服务"
|
|
echo " ./start.sh build # 构建生产版本"
|
|
}
|
|
|
|
# 主逻辑
|
|
COMMAND="${1:-help}"
|
|
|
|
case "$COMMAND" in
|
|
install)
|
|
install_all
|
|
;;
|
|
install:f|install:frontend)
|
|
install_frontend
|
|
;;
|
|
install:b|install:backend)
|
|
install_backend
|
|
;;
|
|
start)
|
|
start_all
|
|
;;
|
|
start:f|start:frontend)
|
|
start_frontend
|
|
;;
|
|
start:b|start:backend)
|
|
start_backend
|
|
;;
|
|
build|build:f|build:frontend)
|
|
build_frontend
|
|
;;
|
|
clean)
|
|
clean_cache
|
|
;;
|
|
help|?)
|
|
show_help
|
|
;;
|
|
*)
|
|
write_error "未知命令: $COMMAND"
|
|
show_help
|
|
exit 1
|
|
;;
|
|
esac |