-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreports.js
More file actions
52 lines (46 loc) · 1.54 KB
/
reports.js
File metadata and controls
52 lines (46 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// const { writeFileSync } = require('fs');
// const shell = require('shelljs');
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
function launchChromeAndRunLighthouse(url, opts, config = null) {
return chromeLauncher
.launch({ chromeFlags: opts.chromeFlags })
.then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => {
// use results.lhr for the JS-consumeable output
// https://github.com/GoogleChrome/lighthouse/blob/master/types/lhr.d.ts
// use results.report for the HTML/JSON/CSV output as a string
// use results.artifacts for the trace/screenshots/other specific case you need (rarer)
// console.log(results.report);
// return chrome.kill().then(() => results.lhr);
return chrome.kill().then(() => ({
// html: results.report,
// results,
results,
}));
});
});
}
const defaultOptions = {
chromeFlags: ['--show-paint-rects'],
onlyCategories: ['performance'],
output: 'html',
// output: 'json',
outputPath: './lighthouse-results.html',
};
const generateReport = async (
url = 'https://example.com',
options = defaultOptions
) => {
const results = await launchChromeAndRunLighthouse(url, options);
// writeFileSync('./output.html', results, 'utf8');
return {
...results.results,
// report: JSON.parse(results.results.report),
};
};
module.exports = {
generateReport,
};
// shell.exec('yarn lighthouse https://example.com --view')