diff --git a/workflow-steps/install-browsers/main.js b/workflow-steps/install-browsers/main.js index 90c01a4..ca9f4cb 100644 --- a/workflow-steps/install-browsers/main.js +++ b/workflow-steps/install-browsers/main.js @@ -1,61 +1,104 @@ -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...', ); - 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 .+(?=")/gi) || []; + + installDeps(installCommand); + } else { + // TODO(caleb): remove this block, left in for debugging rn + console.log( + `Did not detect missing playwright dependencies during install`, + ); + + console.debug('output ->', output); } + } catch (e) { + console.error(e); + console.log('There is an issue install playwright dependencies'); } + } + + if (hasCypress) { + console.log('Installing browsers required by Cypress'); + execSync('npx cypress install', { stdio: 'inherit' }); + } +} + +/** + * @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); - 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' }); + proc?.stdout?.on('data', (data) => { + const chunk = data.toString(); + stdout += chunk; + console.log(chunk.replaceAll(/\n/gm, '')); + }); + + proc?.stderr?.on('data', (data) => { + const chunk = data.toString(); + stderr += chunk; + console.error(chunk.replaceAll(/\n/gm, '')); + }); + + proc.on('close', (code) => { + res({ stdout, stderr, code }); + }); + }); +} + +/** + * @param {string | undefined} installCommand + */ +function installDeps(installCommand) { + try { + if (installCommand) { + console.log(`Installing Playwright dependencies:\n${installCommand}`); + execSync(installCommand, { stdio: 'inherit' }); } - } catch (e) { - console.error(e); + } 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'); } }