diff --git a/lighthouse-cli/bin.ts b/lighthouse-cli/bin.ts index f90e5ee0f675..a957ff7459bb 100755 --- a/lighthouse-cli/bin.ts +++ b/lighthouse-cli/bin.ts @@ -276,38 +276,36 @@ function saveResults(results: Results, return promise.then(_ => Printer.write(results, flags.output, flags.outputPath)); } -export function runLighthouse(url: string, +export async function runLighthouse(url: string, flags: {port: number, skipAutolaunch: boolean, selectChrome: boolean, output: any, outputPath: string, interactive: boolean, saveArtifacts: boolean, saveAssets: boolean maxWaitForLoad: number}, - config: Object | null): Promise { - - let chromeLauncher: ChromeLauncher; - return initPort(flags) - .then(() => getDebuggableChrome(flags)) - .then(chrome => chromeLauncher = chrome) - .then(() => lighthouse(url, flags, config)) - .then((results: Results) => { - // remove artifacts from result so reports won't include artifacts. - const artifacts = results.artifacts; - delete results.artifacts; - - let promise = saveResults(results, artifacts!, flags); - if (flags.interactive) { - promise = promise.then(() => performanceXServer.hostExperiment({url, flags, config}, results)); - } + config: Object | null): Promise<{}|void> { - return promise; - }) - .then(() => chromeLauncher.kill()) - .catch(err => { - if (typeof chromeLauncher !== 'undefined') { - return chromeLauncher.kill().then(() => handleError(err), handleError); - } + let chromeLauncher: ChromeLauncher | undefined = undefined; - return handleError(err); - }) -} + try { + await initPort(flags) + const chromeLauncher = await getDebuggableChrome(flags) + const results = await lighthouse(url, flags, config); + + const artifacts = results.artifacts; + delete results.artifacts; + + await saveResults(results, artifacts!, flags); + if (flags.interactive) { + await performanceXServer.hostExperiment({url, flags, config}, results); + } + + return await chromeLauncher.kill(); + } catch (err) { + if (typeof chromeLauncher !== 'undefined') { + await chromeLauncher!.kill(); + } + + return handleError(err); + } + } export function run() { return runLighthouse(url, cliFlags, config);