Skip to content

Commit

Permalink
--optimized and --download flags
Browse files Browse the repository at this point in the history
  • Loading branch information
hemanth committed Oct 3, 2015
1 parent e242f09 commit 60b8afe
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ $ psi --help
--screenshot jpg screenshot
--locale Locale results should be generated in.
--threshold Threshold score to pass the PageSpeed test.
--optimized Get the URL of optimized resources.
--download Download optimised resources.
```


Expand Down
4 changes: 3 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ var cli = meow({
' --strategy Strategy to use when analyzing the page: mobile|desktop',
' --format Output format: cli|json|tap',
' --locale Locale results should be generated in.',
' --threshold Threshold score to pass the PageSpeed test.'
' --threshold Threshold score to pass the PageSpeed test.',
' --optimized Get the URL of optimized resources.',
' --download Download optimized resources.'
]
});

Expand Down
9 changes: 1 addition & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ module.exports.output = function (url, opts, cb) {

console.log(data);

try {
output(handleOpts(url, opts), data);
} catch (err) {
cb(err);
return;
}

cb();
output(handleOpts(url, opts), data, cb);
});
};
22 changes: 19 additions & 3 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
var prettyBytes = require('pretty-bytes');
var sortOn = require('sort-on');
var humanizeUrl = require('humanize-url');
var Download = require('download');
var querystring = require('querystring');
var writeFileSync = require('fs').writeFileSync;
var THRESHOLD = 70;
var RESOURCE_URL = 'https://developers.google.com/speed/pagespeed/insights/optimizeContents?';

function overview(url, strategy, score) {
var ret = [];
Expand Down Expand Up @@ -56,9 +60,10 @@ function getReporter(format) {
return require('./formats/' + format);
}

module.exports = function (parameters, response) {
module.exports = function (parameters, response, cb) {
var renderer = getReporter(parameters.format);
var threshold = parameters.threshold || THRESHOLD;
var optimizedResoruceURL = RESOURCE_URL + querystring.stringify({url: response.id, strategy: parameters.strategy})

console.log(renderer(
overview(humanizeUrl(response.id), parameters.strategy, response.ruleGroups.SPEED.score),
Expand All @@ -67,9 +72,20 @@ module.exports = function (parameters, response) {
threshold
));

if (parameters.optimized) {
console.log('\nHere are your optimized images: ', humanizeUrl(optimizedResoruceURL));
}

if (response.ruleGroups.SPEED.score < threshold) {
var err = new Error('Threshold of ' + threshold + ' not met with score of ' + response.ruleGroups.SPEED.score);
err.noStack = true;
throw err;
return cb(err);
}

if (parameters.download) {
new Download()
.get(optimizedResoruceURL)
.dest('.')
.rename('./optimized.zip')
.run(cb);
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
],
"dependencies": {
"chalk": "^1.0.0",
"download": "^4.2.0",
"googleapis": "^2.0.3",
"humanize-url": "^1.0.0",
"lodash": "^3.2.0",
Expand Down
9 changes: 5 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ describe('Formatting', function () {
assert(/"Score": 88/.test(chalk.stripColor(this.formattedOutput)));
});

it('should throw when threshold is not met', function () {
assert.throws(function () {
output({threshold: 100}, response);
});
it('should have an error in the callback if threshold is not met', function (done) {
output({threshold: 100}, response, function (err, result) {
assert.equal(err.name, 'Error', 'Expcted an error.');
done();
});
});
});

Expand Down

0 comments on commit 60b8afe

Please sign in to comment.