Skip to content

Commit

Permalink
Add optional generation of JSON as an output (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmagin authored and andreasonny83 committed May 29, 2019
1 parent b4f1518 commit 552f322
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ $ lighthouse-ci --help
-s, --silent Run Lighthouse without printing report log
--report=<path> Generate an HTML report inside a specified folder
--filename=<filename> Specify the name of the generated HTML report file (requires --report)
-json, --jsonReport Generate JSON report in addition to HTML (requires --report)
--config-path The path to the Lighthouse config JSON (read more here: https://github.com/GoogleChrome/lighthouse/blob/master/docs/configuration.md)
--budget-path The path to the Lighthouse budgets config JSON (read more here: https://developers.google.com/web/tools/lighthouse/audits/budgets)
--score=<threshold> Specify a score threshold for the CI to pass
Expand Down
17 changes: 15 additions & 2 deletions bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const cli = meow(
-s, --silent Run Lighthouse without printing report log
--report=<path> Generate an HTML report inside a specified folder
--filename=<filename> Specify the name of the generated HTML report file (requires --report)
-json, --jsonReport Generate JSON report in addition to HTML (requires --report)
--config-path The path to the Lighthouse config JSON (read more here: https://github.com/GoogleChrome/lighthouse/blob/master/docs/configuration.md)
--budget-path The path to the Lighthouse budgets config JSON (read more here: https://developers.google.com/web/tools/lighthouse/audits/budgets)
--score=<threshold> Specify a score threshold for the CI to pass
Expand All @@ -67,6 +68,11 @@ const cli = meow(
alias: 'f',
default: 'report.html',
},
jsonReport: {
type: 'boolean',
alias: 'json',
default: false,
},
silent: {
type: 'boolean',
alias: 's',
Expand Down Expand Up @@ -104,6 +110,8 @@ const cli = meow(
const {
report,
filename,
json,
jsonReport,
silent,
s,
score,
Expand All @@ -120,6 +128,7 @@ const calculatedBestPractices = bestPractice || bestPractices;
const flags = {
report,
filename,
jsonReport: json || jsonReport,
silent,
s,
score,
Expand All @@ -138,13 +147,17 @@ function init(args, chromeFlags) {

// Run Google Lighthouse
return lighthouseReporter(testUrl, flags, chromeFlags, lighthouseFlags).then(
async ({ categoryReport, budgetsReport, htmlReport }) => {
async ({ categoryReport, budgetsReport, htmlReport, jsonReport }) => {
const { silent } = flags;

if (flags.report) {
const outputPath = path.resolve(flags.report, flags.filename);

await fs.writeFileSync(outputPath, htmlReport);

if (flags.jsonReport && jsonReport) {
const jsonReportPath = outputPath.replace(/\.[^\.]+$/, '.json');
await fs.writeFileSync(jsonReportPath, jsonReport);
}
}

return {
Expand Down
21 changes: 16 additions & 5 deletions lib/lighthouse-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ const createHtmlReport = (results, flags) => {
return null;
};

const createJsonReport = (results, flags) => {
if (flags.report && flags.jsonReport) {
return ReportGenerator.generateReport(results, 'json');
}

return null;
};

const createCategoryReport = results => {
const categoryReport = {};

Expand All @@ -103,10 +111,11 @@ const createCategoryReport = results => {

const createBudgetsReport = results => {
const items =
results.audits &&
results.audits['performance-budget'] &&
results.audits['performance-budget'].details &&
results.audits['performance-budget'].details.items;
(results.audits &&
results.audits['performance-budget'] &&
results.audits['performance-budget'].details &&
results.audits['performance-budget'].details.items) ||
[];

const report = items.reduce((acc, obj) => {
if (obj.countOverBudget) {
Expand Down Expand Up @@ -149,10 +158,12 @@ async function writeReport(
);

const htmlReport = createHtmlReport(lighthouseResult.lhr, flags);
const jsonReport = createJsonReport(lighthouseResult.lhr, flags);

const categoryReport = createCategoryReport(lighthouseResult.lhr);
const budgetsReport = createBudgetsReport(lighthouseResult.lhr);

return { categoryReport, htmlReport, budgetsReport };
return { categoryReport, budgetsReport, htmlReport, jsonReport };
}

module.exports = writeReport;

0 comments on commit 552f322

Please sign in to comment.