import { spawnSync } from "node:child_process"; const browsers = process.argv.slice(2); const requestedBrowsers = browsers.length > 0 ? browsers : ["chromium"]; const fallbackHostPlatformByUnsupportedPlatform = (platform) => { const ubuntuMatch = platform.match(/^ubuntu(\d+\.\d+)-(x64|arm64)$/); if (ubuntuMatch && Number.parseInt(ubuntuMatch[1], 10) >= 26) { return `ubuntu24.04-${ubuntuMatch[2]}`; } return null; }; const outputText = (result) => `${result.stdout || ""}\n${result.stderr || ""}`; const unsupportedHostPlatform = (result) => { const output = outputText(result); return ( output.match(/Cannot install dependencies for (?\S+) with Playwright/i)?.groups?.platform || output.match(/Playwright does not support \S+ on (?\S+)/i)?.groups?.platform || null ); }; const runPlaywrightInstall = (args, env = {}) => spawnSync("npx", ["playwright", "install", ...args], { cwd: process.cwd(), env: { ...process.env, ...env, }, encoding: "utf8", }); const writeOutput = (result) => { if (result.stdout) { process.stdout.write(result.stdout); } if (result.stderr) { process.stderr.write(result.stderr); } }; const hasUnsupportedHostPlatformFailure = (result) => { const output = outputText(result); return result.status !== 0 && /Playwright does not support .* on /i.test(output); }; const withDepsResult = runPlaywrightInstall(["--with-deps", ...requestedBrowsers]); writeOutput(withDepsResult); if (withDepsResult.status === 0) { process.exit(0); } if (!hasUnsupportedHostPlatformFailure(withDepsResult)) { process.exit(withDepsResult.status ?? 1); } const unsupportedPlatform = unsupportedHostPlatform(withDepsResult); const fallbackHostPlatform = unsupportedPlatform ? fallbackHostPlatformByUnsupportedPlatform(unsupportedPlatform) : null; if (!fallbackHostPlatform) { console.error( `Playwright dependency install is unsupported for ${unsupportedPlatform || "this platform"}, ` + "and this script does not have a safe browser archive fallback for it." ); process.exit(withDepsResult.status ?? 1); } console.warn( [ `Playwright could not install OS dependencies for ${unsupportedPlatform}.`, `Retrying browser installation using Playwright fallback archive ${fallbackHostPlatform}.`, "The runner image must provide the required browser system libraries.", ].join("\n") ); const fallbackResult = runPlaywrightInstall(requestedBrowsers, { PLAYWRIGHT_HOST_PLATFORM_OVERRIDE: fallbackHostPlatform, PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS: "1", }); writeOutput(fallbackResult); process.exit(fallbackResult.status ?? 1);