diff --git a/README.md b/README.md index 817a8ac..9c85ebd 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ $ lighthouse-ci --help -s, --silent Run Lighthouse without printing report log --report= Generate an HTML report inside a specified folder --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= Specify a score threshold for the CI to pass diff --git a/bin/cli b/bin/cli index aba346d..5b96f29 100644 --- a/bin/cli +++ b/bin/cli @@ -43,6 +43,7 @@ const cli = meow( -s, --silent Run Lighthouse without printing report log --report= Generate an HTML report inside a specified folder --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= Specify a score threshold for the CI to pass @@ -67,6 +68,11 @@ const cli = meow( alias: 'f', default: 'report.html', }, + jsonReport: { + type: 'boolean', + alias: 'json', + default: false, + }, silent: { type: 'boolean', alias: 's', @@ -104,6 +110,8 @@ const cli = meow( const { report, filename, + json, + jsonReport, silent, s, score, @@ -120,6 +128,7 @@ const calculatedBestPractices = bestPractice || bestPractices; const flags = { report, filename, + jsonReport: json || jsonReport, silent, s, score, @@ -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 { diff --git a/lib/lighthouse-reporter.js b/lib/lighthouse-reporter.js index 14b23be..9154d06 100644 --- a/lib/lighthouse-reporter.js +++ b/lib/lighthouse-reporter.js @@ -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 = {}; @@ -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) { @@ -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;