import { execFile, spawn } from 'node:child_process'; import { mkdir } from 'node:fs/promises'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { promisify } from 'node:util'; // Pass the product dev URL, and optionally a built website URL for README captures. const list = process.argv[2] === '--list'; const preview = new URL('/__guide-preview', !list && process.argv[2] || 'http://127.0.0.1:8787'); const readmePreview = process.argv[3] ? new URL('/film/ui/index.html', process.argv[3]) : null; const output = fileURLToPath(new URL('../docs/assets/guide/', import.meta.url)); const browser = process.env.AGENT_BROWSER_PATH || (process.platform === 'win32' ? join(process.env.APPDATA, 'npm/node_modules/agent-browser/bin/agent-browser-win32-x64.exe') : 'agent-browser'); const session = 'piik-guide-images-' + process.pid; const execute = promisify(execFile); const root = '[data-guide-capture][data-guide-ready]'; const sceneControls = { 'app-modes': ['.lr-client-modes'], 'virtual-lan-address': ['details:has(.lr-client-lan)', 'details:has(.lr-client-lan) > summary'], 'invite-copy': ['.cp-guide-invite'], 'invite-qr': ['.lr-invite-qr:popover-open', '.lr-invite-field [popovertarget]'], 'join-code': [root], 'join-password': [root], 'share-source': ['.lr-source-picker-panel'], 'share-source-confirm': ['.lr-source-picker-panel', '.lr-source-option'], 'sharing-settings': ['.lr-sharing-panel'], 'room-voice': ['.lr-interaction-panel', '.lr-scene button[popovertarget]', '.lr-interaction-panel [role="tab"][id$="-voice-tab"]'], 'touch-joystick': ['.lr-tv:has([data-side=left] > button[aria-expanded=true]):has([data-side=right] > button[aria-expanded=true])', '.lr-remote-direction[data-side=left] > button:first-child', '.lr-remote-direction[data-side=right] > button:first-child'], 'share-failure': ['dialog.lr-share-help[open]', 'button.lr-status-indicator'], 'viewer-route-failure': ['.lr-tv-screen'], }; function batch(commands) { return new Promise((resolve, reject) => { const child = spawn(browser, ['--session', session, 'batch', '--bail'], { stdio: ['pipe', 'inherit', 'inherit'], windowsHide: true, }); child.once('error', reject); child.once('close', code => { if (code === 0) resolve(); else reject(new Error('Screenshot batch failed with exit code ' + code)); }); child.stdin.once('error', reject); child.stdin.end(JSON.stringify(commands)); }); } function capture(url, selector, directory, filename, actions = []) { const prepare = `(async () => { const target = document.querySelector(${JSON.stringify(selector)}); await document.fonts.ready; await Promise.all(Array.from(target.querySelectorAll('img'), image => image.decode())); await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve))); })()`; return [ ['open', url.href], ['wait', actions.length ? root : selector], ...actions.map(action => ['click', action]), ['wait', selector], ['mouse', 'move', '1', '1'], ['eval', '-b', Buffer.from(prepare).toString('base64')], ['screenshot', selector, join(directory, filename + '.png')], ]; } if (!list) await execute(process.execPath, [fileURLToPath(import.meta.resolve('tsx/cli')), '--tsconfig', fileURLToPath(new URL('./tsconfig.json', import.meta.url)), fileURLToPath(new URL('./update-guide-diagrams.tsx', import.meta.url))]); if (!list) await mkdir(output, { recursive: true }); try { for (const theme of ['light', 'dark']) { const commands = [['set', 'viewport', '1280', '900'], ['set', 'media', theme, 'reduced-motion']]; for (const [lang, locale] of [['en', 'en'], ['zh', 'zh-CN']]) { const suffix = locale + (theme === 'dark' ? '.dark' : ''); for (const [scene, [selector, ...actions]] of Object.entries(sceneControls)) { const url = new URL(preview); url.search = new URLSearchParams({ scene, lang, theme }); commands.push(...capture(url, selector, output, scene + '.' + suffix, actions)); } } if (readmePreview) { commands.push(['set', 'viewport', '1160', '1040']); for (const lang of ['en', 'zh']) { const url = new URL(readmePreview); url.search = new URLSearchParams({ still: 'viewer', lang, theme }); commands.push(...capture(url, '.is-still .lr-app', join(output, '..'), 'room-' + lang + '-' + theme)); } } if (list) { for (const [action, , path] of commands) if (action === 'screenshot') console.log(path); } else await batch(commands); } } finally { if (!list) await execute(browser, ['--session', session, 'close'], { timeout: 5000 }).catch(error => { console.warn('Could not close the screenshot browser:', error.message); }); }