Skip to content

Commit

Permalink
misc(build): create error-y LHR for the deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Jun 25, 2019
1 parent 5a8c18b commit efd41c9
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions lighthouse-core/scripts/build-report-for-autodeployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,31 @@ const mkdirp = require('mkdirp').sync;
const swapLocale = require('../lib/i18n/swap-locale.js');

const ReportGenerator = require('../../lighthouse-core/report/report-generator.js');
const GatherRunner = require('../gather/gather-runner.js');
const {defaultSettings} = require('../config/constants.js');
const lighthouse = require('../index.js');
const lhr = /** @type {LH.Result} */ (require('../../lighthouse-core/test/results/sample_v2.json'));

const DIST = path.join(__dirname, `../../dist`);

// Add a plugin to demo plugin rendering.
lhr.categories['lighthouse-plugin-someplugin'] = {
id: 'lighthouse-plugin-someplugin',
title: 'Plugin',
score: 0.5,
auditRefs: [],
};

(async function() {
addPluginCategory(lhr);
const errorLhr = await generateErrorLHR();

const filenameToLhr = {
'english': lhr,
'espanol': swapLocale(lhr, 'es').lhr,
'arabic': swapLocale(lhr, 'ar').lhr,
english: lhr,
espanol: swapLocale(lhr, 'es').lhr,
arabic: swapLocale(lhr, 'ar').lhr,
error: errorLhr,
};

mkdirp(DIST);

// Generate and write reports
Object.entries(filenameToLhr).forEach(([filename, lhr]) => {
let html = ReportGenerator.generateReportHtml(lhr);
// TODO: PSI is another variant to consider
for (const variant of ['', '-devtools']) {
if (variant === '-devtools') {
// TODO: Make the DevTools Audits panel "emulation" more comprehensive
html = html.replace(`"lh-root lh-vars"`, `"lh-root lh-vars lh-devtools"`);
}
const filepath = `${DIST}/${filename}${variant}/index.html`;
Expand All @@ -49,3 +48,40 @@ lhr.categories['lighthouse-plugin-someplugin'] = {
}
});
})();

/**
* Add a plugin to demo plugin rendering.
* @param {LH.Result} lhr
*/
function addPluginCategory(lhr) {
lhr.categories['lighthouse-plugin-someplugin'] = {
id: 'lighthouse-plugin-someplugin',
title: 'Plugin',
score: 0.5,
auditRefs: [],
};
}

/**
* Generate an LHR with errors for the renderer to display
* We'll write an "empty" artifacts file to disk, only to use it in auditMode
* @return {Promise<LH.Result>}
*/
async function generateErrorLHR() {
const url = 'http://fakeurl.com';
const options = {
requestedUrl: url,
settings: defaultSettings,
driver: {
getBrowserVersion: () => ({userAgent: 'Mozilla/5.0 ErrorUserAgent Chrome/66'}),
},
};
//@ts-ignore driver isn't mocked out completely
const artifacts = await GatherRunner.initializeBaseArtifacts(options);

const TMP = `${DIST}/.tmp/`;
mkdirp(TMP);
fs.writeFileSync(`${TMP}/artifacts.json`, JSON.stringify(artifacts), 'utf-8');
const errorRunnerResult = await lighthouse(url, {auditMode: TMP});
return /** @type {LH.RunnerResult} */ (errorRunnerResult).lhr;
}

0 comments on commit efd41c9

Please sign in to comment.