storebound/cli/shots/browser.ts

23 lines
1.2 KiB
TypeScript

// Playwright is not a dependency of storebound: it is resolved from the project (or the storebound folder), so a project
// that never renders screenshots never downloads a browser. Launch order: Playwright's own Chromium, then an installed
// Google Chrome (channel "chrome"), so `bunx playwright install chromium` is optional when Chrome is there.
import { join } from 'node:path';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function chromium(root: string): Promise<any> {
for (const base of [root, join(import.meta.dir, '../..')]) {
try {
const p = Bun.resolveSync('playwright', base);
return (await import(p)).chromium;
} catch { /* next */ }
}
throw new Error('playwright not found: in your project run `bun add -d playwright` (and `bunx playwright install chromium` if Google Chrome is not installed)');
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function launch(root: string, opts: Record<string, unknown>): Promise<any> {
const c = await chromium(root);
try { return await c.launch(opts); } catch (e) {
try { return await c.launch({ ...opts, channel: 'chrome' }); } catch { throw e; }
}
}