Skip to content

Commit

Permalink
rework the API
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 11, 2015
1 parent 1ebbe2e commit 8b01266
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
7 changes: 2 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -46,6 +44,5 @@ psi(opts, function (err, res) {
}
}

psi.output(opts, res);
process.exit(0);
});
29 changes: 22 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
});
};
8 changes: 4 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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)));
});
});

0 comments on commit 8b01266

Please sign in to comment.