-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
lighthouse-puppeteer.js
executable file
·116 lines (109 loc) · 4.52 KB
/
lighthouse-puppeteer.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class LighthousePuppeteer {
constructor() {
this.DEBUG_PORT = 9222;
this.puppeteer = require('puppeteer');
this.lightHouseBatch = require('lighthouse-batch');
this.defaultOptions = {
debugPort: this.DEBUG_PORT,
lighthouse: {
out: '/home/chrome/reports',
html: false,
params: '',
verbose: false
},
puppeteer: {
},
chromium: '',
};
this.options = Object.assign({}, this.defaultOptions);
this.browser = null;
}
definePuppeteerOptions(opts = []) {
for (let name in opts) {
if (opts[name].startsWith('--puppeteer-')) {
const param = opts[name].replace('--puppeteer-', '');
const nextParam = opts[name - -1];
this.options.puppeteer[param] = nextParam && !nextParam.startsWith('--puppeteer-') ? nextParam : true;
}
}
return this;
}
defineOptions(opts = {}) {
if (opts.main.port) {
this.options.debugPort = opts.main.port;
}
if (opts.lighthouse && opts.lighthouse.output_directory) {
this.options.lighthouse.out = opts.lighthouse.output_directory;
}
if (opts.lighthouse && opts.lighthouse.html) {
this.options.lighthouse.html = opts.lighthouse.html;
}
if (opts.lighthouse && opts.lighthouse.lighthouse_params) {
this.options.lighthouse.params = opts.lighthouse.lighthouse_params;
}
if (opts.main && opts.main.chromium_params) {
this.options.chromium = opts.main.chromium_params;
}
if (!opts.main.verbose) {
this.options.lighthouse.params += '--quiet';
}
if (opts.main.verbose && opts.main.verbose.length > 1) {
this.options.lighthouse.verbose = true;
}
this.definePuppeteerOptions(opts._unknown || []);
if (typeof (this.options.chromium) === 'undefined') {
this.options.chromium = '';
}
if (this.options.chromium.length > 0) {
this.options.chromium += ' ';
}
this.options.chromium += `--remote-debugging-port=${this.options.debugPort}`;
// https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#setting-up-chrome-linux-sandbox
this.options.puppeteer.args = this.options.chromium.split(' ');
const CHROME_PATH = process.env.CHROME_PATH;
if (CHROME_PATH && CHROME_PATH.length > 0) {
console.debug('Chrome bin path configured through environment variable: ', CHROME_PATH);
this.options.puppeteer.executablePath = CHROME_PATH;
}
return this;
}
exec(modulePath, opts = {}) {
return new Promise((resolveGlobal, reject) => {
this.defineOptions(opts);
const testcase = typeof (modulePath) === 'object' ? modulePath : require(modulePath);
if (typeof(testcase.connect) !== 'function') {
console.log(`${modulePath}: Module incorrectly formatted. Module should have "connect" method!`);
process.exit(-3);
}
if (typeof(testcase.getUrls) !== 'function') {
console.log(`${modulePath}: Module incorrectly formatted. Module should have "getUrls" method!`);
process.exit(-4);
}
this.puppeteer.launch(this.options.puppeteer)
.then(testcase.connect)
.then(b => new Promise((resolve) => {
this.browser = b;
resolve(b);
}))
.then(b => new Promise((resolve) => {
const lighthouseOptions = {
verbose: this.options.lighthouse.verbose,
sites: testcase.getUrls(),
html: this.options.lighthouse.html,
out: this.options.lighthouse.out,
useGlobal: true,
params: `--port ${this.options.debugPort} ${this.options.lighthouse.params}`,
};
this.lightHouseBatch(lighthouseOptions);
resolve(b);
}))
.then(b => b.close())
.then(resolveGlobal)
.catch((err) => {
this.browser && this.browser.close();
reject(err);
});
});
}
}
module.exports = new LighthousePuppeteer();