finance-calculator/scripts/setup.sh
2025-06-14 01:33:52 +08:00

121 lines
2.8 KiB
Bash
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_info() {
echo -e "${BLUE} $1${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
# 检查命令是否存在
command_exists() {
command -v "$1" >/dev/null 2>&1
}
print_info "🚀 开始设置个人理财计算器..."
# 检查必要的命令
print_info "检查必要的依赖..."
if ! command_exists node; then
print_error "Node.js 未安装,请先安装 Node.js"
exit 1
fi
if ! command_exists pnpm; then
print_warning "pnpm 未安装,将使用 npm 代替"
PACKAGE_MANAGER="npm"
else
PACKAGE_MANAGER="pnpm"
print_success "发现 pnpm"
fi
# 安装依赖
print_info "安装项目依赖..."
if [ "$PACKAGE_MANAGER" = "pnpm" ]; then
pnpm install
else
npm install
fi
if [ $? -eq 0 ]; then
print_success "依赖安装完成"
else
print_error "依赖安装失败"
exit 1
fi
# 创建环境变量文件
print_info "设置环境变量..."
if [ ! -f .env.local ]; then
if [ -f env.example ]; then
cp env.example .env.local
print_success "已创建 .env.local 文件"
print_warning "请编辑 .env.local 文件,填入您的配置信息"
else
print_error "找不到 env.example 文件"
fi
else
print_info ".env.local 文件已存在,跳过创建"
fi
# 生成 NextAuth 密钥
print_info "生成 NextAuth 密钥..."
if command_exists openssl; then
NEXTAUTH_SECRET=$(openssl rand -base64 32)
if [ -f .env.local ]; then
if grep -q "NEXTAUTH_SECRET=" .env.local; then
sed -i.bak "s/NEXTAUTH_SECRET=.*/NEXTAUTH_SECRET=\"$NEXTAUTH_SECRET\"/" .env.local
else
echo "NEXTAUTH_SECRET=\"$NEXTAUTH_SECRET\"" >> .env.local
fi
print_success "NextAuth 密钥已生成"
fi
else
print_warning "openssl 未找到,请手动设置 NEXTAUTH_SECRET"
fi
# Prisma 设置
print_info "设置 Prisma..."
if [ "$PACKAGE_MANAGER" = "pnpm" ]; then
pnpm db:generate
else
npm run db:generate
fi
if [ $? -eq 0 ]; then
print_success "Prisma 客户端已生成"
else
print_warning "Prisma 客户端生成失败,请检查数据库配置"
fi
# 完成提示
echo ""
print_success "🎉 设置完成!"
echo ""
print_info "接下来的步骤:"
echo "1. 编辑 .env.local 文件,配置数据库和其他服务"
echo "2. 启动 PostgreSQL 数据库"
echo "3. 运行数据库迁移: ${PACKAGE_MANAGER} run db:push"
echo "4. 启动开发服务器: ${PACKAGE_MANAGER} run dev"
echo ""
print_info "访问 http://localhost:3000 查看应用"
echo ""
print_info "更多信息请查看 README.md 文件"