From 523db0e103c45bed0ad0ec0424366e19e5ca1bd9 Mon Sep 17 00:00:00 2001 From: Jay MOULIN Date: Tue, 14 Nov 2017 18:18:16 +0100 Subject: [PATCH] Uses class as module & fixes debug port --- Makefile | 2 ++ lighthouse-puppeteer.js | 46 ++++++++++++++++++++++------------------- package.json | 2 +- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 80897be..d7d1aef 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/lighthouse-puppeteer.js b/lighthouse-puppeteer.js index 53bfabe..6177fe8 100755 --- a/lighthouse-puppeteer.js +++ b/lighthouse-puppeteer.js @@ -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!`); @@ -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) => { @@ -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(); diff --git a/package.json b/package.json index 6eff152..06ed31b 100644 --- a/package.json +++ b/package.json @@ -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": {