From 8b012664bead1c4c75cc0b4608c2b71c1d513791 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 11 Jan 2015 23:34:08 +0800 Subject: [PATCH] rework the API --- cli.js | 7 ++----- index.js | 29 ++++++++++++++++++++++------- test/test.js | 8 ++++---- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/cli.js b/cli.js index 020f72c..2033748 100755 --- a/cli.js +++ b/cli.js @@ -32,11 +32,9 @@ if (!cli.input[0]) { process.exit(1); } -var opts = cli.flags; -opts.url = prependHttp(cli.input[0]); -opts.strategy = opts.strategy || 'mobile'; +cli.flags.url = cli.input[0]; -psi(opts, function (err, res) { +psi.output(cli.flags, function (err, res) { if (err) { if (err.noStack) { console.error(err.message); @@ -46,6 +44,5 @@ psi(opts, function (err, res) { } } - psi.output(opts, res); process.exit(0); }); diff --git a/index.js b/index.js index 0f78f2f..a193836 100755 --- a/index.js +++ b/index.js @@ -5,21 +5,23 @@ var objectAssign = require('object-assign'); var pagespeed = googleapis.pagespeedonline('v1').pagespeedapi.runpagespeed; var output = require('./lib/output'); -module.exports = function (opts, cb) { +function handleOpts(opts) { opts = objectAssign({ strategy: 'mobile' }, opts); - cb = cb || function () {}; + opts.nokey = opts.key === undefined; + opts.url = prependHttp(opts.url); + + return opts; +} +var psi = module.exports = function (opts, cb) { if (!opts.url) { throw new Error('URL required'); } - opts.nokey = opts.key === undefined; - opts.url = prependHttp(opts.url); - - pagespeed(opts, function (err, response) { + pagespeed(handleOpts(opts), function (err, response) { if (err) { cb(err); return; @@ -29,4 +31,17 @@ module.exports = function (opts, cb) { }); }; -module.exports.output = output; +module.exports.output = function (opts, cb) { + cb = cb || function () {}; + opts = handleOpts(opts); + + psi(opts, function (err, data) { + if (err) { + cb(err); + return; + } + + output(opts, data); + cb(); + }); +}; diff --git a/test/test.js b/test/test.js index 4627918..3102c76 100644 --- a/test/test.js +++ b/test/test.js @@ -3,7 +3,7 @@ var assert = require('assert'); var chalk = require('chalk'); var response = require('./fixtures/response'); -var psi = require('../'); +var output = require('../lib/output'); describe('PSI formatting', function () { beforeEach(function () { @@ -21,17 +21,17 @@ describe('PSI formatting', function () { }); it('should correctly format PageSpeed Insights response', function () { - psi.output({strategy: 'desktop'}, response); + output({strategy: 'desktop'}, response); assert(/Score: 88/.test(chalk.stripColor(this.formattedOutput))); }); it('should format PageSpeed Insights response as TAP output', function () { - psi.output({strategy: 'desktop', format: 'tap'}, response); + output({strategy: 'desktop', format: 'tap'}, response); assert(/ok 1 - psi/.test(chalk.stripColor(this.formattedOutput))); }); it('should format PageSpeed Insights response as JSON output', function () { - psi.output({strategy: 'desktop', format: 'json'}, response); + output({strategy: 'desktop', format: 'json'}, response); assert(/"Score": 88/.test(chalk.stripColor(this.formattedOutput))); }); });