From fc42e5bc3730bc18dd70ce1d7a3801911b9bc008 Mon Sep 17 00:00:00 2001 From: "riccardo.canella" Date: Wed, 24 Oct 2018 11:03:10 +0200 Subject: [PATCH] added readme config example --- README.MD | 47 +++++++++++++++++++++-- lib/audits/A11y.js | 95 +++++++++++++++++++++++----------------------- package.json | 4 +- 3 files changed, 93 insertions(+), 53 deletions(-) diff --git a/README.MD b/README.MD index 886b73d..e57d89a 100644 --- a/README.MD +++ b/README.MD @@ -122,9 +122,50 @@ audit: ### Output examples - - - + + + + + + +### Config json + +You can also pass a custom config json, there are some examples: + +- pagespeed + +```json +{ + pagespeed:{ + "strategy" : [ "mobile", "desktop"], // Strategy to use when analyzing the page. this is the base settings + "locale": "en_US", // Locale results should be generated in. + "threshold": "70", // Threshold score to pass the PageSpeed test. Useful for setting a performance budget. + "pages": ["/"]. //Array of relative pages to analyze, default is only / (please, use relative path) + } +} +``` + +- lighthouse + +```json +{ + lighthouse:{ + "extends" : [ "lighthouse:default" ], // (string|boolean|undefined) The extends property controls if your configuration should inherit from the default Lighthouse configuration. + "settings": { + "onlyCategories": ["performance"], + "onlyAudits": ["works-offline"], + }, // (Object|undefined) The settings property controls various aspects of running Lighthouse such as CPU/network throttling and audit whitelisting/blacklisting. + "audits": [ + "first-meaningful-paint", + "first-interactive", + "byte-efficiency/uses-optimized-images", + ] // (string[]) The audits property controls which audits to run and include with your Lighthouse report. + } +} +``` + +For full list of settings options see [here](https://github.com/GoogleChrome/lighthouse/blob/master/docs/configuration.md). + ## Contributing diff --git a/lib/audits/A11y.js b/lib/audits/A11y.js index 6a418a1..a83ac78 100644 --- a/lib/audits/A11y.js +++ b/lib/audits/A11y.js @@ -3,49 +3,48 @@ const pa11y = require( 'pa11y' ); const puppeteer = require( 'puppeteer' ); class Pagespeed { - constructor( url, opts = {}, isHeadless ) { - this.url = url; - this.auditName = `A11y`; - this.isHeadless = isHeadless; - this.browser = null; - - - this.config = Object.assign( {}, {}, opts ); - - this.start = this.start.bind( this ); - this.a11Request = this.a11Request.bind( this ) - - } - - async start() { - this.spinner = ora( `Start ${this.auditName} audit for ${this.url}` ).start(); - try { - const result = await this.a11Request(); - const formattedResult = this.formatResult( result ) - if ( this.browser !== null ) await this.browser.close(); - this.spinner.succeed( `Finish ${this.auditName} audit for ${this.url}` ); - return !!formattedResult ? formattedResult : '# Well, your page have no problems'; - } catch ( e ) { - console.log( e ) - this.spinner.fail( `Fail ${this.auditName} audit for ${this.url}` ); - return JSON.stringify( e ) + constructor( url, opts = {}, isHeadless ) { + this.url = url; + this.auditName = `A11y`; + this.isHeadless = isHeadless; + this.browser = null; + + + this.config = Object.assign( {}, {}, opts ); + + this.start = this.start.bind( this ); + this.a11Request = this.a11Request.bind( this ) + } - } - async a11Request() { - if ( this.isHeadless ) { - this.browser = await this.getBrowserInstance(); + async start() { + this.spinner = ora( `Start ${this.auditName} audit for ${this.url}` ).start(); + try { + const result = await this.a11Request(); + const formattedResult = this.formatResult( result ) + if ( this.browser !== null ) await this.browser.close(); + this.spinner.succeed( `Finish ${this.auditName} audit for ${this.url}` ); + return !!formattedResult ? formattedResult : '# Well, your page have no problems'; + } catch ( e ) { + this.spinner.fail( `Fail ${this.auditName} audit for ${this.url}` ); + return JSON.stringify( e ) + } } - return pa11y( this.url, { - browser: this.browser, - ...this.config - } ) - .then( data => data.issues ? data.issues : [] ) - } - - formatResult( result ) { - const results = result.map( res => { - return ` + + async a11Request() { + if ( this.isHeadless ) { + this.browser = await this.getBrowserInstance(); + } + return pa11y( this.url, { + browser: this.browser, + ...this.config + } ) + .then( data => data.issues ? data.issues : [] ) + } + + formatResult( result ) { + const results = result.map( res => { + return ` # Code: **${res.code}** ## Severity: **${res.type}** @@ -59,16 +58,16 @@ class Pagespeed { \`\`\` `; - } ); + } ); - return results.join( '' ); - } + return results.join( '' ); + } - getBrowserInstance() { - return puppeteer.launch( { - args: [ '--headless', '--disable-gpu', '--disable-software-rasterizer', '--disable-dev-shm-usage', '--no-sandbox' ] - } ); - } + getBrowserInstance() { + return puppeteer.launch( { + args: [ '--headless', '--disable-gpu', '--disable-software-rasterizer', '--disable-dev-shm-usage', '--no-sandbox' ] + } ); + } } diff --git a/package.json b/package.json index 8f864c2..8dca7dd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "siteaudit", - "version": "1.2.0", + "version": "1.2.1", "description": "Siteaudit will analize your site and generate Google Page Speed and Lighthouse report.", "main": "/lib/index.js", "scripts": {}, @@ -37,4 +37,4 @@ "bugs": { "url": "https://github.com/thecreazy/siteaudit/issues" } -} +} \ No newline at end of file