Files
claude-config/hooks/init.sh
T
ssw fee91b6ccc v16: 迁移 playwright-cli → playwright Node.js 包 + AI wrapper
playwright-cli (2026-06-15 @playwright/cli 废弃):
- init.js / init.sh 移除 @playwright/cli 安装+软链步骤
- npm install -g playwright (Node.js 包) 替代 @playwright/cli
- 本地 5 行 wrapper .claude/hooks/playwright-ai-cli.js 复刻 @playwright/cli
  (用 playwright-core/lib/tools/cli-client/program 入口, 零额外依赖)
- /usr/local/bin/playwright → 软链到 wrapper
- 标准 test/codegen 走 npx playwright (npm bin 仍可用)

文档/命令:
- CLAUDE.md, ssw-test.md, ssw-h5dev-flow.md, ssw-e2e-test/SKILL.md,
  troubleshooting, api-test 全部 playwright-cli → playwright
- auto-push-on-stop.sh JUNK_PATTERNS 加 .playwright/ 目录过滤

验证 (per 用户"注意验证有效性"):
- node init.js / bash init.sh 幂等通过 (再跑也 OK)
- playwright --version 返 1.60.0
- playwright -s=smoke open/snapshot/click/close 全部工作
- snapshot YAML 输出含 ref (e1/e2/e3), AI 可直接用
- 标准 CLI 仍可达: npx playwright --version
2026-06-15 11:56:39 +08:00

95 lines
4.0 KiB
Bash
Executable File
Raw 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
# init.sh - 容器初始化脚本(替代 SessionStart 的两个 hook)
# 职责(全部幂等,可重复执行):
# 1. 把 dacli 可执行入口放到 /usr/local/bin/dacli(TS 版 wrapper)
# 2. 保证 playwright Node.js 库(npm `playwright`)已安装 + bin 软链
# 3. 保证 google-chrome 已就绪(playwright install chrome 兜底)
#
# 触发:Dockerfile CMD 链式调用,容器首次启动时执行一次
# 输出:每步一行状态(容器 stdout,docker logs 可看)
#
# 设计:
# - 不阻塞 JNPF 启动:网络操作(npm install / playwright install)失败用 || true 兜底
# - 不删任何文件,幂等追加 alias(先 sed 清掉旧的同名行)
#
# v16 变更(2026-06-15):
# - 移除 @playwright/cli + 软链步骤(彻底废弃 @playwright/cli 包)
# - 统一用 playwright Node.js 库(npm `playwright`)的 bin
# - AI/skill 引用:`playwright-cli` → `playwright`(保持 -s=dacli session 共享模式)
set -u # 不用 -e:单步失败要继续后续步骤
CCWORKDIR="/home/ubuntu/ccWorkDir"
BASHRC="$HOME/.bashrc"
DACLI_SRC="$CCWORKDIR/dacli/src/cli.ts"
PLAYWRIGHT_LIB_DIR="/usr/lib/node_modules/playwright"
PLAYWRIGHT_BIN="$PLAYWRIGHT_LIB_DIR/cli.js"
PLAYWRIGHT_LINK="/usr/local/bin/playwright"
log() { echo "[init.sh] $*"; }
# ============ 1. dacli 命令(TS 版 npx tsx wrapper,PATH 自动命中) ============
log "① 配置 dacli 命令 (TS 版)"
DACLI_LINK="/usr/local/bin/dacli"
# 顺手清掉旧 alias(避免 .bashrc 残留干扰 type/which 输出)
if [ -f "$BASHRC" ]; then
sed -i '/^# dacli alias (JNPF CLI)/d; /^alias dacli=/d' "$BASHRC"
fi
# TS 版:通过 npx tsx 调 cli.ts,写 wrapper 脚本到 /usr/local/bin
if [ -f "$DACLI_SRC" ]; then
cat > "$DACLI_LINK" <<EOF
#!/bin/bash
exec npx --prefix "$CCWORKDIR/dacli" tsx "$DACLI_SRC" "\$@"
EOF
chmod +x "$DACLI_LINK"
log " ✅ TS 版 wrapper: $DACLI_LINK → npx tsx $DACLI_SRC"
log " 💡 首次使用需 cd $CCWORKDIR/dacli && npm install"
else
log " ⚠️ 找不到 TS 版入口: $DACLI_SRC(应位于 \$ccWorkDir/dacli/)"
fi
log " ✅ $(command -v dacli 2>/dev/null || echo '(未在 PATH 中)')"
# ============ 2. playwright Node.js 库 检测/安装 ============
log "② 检查 playwright Node.js 库 (npm \`playwright\`)"
if [ -f "$PLAYWRIGHT_BIN" ]; then
log " ✅ 已存在: $PLAYWRIGHT_BIN"
else
log " ⏳ 安装 playwright Node.js 库 ..."
npm install -g playwright >/dev/null 2>&1 \
&& log " ✅ 安装完成" \
|| log " ⚠️ 安装失败(继续,后续步骤不阻塞)"
fi
# ============ 2.5. playwright bin 软链兜底 ============
# 优先指向本地 AI-friendly wrapper(session 模式),找不到时 fallback 到 npm 标准 bin
PLAYWRIGHT_AI_CLI="$CCWORKDIR/.claude/hooks/playwright-ai-cli.js"
log "②.5 检查 playwright bin 软链 /usr/local/bin/playwright"
if [ -f "$PLAYWRIGHT_AI_CLI" ]; then
ln -sf "$PLAYWRIGHT_AI_CLI" "$PLAYWRIGHT_LINK" 2>/dev/null \
&& log " ✅ 软链已建: $PLAYWRIGHT_LINK → $PLAYWRIGHT_AI_CLI" \
&& log " (AI wrapper: playwright -s=X open URL / snapshot / click / fill / eval / close)" \
|| log " ⚠️ 软链失败"
elif [ -f "$PLAYWRIGHT_BIN" ]; then
ln -sf "$PLAYWRIGHT_BIN" "$PLAYWRIGHT_LINK" 2>/dev/null \
&& log " ⚠️ AI wrapper 不存在, fallback 到 npm 标准 bin(无 session 模式)" \
&& log " 缺: $PLAYWRIGHT_AI_CLI" \
|| log " ⚠️ 软链失败"
else
log " ⚠️ 找不到 playwright bin(e2e 技能可能不可用)"
fi
# ============ 3. Chrome 检测/安装 ============
log "③ 检查 google-chrome"
if command -v google-chrome >/dev/null 2>&1; then
VER=$(google-chrome --version 2>/dev/null | awk '{print $3}')
log " ✅ chrome $VER"
else
log " ⏳ 安装 Chrome ..."
npx --yes playwright install chrome >/dev/null 2>&1 \
&& log " ✅ Chrome 已装" \
|| log " ⚠️ Chrome 安装失败(不影响 JNPF 启动)"
fi
log "🎉 init 完成,移交 CMD 后续步骤"
exit 0