import { execFileSync, spawnSync } from 'node:child_process'; import { mkdtempSync, readFileSync, rmSync, statSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { GUIDE_END_PREFIX, GUIDE_MAX_CHARS } from '../src/agent-docs/guide.ts'; const root = path.resolve(import.meta.dirname, '..'); const cli = path.join(root, 'dist/cli.js'); const failures: string[] = []; const check = (ok: boolean, message: string): void => { if (!ok) failures.push(message); }; const pkg = JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8')) as { version: string; bin?: Record; }; const packed = JSON.parse( execFileSync('npm', ['pack', '--dry-run', '--json', '--ignore-scripts'], { cwd: root, encoding: 'utf8' }), ) as { files: { path: string }[] }[]; const files = new Set(packed[0]?.files.map((f) => f.path)); for (const file of ['dist/cli.js', 'dist/guide.md', 'dist/api.json', '.claude/skills/browserclaw/SKILL.md']) check(files.has(file), `npm pack does not include ${file}`); check(pkg.bin?.browserclaw === './dist/cli.js', 'package.json bin.browserclaw is not ./dist/cli.js'); const source = readFileSync(cli, 'utf8'); check(source.startsWith('#!/usr/bin/env node\n'), 'dist/cli.js does not start with #!/usr/bin/env node'); check((statSync(cli).mode & 0o111) !== 0, 'dist/cli.js is not executable'); check(statSync(cli).size < 50 * 1024, `dist/cli.js is ${String(statSync(cli).size)} bytes; expected under 50 KB`); for (const module of ['playwright-core', 'typescript']) check( !new RegExp(`(from\\s*|import\\s*\\(\\s*|require\\s*\\(\\s*)['"]${module}['"]`).test(source), `dist/cli.js imports ${module}`, ); const guide = execFileSync(process.execPath, [cli, 'skill'], { encoding: 'utf8' }); const lines = guide.trimEnd().split('\n'); const lastLine = lines.at(-1) ?? ''; const endStart = `${GUIDE_END_PREFIX}${pkg.version} (`; const end = lastLine.startsWith(endStart) ? /^(\d+) lines\)$/.exec(lastLine.slice(endStart.length)) : null; check(end !== null, `the guide's last line is not the end line for v${pkg.version}: ${lines.at(-1) ?? ''}`); check( Number(end?.[1]) === lines.length, `the end line counts ${end?.[1] ?? '?'} lines; the guide has ${String(lines.length)}`, ); check( guide.length <= GUIDE_MAX_CHARS, `the guide is ${String(guide.length)} characters; the cap is ${String(GUIDE_MAX_CHARS)}`, ); const outside = mkdtempSync(path.join(tmpdir(), 'browserclaw-cli-check-')); try { const help = spawnSync(process.execPath, [cli, 'help', 'page.click'], { cwd: outside, encoding: 'utf8' }); check(help.status === 0, `help page.click exited ${String(help.status)}: ${help.stderr}`); check(help.stdout.startsWith('page.click('), 'help page.click did not print the signature'); } finally { rmSync(outside, { recursive: true, force: true }); } if (failures.length > 0) { console.error('Package CLI check FAILED:\n'); for (const failure of failures) console.error(` ✗ ${failure}`); process.exit(1); } console.log( `Package CLI check passed — bin, guide (${String(lines.length)} lines, ${String(guide.length)} characters) and help verified.`, );