Skip to content

Commit

Permalink
migrate run lighthouse to async.
Browse files Browse the repository at this point in the history
This gives us better type saftey around chrome launcher
  • Loading branch information
samccone committed Feb 22, 2017
1 parent cfa6f5b commit 25a5a70
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions lighthouse-cli/bin.ts
Expand Up @@ -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<undefined> {

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);
Expand Down

0 comments on commit 25a5a70

Please sign in to comment.