diff --git a/aio/scripts/audit-web-app.js b/aio/scripts/audit-web-app.js index 6c080e924c..a265194813 100644 --- a/aio/scripts/audit-web-app.js +++ b/aio/scripts/audit-web-app.js @@ -60,9 +60,16 @@ async function _main(args) { try { console.log(''); const startTime = Date.now(); - const results = await launchChromeAndRunLighthouse(url, lhFlags, lhConfig); + const browser = await puppeteer.launch(); + const browserVersion = await browser.version(); + const results = await runLighthouse(browser, url, lhFlags, lhConfig); + + console.log( + `\n Browser version: ${browserVersion}` + + `\n Lighthouse version: ${results.lhr.lighthouseVersion}`); + const success = await processResults(results, minScores, logFile); - console.log(`\n(Completed in ${((Date.now() - startTime) / 1000).toFixed(1)}s.)\n`); + console.log(`\n (Completed in ${((Date.now() - startTime) / 1000).toFixed(1)}s.)\n`); if (!success) { throw new Error('One or more scores are too low.'); @@ -76,17 +83,6 @@ function formatScore(score) { return `${(score * 100).toFixed(0).padStart(3)}`; } -async function launchChromeAndRunLighthouse(url, flags, config) { - const browser = await puppeteer.launch(); - flags.port = (new URL(browser.wsEndpoint())).port; - - try { - return await lighthouse(url, flags, config); - } finally { - await browser.close(); - } -} - function onError(err) { console.error(err); console.error(''); @@ -136,19 +132,17 @@ function parseMinScores(raw) { } async function processResults(results, minScores, logFile) { - const lhVersion = results.lhr.lighthouseVersion; const categories = results.lhr.categories; const report = results.report; if (logFile) { - console.log(`\nSaving results in '${logFile}'...`); - console.log(` LightHouse viewer: ${VIEWER_URL}`); + console.log(`\n Saving results in '${logFile}'...`); + console.log(` LightHouse viewer: ${VIEWER_URL}`); await printer.write(report, printer.OutputMode.json, logFile); } - console.log(`\nLighthouse version: ${lhVersion}`); - console.log('\nAudit results:'); + console.log('\n Audit results:'); const maxTitleLen = Math.max(...Object.values(categories).map(({title}) => title.length)); const success = Object.keys(categories).sort().reduce((aggr, cat) => { @@ -158,10 +152,19 @@ async function processResults(results, minScores, logFile) { const passed = !isNaN(score) && (score >= minScore); console.log( - ` - ${paddedTitle} ${formatScore(score)} (Required: ${formatScore(minScore)}) ${passed ? 'OK' : 'FAILED'}`); + ` - ${paddedTitle} ${formatScore(score)} (Required: ${formatScore(minScore)}) ${passed ? 'OK' : 'FAILED'}`); return aggr && passed; }, true); return success; } + +async function runLighthouse(browser, url, flags, config) { + try { + flags.port = (new URL(browser.wsEndpoint())).port; + return await lighthouse(url, flags, config); + } finally { + await browser.close(); + } +}