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
133 lines
5.4 KiB
JavaScript
133 lines
5.4 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* init.js - 容器初始化脚本(Node.js 版)
|
||
*
|
||
* 职责(全部幂等,可重复执行):
|
||
* 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)失败用 try/catch 兜底
|
||
* - 不删任何文件,幂等追加 alias(先 filter 清掉旧的同名行)
|
||
*
|
||
* v16 变更(2026-06-15):
|
||
* - 移除 @playwright/cli 包依赖(彻底废弃)
|
||
* - 统一用 playwright Node.js 库(npm `playwright` 包)的 playwright-core 子模块
|
||
* - AI session 模式走本地 wrapper:`$ccWorkDir/.claude/hooks/playwright-ai-cli.js`
|
||
* (5 行复刻 @playwright/cli:require playwright-core/lib/tools/cli-client/program + program())
|
||
* - /usr/local/bin/playwright → 软链到 wrapper,覆盖 npm 标准 bin(标准 test/codegen 用 npx)
|
||
* - 验证:`playwright --version` 返 1.x;`playwright -s=X open URL` session 命令工作
|
||
*/
|
||
|
||
const { execSync, spawnSync } = require('child_process');
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
const CCWORKDIR = '/home/ubuntu/ccWorkDir';
|
||
const HOME = process.env.HOME || '/root';
|
||
const BASHRC = path.join(HOME, '.bashrc');
|
||
const DACLI_SRC = path.join(CCWORKDIR, 'dacli/src/cli.ts');
|
||
const DACLI_LINK = '/usr/local/bin/dacli';
|
||
const PLAYWRIGHT_LIB_DIR = '/usr/lib/node_modules/playwright';
|
||
const PLAYWRIGHT_BIN = path.join(PLAYWRIGHT_LIB_DIR, 'cli.js');
|
||
const PLAYWRIGHT_LINK = '/usr/local/bin/playwright';
|
||
const PLAYWRIGHT_AI_CLI = path.join(CCWORKDIR, '.claude/hooks/playwright-ai-cli.js');
|
||
|
||
const log = (msg) => console.log(`[init.js] ${msg}`);
|
||
|
||
const tryExec = (cmd) => {
|
||
try {
|
||
return execSync(cmd, { stdio: 'pipe' }).toString().trim();
|
||
} catch {
|
||
return null;
|
||
}
|
||
};
|
||
|
||
// ==================== 1. dacli 命令 ====================
|
||
log('① 配置 dacli 命令 (TS 版)');
|
||
if (fs.existsSync(BASHRC)) {
|
||
try {
|
||
const lines = fs.readFileSync(BASHRC, 'utf8').split('\n')
|
||
.filter(l => !l.startsWith('# dacli alias (JNPF CLI)') && !l.startsWith('alias dacli='));
|
||
fs.writeFileSync(BASHRC, lines.join('\n'));
|
||
} catch (e) {
|
||
log(` ⚠️ 清理旧 alias 失败: ${e.message}`);
|
||
}
|
||
}
|
||
if (fs.existsSync(DACLI_SRC)) {
|
||
const wrapper = `#!/bin/bash\nexec npx --prefix "${CCWORKDIR}/dacli" tsx "${DACLI_SRC}" "$@"\n`;
|
||
fs.writeFileSync(DACLI_LINK, wrapper, { mode: 0o755 });
|
||
log(` ✅ TS 版 wrapper: ${DACLI_LINK} → npx tsx ${DACLI_SRC}`);
|
||
log(` 💡 首次使用需 cd ${CCWORKDIR}/dacli && npm install`);
|
||
} else {
|
||
log(` ⚠️ 找不到 TS 版入口: ${DACLI_SRC}`);
|
||
}
|
||
log(` ✅ ${tryExec('command -v dacli') || '(未在 PATH 中)'}`);
|
||
|
||
// ==================== 2. playwright Node.js 库 ====================
|
||
log('② 检查 playwright Node.js 库 (npm `playwright`)');
|
||
if (fs.existsSync(PLAYWRIGHT_BIN)) {
|
||
const version = tryExec('playwright --version') || tryExec('npx playwright --version') || '(unknown)';
|
||
log(` ✅ 已存在: ${PLAYWRIGHT_BIN} (${version})`);
|
||
} else {
|
||
log(' ⏳ 安装 playwright Node.js 库 ...');
|
||
const r = spawnSync('npm', ['install', '-g', 'playwright'], { stdio: 'ignore' });
|
||
if (r.status === 0) {
|
||
const version = tryExec('playwright --version') || tryExec('npx playwright --version') || '';
|
||
log(` ✅ 安装完成 (${version})`);
|
||
} else {
|
||
log(' ⚠️ 安装失败(继续,后续步骤不阻塞)');
|
||
}
|
||
}
|
||
|
||
// ==================== 2.5. playwright bin 软链兜底 ====================
|
||
// 优先指向本地 AI-friendly wrapper(session 模式),找不到时 fallback 到 npm 标准 bin
|
||
log('②.5 检查 playwright bin 软链 /usr/local/bin/playwright');
|
||
const ensureSymlink = (target) => {
|
||
// 幂等:先尝试 unlink 旧软链/文件(无则忽略)
|
||
try { fs.unlinkSync(PLAYWRIGHT_LINK); } catch {}
|
||
try {
|
||
fs.symlinkSync(target, PLAYWRIGHT_LINK);
|
||
return true;
|
||
} catch (e) {
|
||
log(` ⚠️ 软链失败: ${e.message}`);
|
||
return false;
|
||
}
|
||
};
|
||
if (fs.existsSync(PLAYWRIGHT_AI_CLI)) {
|
||
if (ensureSymlink(PLAYWRIGHT_AI_CLI)) {
|
||
log(` ✅ 软链已建: ${PLAYWRIGHT_LINK} → ${PLAYWRIGHT_AI_CLI}`);
|
||
log(` (AI wrapper: playwright -s=X open URL / snapshot / click / fill / eval / close)`);
|
||
}
|
||
} else if (fs.existsSync(PLAYWRIGHT_BIN)) {
|
||
if (ensureSymlink(PLAYWRIGHT_BIN)) {
|
||
log(` ⚠️ AI wrapper 不存在, fallback 到 npm 标准 bin(无 session 模式)`);
|
||
log(` 缺: ${PLAYWRIGHT_AI_CLI}`);
|
||
}
|
||
} else {
|
||
log(` ⚠️ 找不到 playwright bin(e2e 技能可能不可用)`);
|
||
}
|
||
|
||
// ==================== 3. Chrome 检测/安装 ====================
|
||
log('③ 检查 google-chrome');
|
||
const chromeVer = tryExec('google-chrome --version');
|
||
if (chromeVer) {
|
||
log(` ✅ ${chromeVer}`);
|
||
} else {
|
||
log(' ⏳ 安装 Chrome (npx playwright install chrome) ...');
|
||
const r = spawnSync('npx', ['--yes', 'playwright', 'install', 'chrome'], { stdio: 'ignore' });
|
||
if (r.status === 0) {
|
||
const ver = tryExec('google-chrome --version') || '已装';
|
||
log(` ✅ Chrome 已装: ${ver}`);
|
||
} else {
|
||
log(' ⚠️ Chrome 安装失败(不影响 JNPF 启动)');
|
||
}
|
||
}
|
||
|
||
log('🎉 init 完成,移交 CMD 后续步骤');
|
||
process.exit(0);
|