Skip to content

Commit

Permalink
fix code style and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 8, 2014
1 parent e8f289b commit 5267d8f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 61 deletions.
5 changes: 4 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# editorconfig.org
root = true

[*]
Expand All @@ -8,5 +7,9 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ Run mobile and desktop performance tests for your deployed site using [Google Pa

Uses [gpagespeed](https://github.com/zrrrzzt/gpagespeed/) and the reporter in [grunt-pagespeed](https://github.com/jrcryer/grunt-pagespeed).


## Install

```bash
```sh
$ npm install --save psi
```


## Preview

<img src="http://i.imgur.com/pbLR4pV.png"/>
![preview](http://i.imgur.com/pbLR4pV.png)


## Usage

Expand All @@ -35,45 +38,52 @@ psi({

Optionally, a callback is also available with access to the response:

```
psi(options, function(err, data){
```js
psi(options, function (err, data) {
console.log(data.score);
console.log(data.pageStats);
});
```

###Options
### Options

#### url

####url
Type: `String`
*Required*
Type: `string`

URL of the page for which the PageSpeed Insights API should generate results.

####locale
Type: `String`
#### locale

Type: `string`
Default: `en_US`

The locale that results should be generated in (e.g 'en_GB').

####strategy
Type: `String`
#### strategy

Type: `string`
Default: `desktop`

The strategy to use when analyzing the page. Valid values are desktop and mobile.

####threshold
Type: `Number`
#### threshold

Type: `number`
Default: `70`

Threshold score that is needed to pass the pagespeed test

####paths
Type: `Array`
#### paths

Type: `array`

An array of URL paths that are appended to the URL

####key
Type: `String`
#### key

Type: `string`
Default: `nokey`

[Google API Key](https://code.google.com/apis/console/)
Expand All @@ -85,23 +95,23 @@ Unless Specified defaults to use the free tier on PageSpeed Insights. Good for g

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

```
npm install -g psi
```sh
$ npm install --global psi
```

You can then casually use it with your key:

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

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

```
```sh
$ psi <url> --key=<key> --prettyprint=<true> --userIp=<userIp> --locale=<locale> --strategy=<desktop|mobile>
```

```
```sh
$ psi http://www.html5rocks.com --strategy=mobile
```

Expand All @@ -113,6 +123,7 @@ If you use Grunt, [grunt-pagespeed](https://github.com/jrcryer/grunt-pagespeed)

For testing local project, we recommend using [ngrok](http://www.jamescryer.com/2014/06/12/grunt-pagespeed-and-ngrok-locally-testing/).


## License

Apache 2.0
Expand Down
28 changes: 14 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ var pagespeed = require('gpagespeed');
var output = require('./output').init();

module.exports = function (opts, cb) {
opts = opts || {};
opts.strategy = opts.strategy || 'desktop';
opts.nokey = opts.key === void 0;
opts = opts || {};
cb = cb || function () {};
opts.strategy = opts.strategy || 'desktop';
opts.nokey = opts.key === undefined;

cb = cb || function () {};
console.log('Running Pagespeed Insights');
pagespeed(opts, function(err, data){
if (err) {
cb(err); return;
} else {
var response = JSON.parse(data);
output.process(opts, response);
cb(err, response);
}
});
pagespeed(opts, function (err, data) {
if (err) {
cb(err);
return;
}

var response = JSON.parse(data);
output.process(opts, response);
cb(err, response);
});
};
53 changes: 29 additions & 24 deletions output.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,44 @@ var chalk = require('chalk');
var utils = require('./lib/utils');

exports.init = function () {

var threshold = 70,
exports = {};
var threshold = 70;
var exports = {};

var generateScore = function (url, strategy, score) {
var color = utils.scoreColor(score);
score = 'Score: ' + color(score);
url = 'URL: ' + chalk.cyan(url);
strategy = 'Strategy: ' + chalk.cyan(strategy);

score = 'Score: ' + color(score);
url = 'URL: ' + chalk.cyan(url);
strategy = 'Strategy: ' + chalk.cyan(strategy);

return [url, score, strategy].join('\n') + '\n';
};

var generateRuleSetResults = function (rulesets) {
var result, ruleImpact, title, _results;
_results = [];
var result, ruleImpact, title;
var _results = [];

for (title in rulesets) {
result = rulesets[title];
ruleImpact = Math.ceil(result.ruleImpact * 100) / 100;
_results.push(utils.labelize(title) + chalk.cyan(ruleImpact));
}

return _results.join('\n');
};

var generateStatistics = function (statistics) {
var result, title, _results;
_results = [];
var result, title;
var _results = [];

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

_results.push(utils.labelize(title) + chalk.cyan(result));
}

return _results.join('\n');
};

Expand All @@ -49,24 +55,23 @@ exports.init = function () {
};

exports.process = function (parameters, response, done) {
var logger = console.log;
done = done || function () {};
threshold = parameters.threshold || threshold;

logger(
[
utils.divider,
generateScore(response.id, parameters.strategy, response.score),
generateStatistics(response.pageStats),
utils.labelize(''),
generateRuleSetResults(response.formattedResults.ruleResults),
utils.divider
].join('\n')
);
var logger = console.log;
done = done || function () {};
threshold = parameters.threshold || threshold;

logger([
utils.divider,
generateScore(response.id, parameters.strategy, response.score),
generateStatistics(response.pageStats),
utils.labelize(''),
generateRuleSetResults(response.formattedResults.ruleResults),
utils.divider
].join('\n'));

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

return done();
};

Expand Down

2 comments on commit 5267d8f

@samccone
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha I was going to do this but typically this is a rude thing to change 🎱

@sindresorhus
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samccone Hah, yeah, but only if it changes an existing style or if you do it in a unrelated pull request. This repo didn't really have any consistency.

Please sign in to comment.