import { PLAYWRIGHT_MISSING_ERROR_MESSAGE, FAILED_TO_INSTALL_DEPENDENCIES_ERROR_MESSAGE, } from "../consts/init.const"; import { exec } from "child_process"; import fs from "fs"; import { OPTIONS } from "../consts/cli.const"; async function installPlaywrightDependenciesPython(): Promise { if (await isPythonPlaywrightInstalled()) { await installPlaywright(`playwright install chromium`); } } async function installPlaywrightDependenciesJavascript(): Promise { if (await isJavascriptPlaywrightInstalled()) { await installPlaywright(`npx playwright install chromium`); } } async function installPlaywright(command: string): Promise { // nosemgrep: javascript.lang.security.detect-child-process.detect-child-process exec(command, (error) => { if (error) { console.error(FAILED_TO_INSTALL_DEPENDENCIES_ERROR_MESSAGE); if (OPTIONS.verbose) { console.error(error); } process.exit(1); } }); } async function isPythonPlaywrightInstalled(): Promise { return new Promise((resolve) => { // we cannot simply do `playwright` because it will be treated as an erroneus command. exec(`playwright -V`, (error) => { if (error) { console.error(PLAYWRIGHT_MISSING_ERROR_MESSAGE); process.exit(1); } else { resolve(true); // Resolve with true if installed } }); }); } async function isJavascriptPlaywrightInstalled(): Promise { try { require.resolve("playwright", { paths: [process.cwd()] }); return true; } catch (_) { console.error(PLAYWRIGHT_MISSING_ERROR_MESSAGE); process.exit(1); } } function ensurePathExists(path: string) { if (!fs.existsSync(path)) { fs.mkdirSync(path, { recursive: true }); } } export { installPlaywrightDependenciesPython, installPlaywrightDependenciesJavascript, isJavascriptPlaywrightInstalled, isPythonPlaywrightInstalled, ensurePathExists, };