refactor(docs-infra): switch test-pwa-score.js to async/await (#29953)

PR Close #29953
This commit is contained in:
George Kalpakas 2019-04-17 09:36:49 +03:00 committed by Ben Lesh
parent a48d288ff8
commit ca9b3eed9e

View File

@ -2,7 +2,9 @@
/** /**
* Usage: * Usage:
* ```sh
* node scripts/test-pwa-score <url> <min-score> [<log-file>] * node scripts/test-pwa-score <url> <min-score> [<log-file>]
* ```
* *
* Fails if the score is below `<min-score>`. * Fails if the score is below `<min-score>`.
* If `<log-file>` is defined, the full results will be logged there. * If `<log-file>` is defined, the full results will be logged there.
@ -32,7 +34,7 @@ if (process.env.CI) {
_main(process.argv.slice(2)); _main(process.argv.slice(2));
// Functions - Definitions // Functions - Definitions
function _main(args) { async function _main(args) {
const {url, minScore, logFile} = parseInput(args); const {url, minScore, logFile} = parseInput(args);
const isOnHttp = /^http:/.test(url); const isOnHttp = /^http:/.test(url);
@ -44,10 +46,13 @@ function _main(args) {
logger.setLevel(LIGHTHOUSE_FLAGS.logLevel); logger.setLevel(LIGHTHOUSE_FLAGS.logLevel);
launchChromeAndRunLighthouse(url, LIGHTHOUSE_FLAGS, config). try {
then(results => processResults(results, logFile)). const results = await launchChromeAndRunLighthouse(url, LIGHTHOUSE_FLAGS, config);
then(score => evaluateScore(minScore, score)). const score = await processResults(results, logFile);
catch(onError); evaluateScore(minScore, score);
} catch (err) {
onError(err);
}
} }
function evaluateScore(expectedScore, actualScore) { function evaluateScore(expectedScore, actualScore) {
@ -60,13 +65,15 @@ function evaluateScore(expectedScore, actualScore) {
} }
} }
function launchChromeAndRunLighthouse(url, flags, config) { async function launchChromeAndRunLighthouse(url, flags, config) {
return chromeLauncher.launch(CHROME_LAUNCH_OPTS).then(chrome => { const chrome = await chromeLauncher.launch(CHROME_LAUNCH_OPTS);
flags.port = chrome.port; flags.port = chrome.port;
return lighthouse(url, flags, config).
then(results => chrome.kill().then(() => results)). try {
catch(err => chrome.kill().then(() => { throw err; }, () => { throw err; })); return await lighthouse(url, flags, config);
}); } finally {
await chrome.kill();
}
} }
function onError(err) { function onError(err) {
@ -88,24 +95,22 @@ function parseInput(args) {
return {url, minScore, logFile}; return {url, minScore, logFile};
} }
function processResults(results, logFile) { async function processResults(results, logFile) {
const lhVersion = results.lhr.lighthouseVersion;
const categories = results.lhr.categories; const categories = results.lhr.categories;
const report = results.report; const report = results.report;
return Promise.resolve().
then(() => {
if (logFile) { if (logFile) {
console.log(`Saving results in '${logFile}'...`); console.log(`\nSaving results in '${logFile}'...`);
console.log(`(LightHouse viewer: ${VIEWER_URL})`); console.log(`(LightHouse viewer: ${VIEWER_URL})`);
return printer.write(report, printer.OutputMode.json, logFile); await printer.write(report, printer.OutputMode.json, logFile);
} }
}).
then(() => {
const categoryData = Object.keys(categories).map(name => categories[name]); const categoryData = Object.keys(categories).map(name => categories[name]);
const maxTitleLen = Math.max(...categoryData.map(({title}) => title.length)); const maxTitleLen = Math.max(...categoryData.map(({title}) => title.length));
console.log('\nLighthouse version:', results.lhr.lighthouseVersion); console.log(`\nLighthouse version: ${lhVersion}`);
console.log('\nAudit scores:'); console.log('\nAudit scores:');
categoryData.forEach(({title, score}) => { categoryData.forEach(({title, score}) => {
@ -113,8 +118,8 @@ function processResults(results, logFile) {
const paddedScore = (score * 100).toFixed(0).padStart(3); const paddedScore = (score * 100).toFixed(0).padStart(3);
console.log(` - ${paddedTitle} ${paddedScore} / 100`); console.log(` - ${paddedTitle} ${paddedScore} / 100`);
}); });
}).
then(() => categories.pwa.score * 100); return categories.pwa.score * 100;
} }
function skipHttpsAudits(config) { function skipHttpsAudits(config) {