import { readdir, readFile } from 'node:fs/promises'; import { existsSync } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; /** * Resolve the `playbooks/` directory. Prefer a copy under the caller's workspace * root/cwd, but fall back to the copy bundled inside the installed npm package * (dist/video/ -> package root) — otherwise `playbook-list`/`playbook-show` return * empty for anyone who installed the package and runs from a different directory. */ function resolvePlaybooksDir(root = process.cwd()): string { const cwdDir = join(resolve(root), 'playbooks'); if (existsSync(cwdDir)) return cwdDir; const packaged = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..', 'playbooks'); return existsSync(packaged) ? packaged : cwdDir; } export interface VideoPlaybook { name: string; provider: 'veo' | 'seedance'; useWhen: string[]; promptFormula: string[]; constraints: string[]; adaptationChecklist: string[]; } function playbooksDir(root = process.cwd()): string { return resolvePlaybooksDir(root); } export async function listPlaybooks(root = process.cwd()): Promise { const dir = playbooksDir(root); if (!existsSync(dir)) return []; return (await readdir(dir)) .filter((entry) => entry.endsWith('.json')) .map((entry) => entry.replace(/\.json$/, '')) .sort(); } export async function readPlaybook( name: string, root = process.cwd(), ): Promise { const path = join(playbooksDir(root), `${name}.json`); if (!existsSync(path)) return null; return JSON.parse(await readFile(path, 'utf-8')) as VideoPlaybook; }