Skip to content

Commit

Permalink
Report filename option in cli (#27)
Browse files Browse the repository at this point in the history
* Added filename option to cli

* 1.5.0

* Updated README
  • Loading branch information
ikigeg authored and andreasonny83 committed Apr 16, 2019
1 parent b22867e commit f782bcc
Show file tree
Hide file tree
Showing 4 changed files with 1,715 additions and 1,671 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ $ lighthouse-ci --help
$ lighthouse-ci -s https://example.com/
$ lighthouse-ci https://example.com/ --score=75
$ lighthouse-ci https://example.com/ --accessibility=90 --seo=80
$ lighthouse-ci https://example.com/ --report=. --filename=example-report.html
Options
--report=<path> Generate an HTML report inside a specified folder
--filename=<filename> Specify the name of the generated HTML report file (requires --report).
-s, --silent Run Lighthouse without printing report log.
--score=<threshold> Specify a score threshold for the CI to pass.
--performance=<threshold> Specify a minimal performance score for the CI to pass.
Expand Down
82 changes: 62 additions & 20 deletions bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ const meow = require('meow');
const ora = require('ora');
const chalk = require('chalk');

const { scoreReducer } = require('../lib/helpers');
const { getScores, getChromeFlags } = require('../lib/config');
const {
scoreReducer
} = require('../lib/helpers');
const {
getScores,
getChromeFlags
} = require('../lib/config');
const lighthouseReporter = require('../lib/lighthouse-reporter');
const analizeScore = require('../lib/score-analizer');

Expand All @@ -38,6 +43,7 @@ const cli = meow(
Options
-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).
--score=<threshold> Specify a score threshold for the CI to pass.
--performance=<threshold> Specify a minimal performance score for the CI to pass.
--pwa=<threshold> Specify a minimal pwa score for the CI to pass.
Expand All @@ -48,30 +54,49 @@ const cli = meow(
In addition to listed "lighthouse-ci" configuration flags, it is also possible to pass any native "lighthouse" flag.
To see the full list of available flags, please refer to the official Google Lighthouse documentation at https://github.com/GoogleChrome/lighthouse#cli-options
`,
{
`, {
flags: {
report: {
type: 'string',
},
filename: {
type: 'string',
alias: 'f',
default: 'report.html',
},
silent: {
type: 'boolean',
alias: 's',
default: false,
},
score: { type: 'string' },
performance: { type: 'string' },
pwa: { type: 'string' },
accessibility: { type: 'string' },
bestPractice: { type: 'string' },
bestPractices: { type: 'string' },
seo: { type: 'string' },
score: {
type: 'string'
},
performance: {
type: 'string'
},
pwa: {
type: 'string'
},
accessibility: {
type: 'string'
},
bestPractice: {
type: 'string'
},
bestPractices: {
type: 'string'
},
seo: {
type: 'string'
},
},
},
);

const {
report,
filename,
silent,
s,
score,
Expand All @@ -81,35 +106,47 @@ const {
bestPractice,
bestPractices,
seo,
...lighthouseFlags} = cli.flags;
...lighthouseFlags
} = cli.flags;
const calculatedBestPractices = bestPractice || bestPractices;
const flags = {
report,
filename,
silent,
s,
score,
performance,
pwa,
accessibility,
...(calculatedBestPractices && { 'best-practices': calculatedBestPractices }),
seo
...(calculatedBestPractices && {
'best-practices': calculatedBestPractices
}),
seo,
};

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

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

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

await fs.writeFileSync(outputPath, htmlReport);
}

return { categoryReport, silent };
return {
categoryReport,
silent
};
},
);
}
Expand All @@ -125,7 +162,10 @@ Promise.resolve()

return init(cli.input, getChromeFlags());
})
.then(({ categoryReport, silent }) => {
.then(({
categoryReport,
silent
}) => {
spinner.stop();

if (!silent) {
Expand All @@ -148,7 +188,9 @@ Promise.resolve()
let thresholds = scoreReducer(flags, getScores());

thresholds =
Object.keys(thresholds).length === 0 ? { score: 100 } : thresholds;
Object.keys(thresholds).length === 0 ? {
score: 100
} : thresholds;

if (thresholds && Object.keys(thresholds).length !== 0) {
const isScorePassing = analizeScore(categoryReport, thresholds);
Expand Down
Loading

0 comments on commit f782bcc

Please sign in to comment.