Files
aiapply/install.sh
2025-09-06 08:28:47 +08:00

303 lines
7.7 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# PromptForge 自动化安装脚本
# 适用于 Linux 和 macOS 系统
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_message() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE} PromptForge 自动化安装脚本${NC}"
echo -e "${BLUE}================================${NC}"
}
# 检查系统要求
check_system_requirements() {
print_message "检查系统要求..."
# 检查操作系统
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
print_message "检测到 Linux 系统"
elif [[ "$OSTYPE" == "darwin"* ]]; then
print_message "检测到 macOS 系统"
else
print_error "不支持的操作系统: $OSTYPE"
exit 1
fi
# 检查内存
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
MEMORY=$(free -m | awk 'NR==2{printf "%.0f", $2/1024}')
else
MEMORY=$(sysctl -n hw.memsize | awk '{printf "%.0f", $1/1024/1024/1024}')
fi
if [ "$MEMORY" -lt 4 ]; then
print_warning "内存不足,推荐至少 4GB RAM当前: ${MEMORY}GB"
else
print_message "内存检查通过: ${MEMORY}GB"
fi
# 检查磁盘空间
DISK_SPACE=$(df -BG . | awk 'NR==2{print $4}' | sed 's/G//')
if [ "$DISK_SPACE" -lt 2 ]; then
print_warning "磁盘空间不足,推荐至少 2GB当前: ${DISK_SPACE}GB"
else
print_message "磁盘空间检查通过: ${DISK_SPACE}GB"
fi
}
# 检查 Node.js
check_nodejs() {
print_message "检查 Node.js..."
if command -v node &> /dev/null; then
NODE_VERSION=$(node --version | sed 's/v//')
NODE_MAJOR=$(echo $NODE_VERSION | cut -d. -f1)
if [ "$NODE_MAJOR" -ge 18 ]; then
print_message "Node.js 版本检查通过: v$NODE_VERSION"
else
print_error "Node.js 版本过低,需要 v18.0.0 或更高版本,当前: v$NODE_VERSION"
install_nodejs
fi
else
print_warning "Node.js 未安装"
install_nodejs
fi
if command -v npm &> /dev/null; then
NPM_VERSION=$(npm --version)
print_message "npm 版本: v$NPM_VERSION"
else
print_error "npm 未安装"
exit 1
fi
}
# 安装 Node.js
install_nodejs() {
print_message "安装 Node.js..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux 安装
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS 安装
if command -v brew &> /dev/null; then
brew install node
else
print_error "请先安装 Homebrew: https://brew.sh/"
exit 1
fi
fi
print_message "Node.js 安装完成"
}
# 检查 MySQL
check_mysql() {
print_message "检查 MySQL..."
if command -v mysql &> /dev/null; then
MYSQL_VERSION=$(mysql --version | awk '{print $5}' | sed 's/,//')
print_message "MySQL 版本检查通过: $MYSQL_VERSION"
else
print_warning "MySQL 未安装"
install_mysql
fi
}
# 安装 MySQL
install_mysql() {
print_message "安装 MySQL..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux 安装
sudo apt update
sudo apt install -y mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS 安装
if command -v brew &> /dev/null; then
brew install mysql
brew services start mysql
else
print_error "请先安装 Homebrew: https://brew.sh/"
exit 1
fi
fi
print_message "MySQL 安装完成"
}
# 检查 Git
check_git() {
print_message "检查 Git..."
if command -v git &> /dev/null; then
GIT_VERSION=$(git --version | awk '{print $3}')
print_message "Git 版本检查通过: $GIT_VERSION"
else
print_warning "Git 未安装"
install_git
fi
}
# 安装 Git
install_git() {
print_message "安装 Git..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sudo apt update
sudo apt install -y git
elif [[ "$OSTYPE" == "darwin"* ]]; then
if command -v brew &> /dev/null; then
brew install git
else
print_error "请先安装 Homebrew: https://brew.sh/"
exit 1
fi
fi
print_message "Git 安装完成"
}
# 安装项目依赖
install_dependencies() {
print_message "安装项目依赖..."
if [ -f "package.json" ]; then
npm install
print_message "项目依赖安装完成"
else
print_error "package.json 文件不存在"
exit 1
fi
}
# 配置环境变量
setup_environment() {
print_message "配置环境变量..."
if [ ! -f ".env.local" ]; then
if [ -f ".env.example" ]; then
cp .env.example .env.local
print_message "已创建 .env.local 文件"
print_warning "请编辑 .env.local 文件,填入您的配置信息"
else
print_warning ".env.example 文件不存在,请手动创建 .env.local 文件"
fi
else
print_message ".env.local 文件已存在"
fi
}
# 设置数据库
setup_database() {
print_message "设置数据库..."
# 检查数据库脚本是否存在
if [ -f "create-tables-final.js" ]; then
print_message "创建数据库表..."
node create-tables-final.js
else
print_warning "数据库脚本不存在,跳过数据库设置"
fi
if [ -f "create-template-data.js" ]; then
print_message "创建测试数据..."
node create-template-data.js
else
print_warning "测试数据脚本不存在,跳过数据创建"
fi
}
# 验证安装
verify_installation() {
print_message "验证安装..."
# 检查关键文件
FILES=("package.json" "next.config.js" "src/app/layout.tsx")
for file in "${FILES[@]}"; do
if [ -f "$file" ]; then
print_message "$file 存在"
else
print_warning "$file 不存在"
fi
done
# 检查依赖
if [ -d "node_modules" ]; then
print_message "✓ node_modules 目录存在"
else
print_error "✗ node_modules 目录不存在"
fi
# 类型检查
if npm run type-check &> /dev/null; then
print_message "✓ TypeScript 类型检查通过"
else
print_warning "⚠ TypeScript 类型检查失败"
fi
}
# 显示后续步骤
show_next_steps() {
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE} 安装完成!后续步骤:${NC}"
echo -e "${BLUE}================================${NC}"
echo ""
echo "1. 编辑 .env.local 文件,配置数据库和 API 密钥"
echo "2. 启动开发服务器: npm run dev"
echo "3. 访问 http://localhost:3000"
echo "4. 注册新账户并开始使用"
echo ""
echo "📖 更多信息请查看:"
echo " - 安装指南: INSTALLATION_GUIDE.md"
echo " - 环境配置: ENV_SETUP_GUIDE.md"
echo " - 模板教程: TEMPLATE_TUTORIAL.md"
echo ""
echo "🚀 祝您使用愉快!"
}
# 主函数
main() {
print_header
check_system_requirements
check_nodejs
check_mysql
check_git
install_dependencies
setup_environment
setup_database
verify_installation
show_next_steps
}
# 运行主函数
main "$@"