38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
import path from 'node:path';
|
|
import { pathToFileURL } from 'node:url';
|
|
|
|
let playwrightPromise;
|
|
|
|
// Resolve project dependencies normally; an external installation is optional.
|
|
export async function loadPlaywright() {
|
|
playwrightPromise ??= (async () => {
|
|
const modulePath = process.env.ORGCHART_PLAYWRIGHT_MODULE?.trim();
|
|
try {
|
|
const runtime = await import(modulePath ? pathToFileURL(path.resolve(modulePath)).href : 'playwright');
|
|
if (typeof runtime.chromium?.launch !== 'function') throw new Error('Module does not export chromium.launch');
|
|
return runtime;
|
|
} catch (cause) {
|
|
throw new Error(
|
|
`OrgChart browser verification cannot load Playwright (${modulePath || 'project module: playwright'}). ` +
|
|
'Use an existing project Playwright installation, or set ORGCHART_PLAYWRIGHT_MODULE to an existing Playwright entry file.',
|
|
{ cause },
|
|
);
|
|
}
|
|
})();
|
|
return playwrightPromise;
|
|
}
|
|
|
|
export async function launchChromium(options = {}) {
|
|
const { chromium } = await loadPlaywright();
|
|
const executablePath = process.env.ORGCHART_CHROME?.trim();
|
|
try {
|
|
return await chromium.launch({ ...options, ...(executablePath ? { executablePath: path.resolve(executablePath) } : {}) });
|
|
} catch (cause) {
|
|
throw new Error(
|
|
`OrgChart browser verification cannot launch Chromium (${executablePath || 'Playwright-managed browser'}). ` +
|
|
'Ensure this Playwright installation has its Chromium browser, or set ORGCHART_CHROME to an existing browser executable.',
|
|
{ cause },
|
|
);
|
|
}
|
|
}
|