-
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.
- Loading branch information
1 parent
770788a
commit 9af7f45
Showing
7 changed files
with
109 additions
and
143 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
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,14 +1,14 @@ | ||
'use strict'; | ||
exports.render = function (overview, statistics, ruleSetResults, threshold) { | ||
module.exports = 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([ | ||
return [ | ||
'TAP version 13', | ||
'1..1', | ||
outputTest(overview, threshold) | ||
].join('\n')); | ||
].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 |
---|---|---|
@@ -1,106 +1,72 @@ | ||
'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(Number(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) { | ||
var renderer = format(parameters.format); | ||
|
||
threshold = parameters.threshold || threshold; | ||
|
||
renderer( | ||
overview(response.id, parameters.strategy, response.score), | ||
statistics(response.pageStats), | ||
ruleSetResults(response.formattedResults.ruleResults), | ||
threshold | ||
); | ||
|
||
if (response.score < threshold) { | ||
throw new Error('Threshold of ' + threshold + ' not met with score of ' + response.score); | ||
} | ||
} | ||
}; | ||
var THRESHOLD = 70; | ||
|
||
function overview(url, strategy, score) { | ||
var ret = []; | ||
|
||
ret.push({ | ||
label: 'URL', | ||
value: url | ||
}); | ||
|
||
ret.push({ | ||
label: 'Score', | ||
value: score | ||
}); | ||
|
||
ret.push({ | ||
label: 'Strategy', | ||
value: strategy | ||
}); | ||
|
||
return ret; | ||
} | ||
|
||
function ruleSetResults(rulesets) { | ||
var ret = []; | ||
|
||
for (var title in rulesets) { | ||
ret.push({ | ||
label: title, | ||
value: Math.ceil(rulesets[title].ruleImpact * 100) / 100 | ||
}); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
function statistics(stats) { | ||
var ret = []; | ||
|
||
for (var title in stats) { | ||
ret.push({ | ||
label: title, | ||
value: title.indexOf('Bytes') !== -1 ? prettyBytes(Number(stats[title])) : stats[title] | ||
}); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
function getReporter(format) { | ||
format = ['cli', 'json', 'tap'].indexOf(format) === -1 ? 'cli' : format; | ||
return require('./formats/' + format); | ||
} | ||
|
||
module.exports = function (parameters, response) { | ||
var renderer = getReporter(parameters.format); | ||
var threshold = parameters.threshold || THRESHOLD; | ||
|
||
console.log(renderer( | ||
overview(response.id, parameters.strategy, response.score), | ||
statistics(response.pageStats), | ||
ruleSetResults(response.formattedResults.ruleResults), | ||
threshold | ||
)); | ||
|
||
if (response.score < threshold) { | ||
var err = new Error('Threshold of ' + threshold + ' not met with score of ' + response.score); | ||
err.noStack = true; | ||
} | ||
}; |
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
9af7f45
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the amount of effort you've put into improving the module, please feel free to add yourself to the package.json authors :)
9af7f45
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, don't care. Thanks though :)