From 60b8afe1472f26c020545912403d76d646d3f76e Mon Sep 17 00:00:00 2001 From: "Hemanth.HM" Date: Sat, 3 Oct 2015 12:46:46 +0530 Subject: [PATCH] --optimized and --download flags --- README.md | 2 ++ cli.js | 4 +++- index.js | 9 +-------- lib/output.js | 22 +++++++++++++++++++--- package.json | 1 + test/test.js | 9 +++++---- 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e9025d6..aecaf1f 100644 --- a/README.md +++ b/README.md @@ -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. ``` diff --git a/cli.js b/cli.js index 0e14126..c59947d 100755 --- a/cli.js +++ b/cli.js @@ -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.' ] }); diff --git a/index.js b/index.js index 6d719a7..77384f8 100755 --- a/index.js +++ b/index.js @@ -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); }); }; diff --git a/lib/output.js b/lib/output.js index fec48fb..d0f6d17 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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 = []; @@ -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), @@ -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); } }; diff --git a/package.json b/package.json index bced302..425d378 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/test.js b/test/test.js index 03c8b7a..c34e691 100644 --- a/test/test.js +++ b/test/test.js @@ -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(); + }); }); });