resolve path issues and eliminate code duplication

This commit is contained in:
jingrow 2025-11-14 22:38:44 +08:00
parent 9e2461af1a
commit e5629922e2

49
dev.sh
View File

@ -3,6 +3,14 @@
set -e
# 获取脚本所在目录(项目根目录)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR" || exit 1
# 项目路径常量
JINGROW_APP_DIR="apps/jingrow"
FRONTEND_DIR="$JINGROW_APP_DIR/frontend"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
@ -103,6 +111,16 @@ install_redis() {
fi
}
# 安装前端依赖
install_frontend_deps() {
local reason="${1:-前端依赖}"
info "安装$reason..."
(cd "$FRONTEND_DIR" && npm install) || {
error "$reason安装失败"
return 1
}
}
# 清理进程
cleanup() {
info "清理现有进程..."
@ -180,34 +198,33 @@ check_deps() {
check_cmd uv || exit 1
# 检查前端依赖
if [ ! -d "apps/jingrow/frontend/node_modules" ]; then
if [ ! -d "$FRONTEND_DIR/node_modules" ]; then
warn "前端依赖未安装"
info "安装前端依赖..."
cd apps/jingrow/frontend && npm install && cd ../..
install_frontend_deps "前端依赖" || exit 1
fi
# 即使 node_modules 存在,也校验 vite 是否可用(可能之前安装不完整)
if [ ! -x "apps/jingrow/frontend/node_modules/.bin/vite" ]; then
if [ ! -x "$FRONTEND_DIR/node_modules/.bin/vite" ]; then
warn "未检测到 vite可执行文件缺失重新安装前端依赖..."
(cd apps/jingrow/frontend && npm install)
install_frontend_deps "前端依赖" || exit 1
fi
# 检查后端依赖(优先使用 uv
info "同步后端依赖 (uv)..."
if [ -f "apps/jingrow/pyproject.toml" ]; then
if [ -f "$JINGROW_APP_DIR/pyproject.toml" ]; then
# 同步主项目依赖
(cd apps/jingrow && uv sync) || {
(cd "$JINGROW_APP_DIR" && uv sync) || {
error "uv 同步失败"
exit 1
}
# 安装所有节点的依赖(使用异步并行安装,提升性能)
info "安装节点依赖..."
(cd apps/jingrow && uv run python -c "from jingrow.utils.node_dependencies import install_all_nodes_dependencies; install_all_nodes_dependencies(max_concurrent=5)" 2>&1) || {
(cd "$JINGROW_APP_DIR" && uv run python -c "from jingrow.utils.node_dependencies import install_all_nodes_dependencies; install_all_nodes_dependencies(max_concurrent=5)" 2>&1) || {
warn "安装节点依赖失败,但继续执行"
}
elif [ -f "apps/jingrow/requirements.txt" ]; then
elif [ -f "$JINGROW_APP_DIR/requirements.txt" ]; then
warn "未发现 pyproject.toml使用 requirements.txt 安装"
(cd apps/jingrow && uv pip install -r requirements.txt) || {
(cd "$JINGROW_APP_DIR" && uv pip install -r requirements.txt) || {
error "uv pip 安装失败"
exit 1
}
@ -276,9 +293,9 @@ start_all() {
npx --yes concurrently \
--names "BACKEND,WORKER,FRONTEND" \
--prefix-colors "blue,yellow,green" \
"cd apps/jingrow && uv run python -m jingrow.app" \
"cd apps/jingrow && uv run python -m jingrow.dramatiq" \
"sleep 3 && cd apps/jingrow/frontend && npm run dev"
"cd $JINGROW_APP_DIR && uv run python -m jingrow.app" \
"cd $JINGROW_APP_DIR && uv run python -m jingrow.dramatiq" \
"sleep 3 && cd $FRONTEND_DIR && npm run dev"
}
# 只启动前端
@ -287,7 +304,7 @@ start_frontend() {
ensure_uv
cleanup
check_deps
cd apps/jingrow/frontend && npm run dev
(cd "$FRONTEND_DIR" && npm run dev)
}
# 只启动后端
@ -308,8 +325,8 @@ start_backend() {
npx --yes concurrently \
--names "BACKEND,WORKER" \
--prefix-colors "blue,yellow" \
"cd apps/jingrow && uv run python -m jingrow.app" \
"cd apps/jingrow && uv run python -m jingrow.dramatiq"
"cd $JINGROW_APP_DIR && uv run python -m jingrow.app" \
"cd $JINGROW_APP_DIR && uv run python -m jingrow.dramatiq"
}
# 停止服务