-
Notifications
You must be signed in to change notification settings - Fork 0
/
LighthouseLauncher.js
53 lines (49 loc) · 1.52 KB
/
LighthouseLauncher.js
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
53
import lighthouse from "lighthouse";
import doDebug from "debug";
const debug = doDebug("Pagespeeder-Core:LighthouseLauncher");
/**
* @description Launches chrome with url, options and config
* @class LighthouseLauncher
*/
class LighthouseLauncher {
/**
* Creates an instance of LighthouseLauncher.
* @param {string} url
* @param {Browser} Browser
* @param {Object} options
* @memberof LighthouseLauncher
*/
constructor({ url, browser, browserPort, lighthouseConfig = null }) {
this.url = url;
this.browser = browser;
this.browserPort = browserPort;
this.lighthouseConfig = lighthouseConfig;
debug("LighthouseLauncher instance constructed");
}
/**
* @description Finally launches chrome
* @returns {Promise<Object>} Results
* @memberof LighthouseLauncher
*/
launch() {
debug("launching lighthouse");
return lighthouse(
this.url,
{ port: this.browserPort },
this.lighthouseConfig
)
.then((results) => {
// use results.lhr for the JS-consumable 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)
debug("lighthouse run successfully");
return results.lhr;
})
.catch((err) => {
debug("lighthouse crashed");
console.error(err);
});
}
}
export { LighthouseLauncher, LighthouseLauncher as default };