Skip to content

Commit

Permalink
Uses class as module & fixes debug port
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymoulin committed Nov 14, 2017
1 parent f7aa50c commit 523db0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.PHONY=install install-npm publish publish-npm

install:
touch t.tgz
docker run --rm -v `pwd`:/app -ti -w /app node make install-npm
publish:
touch t.tgz
docker run --rm -v `pwd`:/app -ti -w /app node make publish-npm
major:
docker run --rm -v $$HOME:/root -v `pwd`:/app -ti -w /app node npm version major
Expand Down
46 changes: 25 additions & 21 deletions lighthouse-puppeteer.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
const DEBUG_PORT = 9222;
const puppeteer = require('puppeteer');
const lightHouse = require('lighthouse-batch');
const defaultOptions = {
debugPort: DEBUG_PORT,
lighthouse: {
params: '',
useGlobal: true,
out: '/home/chrome/reports',
html: true,
verbose: false,
class LighthousePuppeteer {
constructor() {
this.DEBUG_PORT = 9222;
this.puppeteer = require('puppeteer');
this.lightHouse = require('lighthouse-batch');
this.defaultOptions = {
debugPort: this.DEBUG_PORT,
lighthouse: {
params: '',
useGlobal: true,
out: '/home/chrome/reports',
html: true,
verbose: false,
}
};
this.browser = null;
}
};
var browser;

module.exports = {
exec(modulePath, opts = {}) {
return new Promise((resolveGlobal, reject) => {
const options = Object.assign({}, defaultOptions, opts);
const options = Object.assign({}, this.defaultOptions, 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!`);
Expand All @@ -26,10 +28,10 @@ module.exports = {
console.log(`${modulePath}: Module incorrectly formatted. Module should have "getUrls" method!`);
process.exit(-4);
}
puppeteer.launch({args: [`--remote-debugging-port=${options.debugPort}`]})
this.puppeteer.launch({args: [`--remote-debugging-port=${options.debugPort}`]})
.then(testcase.connect)
.then(b => new Promise((resolve) => {
browser = b;
this.browser = b;
resolve(b);
}))
.then(b => new Promise((resolve) => {
Expand All @@ -39,17 +41,19 @@ module.exports = {
html: options.lighthouse.html,
out: options.lighthouse.out,
useGlobal: options.lighthouse.useGlobal,
params: `--port ${debugPort} ${options.lighthouse.params}`,
params: `--port ${options.debugPort} ${options.lighthouse.params}`,
};
lightHouse(options);
this.lightHouse(options);
resolve(b);
}))
.then(b => b.close())
.then(b => resolveGlobal)
.catch((err) => {
browser.close();
this.browser.close();
reject(err);
});
});
}
};
}

module.exports = new LighthousePuppeteer();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "google-lighthouse-puppeteer",
"version": "0.2.4",
"version": "0.2.5",
"description": "Google Lighthouse Puppeteer is a package to generate reports on multiple urls that allows or not authentication",
"main": "lighthouse-puppeteer.js",
"scripts": {
Expand Down

0 comments on commit 523db0e

Please sign in to comment.