Skip to content

Commit

Permalink
spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Sep 3, 2024
1 parent 80dadcf commit c447a92
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions cli/test/smokehouse/lighthouse-runners/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
*/

import {promises as fs} from 'fs';
import {promisify} from 'util';
import {execFile} from 'child_process';
import {spawn} from 'child_process';

import log from 'lighthouse-logger';

Expand All @@ -22,8 +21,6 @@ import {LocalConsole} from '../lib/local-console.js';
import {ChildProcessError} from '../lib/child-process-error.js';
import {LH_ROOT} from '../../../../shared/root.js';

const execFileAsync = promisify(execFile);

/**
* Launch Chrome and do a full Lighthouse run via the Lighthouse CLI.
* @param {string} url
Expand Down Expand Up @@ -82,27 +79,23 @@ async function internalRun(url, tmpPath, config, logger, options) {
const env = {...process.env, NODE_ENV: 'test'};
logger.log(`${log.dim}$ ${command} ${args.join(' ')} ${log.reset}`);

/** @type {{stdout: string, stderr: string, code?: number}} */
let execResult;
try {
execResult = await execFileAsync(command, args, {env});
} catch (e) {
// exec-thrown errors have stdout, stderr, and exit code from child process.
execResult = e;
}

const exitCode = execResult.code || 0;
if (isDebug) {
const cp = spawn(command, args, {env});
cp.stdout.on('data', data => logger.log(`[STDOUT] ${data.toString().trim()}`));
cp.stderr.on('data', data => logger.log(`[STDERR] ${data.toString().trim()}`));
/** @type {Promise<number|null>} */
const cpPromise = new Promise((resolve, reject) => {
cp.addListener('exit', resolve);
cp.addListener('error', reject);
});
const exitCode = await cpPromise;
if (exitCode) {
logger.log(`exit code ${exitCode}`);
logger.log(`STDOUT: ${execResult.stdout}`);
logger.log(`STDERR: ${execResult.stderr}`);
}

try {
await fs.access(outputPath);
} catch (e) {
throw new ChildProcessError(`Lighthouse run failed to produce a report and exited with ${exitCode}.`, // eslint-disable-line max-len
logger.getLog());
throw new ChildProcessError(`Lighthouse run failed to produce a report.`, logger.getLog());
}

/** @type {LH.Result} */
Expand Down

0 comments on commit c447a92

Please sign in to comment.