Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasonny83 committed Jul 24, 2021
1 parent 476e9c7 commit 0fb07c3
Show file tree
Hide file tree
Showing 6 changed files with 1,479 additions and 499 deletions.
46 changes: 24 additions & 22 deletions bin/cli → bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* https://opensource.org/licenses/MIT
*/

'use strict';
const fs = require('fs');
const path = require('path');
const meow = require('meow');
Expand Down Expand Up @@ -145,31 +144,33 @@ const flags = {
failOnBudgets,
};

function init(args, chromeFlags) {
async function init(args, chromeFlags) {
const testUrl = args[0];

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

if (flags.report) {
const outputPath = path.resolve(flags.report, flags.filename);
await fs.writeFileSync(outputPath, htmlReport);
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);
}
}
if (flags.jsonReport && jsonReport) {
const jsonReportPath = outputPath.replace(/\.[^.]+$/, '.json');
await fs.writeFileSync(jsonReportPath, jsonReport);
}
}

return {
categoryReport,
budgetsReport,
silent,
};
},
);
return {
categoryReport,
budgetsReport,
silent,
};
}

Promise.resolve()
Expand Down Expand Up @@ -223,14 +224,15 @@ Promise.resolve()
if (result.score === false) {
throw new Error('Target score not reached.');
}

if (result.budget === false) {
throw new Error('Target budget not reached.');
}

throw new Error('lighthouse-ci test failed.');
})
.catch((err) => {
.catch((error) => {
spinner.stop();
console.log(chalk.red(err), '\n');
console.log(chalk.red(error), '\n');
return process.exit(1);
});
2 changes: 1 addition & 1 deletion lib/calculate-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const calculateResults = (flags, categoryReport, budgetsReport, failOnBudgets) =
}
: thresholds;

if (thresholds && Object.keys(thresholds).length !== 0) {
if (thresholds && Object.keys(thresholds).length > 0) {
const isScorePassing = analyzeScore(categoryReport, thresholds);
const areBudgetsPassing = analyzeBudgets(budgetsReport, failOnBudgets);

Expand Down
6 changes: 3 additions & 3 deletions lib/lighthouse-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

const fs = require('fs');
const path = require('path');
const util = require('util');
const { promisify } = require('util');
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const ReportGenerator = require('lighthouse/report/report-generator');
const chalk = require('chalk');
const { createDefaultConfig, getOwnProps, convertToBudgetList, convertToResourceKey } = require('./helpers');

const readFile = util.promisify(fs.readFile);
const readFile = promisify(fs.readFile);

const launchChromeAndRunLighthouse = async (url, chromeFlags, lighthouseFlags, configPath, budgetPath) => {
const chrome = await chromeLauncher.launch({
Expand Down Expand Up @@ -87,7 +87,7 @@ const launchChromeAndRunLighthouse = async (url, chromeFlags, lighthouseFlags, c
throw new Error(result.lhr.runtimeError.message);
}

if (result.lhr.runWarnings.length >= 1) {
if (result.lhr.runWarnings.length > 0) {
for (const warningMessage of result.lhr.runWarnings) {
console.warn(`\n${chalk.yellow('WARNING:')} ${warningMessage}`);
}
Expand Down
12 changes: 3 additions & 9 deletions lib/score-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,13 @@ function analyzeTotalScore(threshold, categoryReport) {
}

function analyzeScore(categoryReport, thresholds) {
let result;

if (!thresholds || thresholds.length === 0) {
throw new Error('Invalid threshold score.');
}

if (typeof thresholds === 'object') {
result = analyzeScores(thresholds, categoryReport);
} else {
result = analyzeTotalScore(thresholds, categoryReport);
}

return result;
return typeof thresholds === 'object'
? analyzeScores(thresholds, categoryReport)
: analyzeTotalScore(thresholds, categoryReport);
}

module.exports = analyzeScore;
Loading

0 comments on commit 0fb07c3

Please sign in to comment.