-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #33 - Adds support for multiple formats
- Loading branch information
Showing
13 changed files
with
268 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var chalk = require('chalk'); | ||
var utils = require('../utils'); | ||
|
||
exports.render = function (overview, statistics, ruleSetResults) { | ||
|
||
var renderOverview = function (item) { | ||
var color = item.label === 'Score' ? utils.scoreColor(item.value) : chalk.cyan; | ||
return item.label + ':' + utils.buffer(item.label, 11) + color(item.value); | ||
}; | ||
|
||
var renderSection = function (item) { | ||
return utils.labelize(item.label) + chalk.cyan(item.value); | ||
}; | ||
|
||
console.log([ | ||
utils.divider, | ||
_.map(overview, renderOverview).join('\n') + '\n', | ||
_.map(statistics, renderSection).join('\n'), | ||
utils.labelize(''), | ||
_.map(ruleSetResults, renderSection).join('\n'), | ||
utils.divider | ||
].join('\n')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var utils = require('../utils'); | ||
|
||
exports.render = function (overview, statistics, ruleSetResults) { | ||
|
||
var mapSection = function (section) { | ||
return utils.firstToUpperCaseAndAddSpace(section.label); | ||
}; | ||
|
||
var zip = function (section, labelMapping) { | ||
return _.zipObject(_.map(section, labelMapping), _.map(section, 'value')); | ||
}; | ||
|
||
overview = zip(overview, 'label'); | ||
statistics = zip(statistics, mapSection); | ||
ruleSetResults = zip(ruleSetResults, mapSection); | ||
console.log( | ||
JSON.stringify( | ||
{ | ||
'overview': overview, | ||
'statistics': statistics, | ||
'ruleResults': ruleSetResults | ||
}, | ||
undefined, 2 | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
exports.render = function (overview, statistics, ruleSetResults, threshold) { | ||
|
||
var outputTest = function (overview, threshold) { | ||
var score = overview[1].value; | ||
var result = score < threshold ? 'not ok' : 'ok'; | ||
return result + ' 1 - psi'; | ||
}; | ||
|
||
console.log([ | ||
'TAP version 13', | ||
'1..1', | ||
outputTest(overview, threshold) | ||
].join('\n')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
'use strict'; | ||
// Based on the reporting in grunt-pagespeed by James Cryer | ||
// TODO: Refactor further as this still uses patterns that | ||
// are based on Grunt conventions. | ||
|
||
var prettyBytes = require('pretty-bytes'); | ||
|
||
exports.init = function () { | ||
|
||
/** | ||
* @var {int} default threshold | ||
*/ | ||
var threshold = 70; | ||
|
||
/** | ||
* Gather overview of PageSpeed results | ||
* | ||
* @param {String} url | ||
* @param {String} strategy | ||
* @param {String} score | ||
* @return array | ||
*/ | ||
var overview = function (url, strategy, score) { | ||
var _results = []; | ||
_results.push({label: 'URL', value: url}); | ||
_results.push({label: 'Score', value: score}); | ||
_results.push({label: 'Strategy', value: strategy}); | ||
|
||
return _results; | ||
}; | ||
|
||
/** | ||
* Gather and normalise rule results for PageSpeed result | ||
* | ||
* @param {Object} rulesets | ||
* @return array | ||
*/ | ||
var ruleSetResults = function (rulesets) { | ||
var title; | ||
var result; | ||
var ruleImpact; | ||
var _results = []; | ||
|
||
for (title in rulesets) { | ||
result = rulesets[title]; | ||
ruleImpact = Math.ceil(result.ruleImpact * 100) / 100; | ||
_results.push({label: title, value: ruleImpact}); | ||
} | ||
|
||
return _results; | ||
}; | ||
|
||
/** | ||
* Gather and normalise statistics for PageSpeed result | ||
* | ||
* @param {Object} statistics | ||
* @return array | ||
*/ | ||
var statistics = function (statistics) { | ||
var title; | ||
var result; | ||
var _results = []; | ||
|
||
for (title in statistics) { | ||
result = title.indexOf('Bytes') !== -1 ? | ||
prettyBytes(+statistics[title]) : | ||
statistics[title]; | ||
|
||
_results.push({label: title, value: result}); | ||
} | ||
|
||
return _results; | ||
}; | ||
|
||
/** | ||
* Loads the correct output format | ||
* | ||
* @param {String} formt | ||
* @return Function | ||
*/ | ||
var format = function (format) { | ||
if (['cli', 'json', 'tap'].indexOf(format) === -1) { | ||
format = 'cli'; | ||
} | ||
return require('./formats/' + format).render; | ||
}; | ||
|
||
return { | ||
process: function (parameters, response, done) { | ||
var error; | ||
var renderer = format(parameters.format); | ||
done = done || function () {}; | ||
threshold = parameters.threshold || threshold; | ||
|
||
renderer( | ||
overview(response.id, parameters.strategy, response.score), | ||
statistics(response.pageStats), | ||
ruleSetResults(response.formattedResults.ruleResults), | ||
threshold | ||
); | ||
|
||
if (response.score < threshold) { | ||
error = new Error('Threshold of ' + threshold + ' not met with score of ' + response.score); | ||
} | ||
|
||
return done(error); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/* | ||
/** | ||
* utils | ||
* | ||
* Collection of functions for report output | ||
|
Oops, something went wrong.