Skip to content

Commit

Permalink
lots of tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 8, 2015
1 parent 0e4ecab commit 01a7ce2
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 99 deletions.
7 changes: 2 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
root = true

[*]
indent_style = tab
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/node_modules/
node_modules
npm-debug.log
10 changes: 2 additions & 8 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true
"unused": "vars",
"strict": true
}
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ Default: `cli`

The format of the report generated from the PageSpeed Insights API. Supported formats: cli, json and tap.

## CLI support

You will probably want to globally install psi if using as a CLI. This can be done as follows:
## CLI

```sh
$ npm install --global psi
Expand All @@ -108,16 +107,16 @@ $ npm install --global psi
You can then casually use it with your key:

```sh
$ psi http://www.todomvc.com --key 'YOUR_KEY_GOES_HERE'
$ psi http://todomvc.com --key 'YOUR_KEY_GOES_HERE'
```

Humanized URLs are also supported. For example:
Humanized URLs are also supported:

```
$ psi todomvc.com
```

Similar to gpagespeed, the following optional flags are also supported:
The following optional flags are also supported:

```sh
$ psi <url> --key=<key> --prettyprint=<true> --userIp=<userIp> --locale=<locale> --strategy=<desktop|mobile> --format<cli|json|tap>
Expand Down
4 changes: 2 additions & 2 deletions bin/cli.js → cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
'use strict';
var pkg = require('../package.json');
var pkg = require('./package.json');
var argv = require('minimist')((process.argv.slice(2)));
var insights = require('../');
var insights = require('./');
var query = process.argv[2];
var opts = {};

Expand Down
11 changes: 2 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*
* psi
* http://github.com/addyosmani/psi
*
* Copyright (c) 2014 Google Inc.
* Licensed under an Apache 2 license.
*/
'use strict';
var googleapis = require('googleapis');
var prependHttp = require('prepend-http');
Expand All @@ -30,8 +23,8 @@ module.exports = function (opts, cb) {
return;
}

output.process(opts, response, function (processErr) {
cb(processErr || err, response);
output.process(opts, response, function (err) {
cb(err, response);
});
});
};
4 changes: 1 addition & 3 deletions lib/formats/cli.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

var _ = require('lodash');
var _ = require('lodash');
var chalk = require('chalk');
var utils = require('../utils');

