2017-03-31 19:24:25 -04:00
|
|
|
#!/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Usage:
|
2019-04-17 02:36:49 -04:00
|
|
|
* ```sh
|
|
|
|
* node scripts/test-pwa-score <url> <min-score> [<log-file>]
|
|
|
|
* ```
|
2017-03-31 19:24:25 -04:00
|
|
|
*
|
2017-05-15 08:43:08 -04:00
|
|
|
* Fails if the score is below `<min-score>`.
|
|
|
|
* If `<log-file>` is defined, the full results will be logged there.
|
2017-03-31 19:24:25 -04:00
|
|
|
*
|
2017-10-10 05:22:06 -04:00
|
|
|
* (Skips HTTPS-related audits, when run for HTTP URL.)
|
2017-03-31 19:24:25 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Imports
|
2018-10-10 11:46:47 -04:00
|
|
|
const chromeLauncher = require('chrome-launcher');
|
2017-03-31 19:24:25 -04:00
|
|
|
const lighthouse = require('lighthouse');
|
2017-06-13 10:08:31 -04:00
|
|
|
const printer = require('lighthouse/lighthouse-cli/printer');
|
2018-10-10 11:52:10 -04:00
|
|
|
const logger = require('lighthouse-logger');
|
2017-03-31 19:24:25 -04:00
|
|
|
|
|
|
|
// Constants
|
2018-01-18 13:58:40 -05:00
|
|
|
const CHROME_LAUNCH_OPTS = {};
|
2018-10-10 11:52:10 -04:00
|
|
|
const LIGHTHOUSE_FLAGS = {logLevel: 'info'};
|
2018-01-18 13:58:40 -05:00
|
|
|
const SKIPPED_HTTPS_AUDITS = ['redirects-http'];
|
2017-05-15 08:43:08 -04:00
|
|
|
const VIEWER_URL = 'https://googlechrome.github.io/lighthouse/viewer/';
|
2019-04-19 05:46:23 -04:00
|
|
|
const WAIT_FOR_SW_DELAY = 5000;
|
2017-03-31 19:24:25 -04:00
|
|
|
|
2018-10-10 11:52:10 -04:00
|
|
|
// Be less verbose on CI.
|
|
|
|
if (process.env.CI) {
|
2018-10-10 11:52:10 -04:00
|
|
|
LIGHTHOUSE_FLAGS.logLevel = 'error';
|
2017-10-19 14:15:44 -04:00
|
|
|
}
|
|
|
|
|
2017-03-31 19:24:25 -04:00
|
|
|
// Run
|
|
|
|
_main(process.argv.slice(2));
|
|
|
|
|
|
|
|
// Functions - Definitions
|
2019-04-17 02:36:49 -04:00
|
|
|
async function _main(args) {
|
2017-05-15 08:43:08 -04:00
|
|
|
const {url, minScore, logFile} = parseInput(args);
|
2017-03-31 19:24:25 -04:00
|
|
|
const isOnHttp = /^http:/.test(url);
|
2019-04-19 05:46:23 -04:00
|
|
|
const config = {
|
|
|
|
extends: 'lighthouse:default',
|
|
|
|
// Since the Angular ServiceWorker waits for the app to stabilize before registering,
|
|
|
|
// wait a few seconds after load to allow Lighthouse to reliably detect it.
|
|
|
|
passes: [{passName: 'defaultPass', pauseAfterLoadMs: WAIT_FOR_SW_DELAY}],
|
|
|
|
}
|
2017-03-31 19:24:25 -04:00
|
|
|
|
|
|
|
console.log(`Running PWA audit for '${url}'...`);
|
|
|
|
|
2019-04-17 02:36:50 -04:00
|
|
|
// If testing on HTTP, skip HTTPS-specific tests.
|
|
|
|
// (Note: Browsers special-case localhost and run ServiceWorker even on HTTP.)
|
|
|
|
if (isOnHttp) skipHttpsAudits(config);
|
|
|
|
|
2018-10-10 11:52:10 -04:00
|
|
|
logger.setLevel(LIGHTHOUSE_FLAGS.logLevel);
|
|
|
|
|
2019-04-17 02:36:49 -04:00
|
|
|
try {
|
|
|
|
const results = await launchChromeAndRunLighthouse(url, LIGHTHOUSE_FLAGS, config);
|
|
|
|
const score = await processResults(results, logFile);
|
|
|
|
evaluateScore(minScore, score);
|
|
|
|
} catch (err) {
|
|
|
|
onError(err);
|
|
|
|
}
|
2017-03-31 19:24:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateScore(expectedScore, actualScore) {
|
2018-10-10 11:52:10 -04:00
|
|
|
console.log('\nLighthouse PWA score:');
|
|
|
|
console.log(` - Expected: ${expectedScore.toFixed(0).padStart(3)} / 100 (or higher)`);
|
|
|
|
console.log(` - Actual: ${actualScore.toFixed(0).padStart(3)} / 100\n`);
|
2017-03-31 19:24:25 -04:00
|
|
|
|
2018-10-10 11:52:10 -04:00
|
|
|
if (isNaN(actualScore) || (actualScore < expectedScore)) {
|
2017-03-31 19:24:25 -04:00
|
|
|
throw new Error(`PWA score is too low. (${actualScore} < ${expectedScore})`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 02:36:49 -04:00
|
|
|
async function launchChromeAndRunLighthouse(url, flags, config) {
|
|
|
|
const chrome = await chromeLauncher.launch(CHROME_LAUNCH_OPTS);
|
|
|
|
flags.port = chrome.port;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await lighthouse(url, flags, config);
|
|
|
|
} finally {
|
|
|
|
await chrome.kill();
|
|
|
|
}
|
2017-03-31 19:24:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function onError(err) {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseInput(args) {
|
|
|
|
const url = args[0];
|
|
|
|
const minScore = Number(args[1]);
|
2017-05-15 08:43:08 -04:00
|
|
|
const logFile = args[2];
|
2017-03-31 19:24:25 -04:00
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
onError('Invalid arguments: <URL> not specified.');
|
|
|
|
} else if (isNaN(minScore)) {
|
|
|
|
onError('Invalid arguments: <MIN_SCORE> not specified or not a number.');
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:43:08 -04:00
|
|
|
return {url, minScore, logFile};
|
|
|
|
}
|
|
|
|
|
2019-04-17 02:36:49 -04:00
|
|
|
async function processResults(results, logFile) {
|
|
|
|
const lhVersion = results.lhr.lighthouseVersion;
|
2018-10-10 11:46:47 -04:00
|
|
|
const categories = results.lhr.categories;
|
|
|
|
const report = results.report;
|
|
|
|
|
2019-04-17 02:36:49 -04:00
|
|
|
if (logFile) {
|
|
|
|
console.log(`\nSaving results in '${logFile}'...`);
|
|
|
|
console.log(`(LightHouse viewer: ${VIEWER_URL})`);
|
|
|
|
|
|
|
|
await printer.write(report, printer.OutputMode.json, logFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
const categoryData = Object.keys(categories).map(name => categories[name]);
|
|
|
|
const maxTitleLen = Math.max(...categoryData.map(({title}) => title.length));
|
|
|
|
|
|
|
|
console.log(`\nLighthouse version: ${lhVersion}`);
|
|
|
|
|
|
|
|
console.log('\nAudit scores:');
|
|
|
|
categoryData.forEach(({title, score}) => {
|
|
|
|
const paddedTitle = `${title}:`.padEnd(maxTitleLen + 1);
|
|
|
|
const paddedScore = (score * 100).toFixed(0).padStart(3);
|
|
|
|
console.log(` - ${paddedTitle} ${paddedScore} / 100`);
|
|
|
|
});
|
|
|
|
|
|
|
|
return categories.pwa.score * 100;
|
2017-03-31 19:24:25 -04:00
|
|
|
}
|
2018-01-18 13:58:40 -05:00
|
|
|
|
|
|
|
function skipHttpsAudits(config) {
|
|
|
|
console.info(`Skipping HTTPS-related audits (${SKIPPED_HTTPS_AUDITS.join(', ')})...`);
|
2019-04-17 02:36:50 -04:00
|
|
|
const settings = config.settings || (config.settings = {});
|
|
|
|
settings.skipAudits = SKIPPED_HTTPS_AUDITS;
|
|
|
|
}
|