From 0fe33f4dbb2db9288faa0ef853101af195b41d79 Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Fri, 10 Jan 2025 10:14:43 -0600 Subject: [PATCH] chore(install-browsers): testing stuff --- workflow-steps/install-browsers/main.js | 135 +++++++++++++++--------- 1 file changed, 84 insertions(+), 51 deletions(-) diff --git a/workflow-steps/install-browsers/main.js b/workflow-steps/install-browsers/main.js index 90c01a4..4dd7c3b 100644 --- a/workflow-steps/install-browsers/main.js +++ b/workflow-steps/install-browsers/main.js @@ -1,61 +1,94 @@ -const { execSync } = require('child_process'); +//@ts-check +const { execSync, exec } = require('child_process'); const { existsSync, readFileSync } = require('fs'); -if (existsSync('package.json')) { - try { - const json = JSON.parse(readFileSync('package.json').toString()); - const hasPlaywright = - (json.dependencies || {}).hasOwnProperty('@playwright/test') || - (json.devDependencies || {}).hasOwnProperty('@playwright/test'); - if (hasPlaywright) { - console.log('Installing browsers required by Playwright'); - const output = execSync('npx playwright install', { - stdio: 'inherit', - }).toString(); - - if (output.includes('missing dependencies')) { +main(); + +async function main() { + console.log('working??'); + if (!existsSync('package.json')) { + console.log( + 'Unable to determine which e2e test runner being used. Missing root level package.json file', + ); + return; + } + + const json = JSON.parse(readFileSync('package.json').toString()); + const hasPlaywright = + (json.dependencies || {}).hasOwnProperty('@playwright/test') || + (json.devDependencies || {}).hasOwnProperty('@playwright/test'); + + const hasCypress = + (json.dependencies || {}).hasOwnProperty('cypress') || + (json.devDependencies || {}).hasOwnProperty('cypress'); + + if (hasPlaywright) { + console.log('Installing browsers required by Playwright'); + try { + const output = await runCmdAsync('npx playwright install'); + + if (output.stderr.includes('apt-get install')) { console.log( - 'Playwright detected missing dependencies. Attempting to install...', + 'Detected missing Playwright dependencies. Attempting manual install...', ); - try { - // playwright has detected out of sync dependencies on the host machine, we we'll try to manually install them to prevent hard to debug failures - const installDryRun = execSync( - 'npx playwright install --with-deps --dry-run', - { stdio: 'pipe' }, - ).toString(); - - const [installCommand] = installDryRun.match( - /apt-get install .+(?=")/gi, - ); - if (installCommand) { - console.log( - `Installing Playwright dependencies:\n${installCommand}`, - ); - execSync(installCommand, { stdio: 'inherit' }); - } - } catch (installError) { - console.error( - 'There was an issue installing dependencies for Playwright.', - ); - console.error(installError); - console.log( - 'You can create a custom launch template and add a step to manually install the missing Playwright dependencies in order to get around this error.', - ); - console.log( - 'See docs here: https://nx.dev/ci/reference/launch-templates', - ); + // playwright has detected out of sync dependencies on the host machine, we we'll try to manually install them to prevent hard to debug failures + const [installCommand] = + output.stderr.match(/apt-get install (\b\w+\b )+/gi) || []; + if (installCommand) { + installDeps(`sudo ${installCommand.trim()} -y`); } } + } catch (e) { + console.error(e); + console.log('There is an issue install playwright dependencies'); } + } - const hasCypress = - (json.dependencies || {}).hasOwnProperty('cypress') || - (json.devDependencies || {}).hasOwnProperty('cypress'); - if (hasCypress) { - console.log('Installing browsers required by Cypress'); - execSync('npx cypress install', { stdio: 'inherit' }); - } - } catch (e) { - console.error(e); + if (hasCypress) { + console.log('Installing browsers required by Cypress'); + execSync('npx cypress install', { stdio: 'inherit' }); + } + console.log('Done'); +} + +/** + * @param {string} cmd + * @returns {Promise<{ stdout: string; stderr: string; code: number | null; }>} + */ +async function runCmdAsync(cmd) { + return new Promise((res) => { + let stdout = ''; + let stderr = ''; + const proc = exec(cmd); + + proc?.stdout?.on('data', (data) => { + stdout += data.toString(); + process.stdout.write(data); + }); + + proc?.stderr?.on('data', (data) => { + stderr += data.toString(); + process.stderr.write(data); + }); + + proc.on('close', (code) => { + res({ stdout, stderr, code }); + }); + }); +} + +/** + * @param {string} installCommand + */ +function installDeps(installCommand) { + try { + console.log(`Installing Playwright dependencies:\n${installCommand}`); + execSync(installCommand.trim(), { stdio: 'inherit' }); + } catch (installError) { + console.error('There was an issue installing dependencies for Playwright.'); + console.log( + 'You can create a custom launch template and add a step to manually install the missing Playwright dependencies in order to get around this error.', + ); + console.log('See docs here: https://nx.dev/ci/reference/launch-templates'); } }