exports.render = function (overview, statistics, ruleSetResults) {

var renderOverview = function (item) {
var color = item.label === 'Score' ? utils.scoreColor(item.value) : chalk.cyan;
return item.label + ':' + utils.buffer(item.label, 11) + color(item.value);
Expand Down
20 changes: 7 additions & 13 deletions lib/formats/json.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

var _ = require('lodash');
var _ = require('lodash');
var utils = require('../utils');

exports.render = function (overview, statistics, ruleSetResults) {

var mapSection = function (section) {
return utils.firstToUpperCaseAndAddSpace(section.label);
};
Expand All @@ -16,14 +14,10 @@ exports.render = function (overview, statistics, ruleSetResults) {
overview = zip(overview, 'label');
statistics = zip(statistics, mapSection);
ruleSetResults = zip(ruleSetResults, mapSection);
console.log(
JSON.stringify(
{
'overview': overview,
'statistics': statistics,
'ruleResults': ruleSetResults
},
undefined, 2
)
);

console.log(JSON.stringify({
'overview': overview,
'statistics': statistics,
'ruleResults': ruleSetResults
}, undefined, 2));
};
2 changes: 0 additions & 2 deletions lib/formats/tap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

exports.render = function (overview, statistics, ruleSetResults, threshold) {

var outputTest = function (overview, threshold) {
var score = overview[1].value;
var result = score < threshold ? 'not ok' : 'ok';
Expand Down
14 changes: 8 additions & 6 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// 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
*/
Expand All @@ -22,6 +20,7 @@ exports.init = function () {
*/
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});
Expand Down Expand Up @@ -63,7 +62,7 @@ exports.init = function () {

for (title in statistics) {
result = title.indexOf('Bytes') !== -1 ?
prettyBytes(+statistics[title]) :
prettyBytes(Number(statistics[title])) :
statistics[title];

_results.push({label: title, value: result});
Expand All @@ -82,13 +81,14 @@ exports.init = function () {
if (['cli', 'json', 'tap'].indexOf(format) === -1) {
format = 'cli';
}

return require('./formats/' + format).render;
};

return {
process: function (parameters, response, done) {
var error;
var renderer = format(parameters.format);

done = done || function () {};
threshold = parameters.threshold || threshold;

Expand All @@ -100,10 +100,12 @@ exports.init = function () {
);

if (response.score < threshold) {
error = new Error('Threshold of ' + threshold + ' not met with score of ' + response.score);
done(new Error('Threshold of ' + threshold + ' not met with score of ' + response.score));
return;
}

return done(error);
done();
return;
}
};
};
40 changes: 13 additions & 27 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
/**
* utils
*
* Collection of functions for report output
*/
'use strict';

var chalk = require('chalk');
var repeating = require('repeating');

var divider = '\n' + chalk.grey(repeating('-', 64)) + '\n';
exports.divider = '\n' + chalk.grey(repeating('-', 64)) + '\n';

function buffer(msg, length) {
var buffer = '';
exports.buffer = function (msg, length) {
var ret = '';

if (length === undefined) {
length = 50;
Expand All @@ -20,37 +14,29 @@ function buffer(msg, length) {
length = length - msg.length - 1;

if (length > 0) {
buffer = repeating(' ', length);
ret = repeating(' ', length);
}

return buffer;
}
return ret;
};

var addSpacesToWords = function (msg) {
exports.addSpacesToWords = function (msg) {
return msg.replace(/([A-Z]+)/g, ' $1').replace(/([A-Z][a-z])/g, '$1');
};

var firstToUpperCaseAndAddSpace = function (msg) {
exports.firstToUpperCaseAndAddSpace = function (msg) {
msg = msg.replace('Bytes', '');
return msg.charAt(0).toUpperCase() + addSpacesToWords(msg.slice(1));
return msg.charAt(0).toUpperCase() + exports.addSpacesToWords(msg.slice(1));
};

var scoreColor = function(score) {
exports.scoreColor = function(score) {
var color = chalk.yellow;
color = score < 21 ? chalk.red : color;
color = score > 79 ? chalk.green : color;
return color;
};

var labelize = function(msg) {
var label = firstToUpperCaseAndAddSpace(msg);
return label + buffer(label) + chalk.grey('| ');
};

module.exports = {
scoreColor: scoreColor,
buffer: buffer,
divider: divider,
labelize: labelize,
firstToUpperCaseAndAddSpace: firstToUpperCaseAndAddSpace
exports.labelize = function(msg) {
var label = exports.firstToUpperCaseAndAddSpace(msg);
return label + exports.buffer(label) + chalk.grey('| ');
};
30 changes: 12 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
{
"name": "psi",
"version": "0.1.6",
"main": "index.js",
"description": "PageSpeed Insights for Node",
"homepage": "http://github.com/addyosmani/psi",
"bugs": "https://github.com/addyosmani/psi/issues",
"author": {
"name": "Addy Osmani",
"email": "[email protected]",
"url": "addyosmani.com"
},
"bin": {
"psi": "./bin/cli.js"
},
"bin": "cli.js",
"engines": {
"node": ">=0.10.0"
},
"repository": {
"type": "git",
"url": "https://github.com/addyosmani/psi"
},
"license": "Apache 2",
"repository": "addyosmani/psi",
"license": "Apache-2.0",
"scripts": {
"test": "mocha test"
},
"files": [
"cli.js",
"index.js",
"bin/cli.js",
"lib/output.js",
"lib/utils.js",
"lib/formats/cli.js",
"lib/formats/json.js",
"lib/formats/tap.js"
"lib"
],
"keywords": [
"pagespeed",
"insights"
"insights",
"speed",
"page",
"website",
"measure",
"optimize",
"size"
],
"dependencies": {
"chalk": "^0.5.1",
Expand Down

0 comments on commit 01a7ce2

Please sign in to comment.