Files
2026-08-24 22:36:17 +08:00

57 lines
2.0 KiB
Bash
Executable File
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/zsh
# 双击本文件,或在终端执行:./start.command
# 流程:停掉占用 3300 的旧后端 → 重新 build 插件 → 启动后端(http://127.0.0.1:3300
cd "$(dirname "$0")"
# ── 1) 停掉占用 3300 端口的旧后端(上次残留的服务会导致 address already in use)──
listen_pids() { lsof -tnP -iTCP:3300 -sTCP:LISTEN 2>/dev/null; }
if [[ -n "$(listen_pids)" ]]; then
echo "端口 3300 被旧服务占用(PID: $(listen_pids | tr '\n' ' ')),正在停止…"
listen_pids | xargs kill 2>/dev/null
for _ in {1..10}; do # 等待优雅退出,最多 5s
[[ -z "$(listen_pids)" ]] && break
sleep 0.5
done
if [[ -n "$(listen_pids)" ]]; then
echo "旧服务未响应退出信号,强制结束…"
listen_pids | xargs kill -9 2>/dev/null
sleep 1
fi
echo "端口 3300 已释放"
fi
# ── 2) 重新 build 插件(产物在 extension/.output/chrome-mv3)──
if command -v pnpm >/dev/null 2>&1; then
echo "正在重新 build 插件…"
(
cd extension || exit 1
[[ -d node_modules ]] || pnpm install
pnpm run build
) || echo "⚠️ 插件 build 失败,后端照常启动(可稍后手动执行:cd extension && pnpm run build"
echo "提示:build 后需在 chrome://extensions 重新加载插件,并刷新已打开的商品页"
else
echo "⚠️ 未找到 pnpm,跳过插件 build"
fi
if [[ ! -d server/.venv ]]; then
echo "未找到 server/.venv,正在创建并安装依赖…"
python3 -m venv server/.venv || exit 1
fi
# 每次启动同步依赖:代码新增依赖(如 Pillow)装上即可用,已满足时秒过
server/.venv/bin/pip install -q -r server/requirements.txt || exit 1
if [[ ! -f .env ]]; then
echo "未找到 .env,已从 .env.example 复制,请填入 API Key 后再启动。"
cp .env.example .env
echo "按回车关闭…"
read -r
exit 1
fi
echo
echo "启动中:http://127.0.0.1:3300 (插件保持默认后端地址即可)"
echo "按 Ctrl+C 可停止服务"
echo
exec server/.venv/bin/python server/main.py