Skip to content

Commit edd21ad

Browse files
committed
ES2015ify and require Node.js 4
1 parent 9ac737e commit edd21ad

File tree

14 files changed

+155
-177
lines changed

14 files changed

+155
-177
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
* text=auto
2+
*.js text eol=lf

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- '5'
4+
- '6'
55
- '4'
6-
- '0.12'
7-
- '0.10'

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ $ npm install --save psi
1919
```js
2020
const psi = require('psi');
2121

22-
// get the PageSpeed Insights report
22+
// Get the PageSpeed Insights report
2323
psi('theverge.com').then(data => {
2424
console.log(data.ruleGroups.SPEED.score);
2525
console.log(data.pageStats);
2626
});
2727

28-
// output a formatted report to the terminal
28+
// Output a formatted report to the terminal
2929
psi.output('theverge.com').then(() => {
3030
console.log('done');
3131
});
3232

3333
// Supply options to PSI and get back speed and usability scores
3434
psi('theverge.com', {nokey: 'true', strategy: 'mobile'}).then(data => {
35-
console.log('Speed score: ' + data.ruleGroups.SPEED.score);
36-
console.log('Usability score: ' + data.ruleGroups.USABILITY.score);
35+
console.log('Speed score:', data.ruleGroups.SPEED.score);
36+
console.log('Usability score:', data.ruleGroups.USABILITY.score);
3737
});
3838
```
3939

@@ -44,7 +44,7 @@ As of PSI 2.x, we expose both the PageSpeed Insights speed and usability scores.
4444

4545
### psi(url, [options])
4646

47-
Returns a promise for the response data from Google PageSpeed Insights.
47+
Returns a `Promise` for the response data from Google PageSpeed Insights.
4848

4949
#### url
5050

@@ -54,7 +54,7 @@ URL of the page for which the PageSpeed Insights API should generate results.
5454

5555
#### options
5656

57-
Type: `object`
57+
Type: `Object`
5858

5959
##### key
6060

@@ -67,7 +67,7 @@ When using this module for a production-level build process, registering for an
6767

6868
Type: `string`<br>
6969
Default: `mobile`<br>
70-
Values: `mobile`, `desktop`
70+
Values: `mobile` `desktop`
7171

7272
Strategy to use when analyzing the page.
7373

@@ -106,25 +106,25 @@ $ psi --help
106106
Usage
107107
$ psi <url>
108108
109-
Example
110-
$ psi todomvc.com --strategy=mobile
111-
112109
Options
113-
--key Google API Key. By default the free tier is used.
110+
--key Google API Key. By default the free tier is used
114111
--strategy Strategy to use when analyzing the page: mobile|desktop
115-
--format Output format: cli|json|tap
116-
--locale Locale results should be generated in.
117-
--threshold Threshold score to pass the PageSpeed test.
118-
--optimized Get the URL of optimized resources.
119-
--download Download optimized resources.
112+
--format Output format: cli|json|ta
113+
--locale Locale results should be generated in
114+
--threshold Threshold score to pass the PageSpeed tes
115+
--optimized Get the URL of optimized resources
116+
--download Download optimized resources
117+
118+
Example
119+
$ psi todomvc.com --strategy=mobile
120120
```
121121

122122

123123
## Getting PSI into your build process
124124

125125
A sample [Gulp](https://github.com/addyosmani/psi-gulp-sample) project using PSI is available.
126126

127-
If you use Grunt, [grunt-pagespeed](https://github.com/jrcryer/grunt-pagespeed) is a task by James Cryer that uses PSI under the hood.
127+
If you use Grunt, [`grunt-pagespeed`](https://github.com/jrcryer/grunt-pagespeed) is a task by James Cryer that uses PSI under the hood.
128128

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

cli.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
#!/usr/bin/env node
22
'use strict';
3-
var meow = require('meow');
4-
var updateNotifier = require('update-notifier');
5-
var psi = require('./');
3+
const meow = require('meow');
4+
const updateNotifier = require('update-notifier');
5+
const psi = require('.');
66

7-
var cli = meow([
8-
'Usage',
9-
' $ psi <url>',
10-
'',
11-
'Example',
12-
' $ psi todomvc.com --strategy=mobile',
13-
'',
14-
'Options',
15-
' --key Google API Key. By default the free tier is used.',
16-
' --strategy Strategy to use when analyzing the page: mobile|desktop',
17-
' --format Output format: cli|json|tap',
18-
' --locale Locale results should be generated in.',
19-
' --threshold Threshold score to pass the PageSpeed test.',
20-
' --optimized Get the URL of optimized resources.',
21-
' --download Download optimized resources.'
22-
]);
7+
const cli = meow(`
8+
Usage
9+
$ psi <url>
10+
11+
Options
12+
--key Google API Key. By default the free tier is used
13+
--strategy Strategy to use when analyzing the page: mobile|desktop
14+
--format Output format: cli|json|ta
15+
--locale Locale results should be generated in
16+
--threshold Threshold score to pass the PageSpeed tes
17+
--optimized Get the URL of optimized resources
18+
--download Download optimized resources
19+
20+
Example
21+
$ psi todomvc.com --strategy=mobile
22+
`);
2323

2424
updateNotifier({pkg: cli.pkg}).notify();
2525

2626
if (!cli.input[0]) {
27-
console.error('Please specify a URL');
27+
console.error('Specify a URL');
2828
process.exit(1);
2929
}
3030

31-
psi.output(cli.input[0], cli.flags).then(function () {
31+
psi.output(cli.input[0], cli.flags).then(() => {
3232
process.exit();
33-
}).catch(function (err) {
33+
}).catch(err => {
3434
if (err.noStack) {
3535
console.error(err.message);
3636
process.exit(1);

index.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
'use strict';
2-
var Promise = require('pinkie-promise');
3-
var googleapis = require('googleapis');
4-
var prependHttp = require('prepend-http');
5-
var objectAssign = require('object-assign');
6-
var pify = require('pify');
7-
var pagespeed = pify(googleapis.pagespeedonline('v2').pagespeedapi.runpagespeed, Promise);
8-
var output = require('./lib/output');
2+
const googleapis = require('googleapis');
3+
const prependHttp = require('prepend-http');
4+
const pify = require('pify');
5+
const output = require('./lib/output');
6+
7+
const pagespeed = pify(googleapis.pagespeedonline('v2').pagespeedapi.runpagespeed);
98

109
function handleOpts(url, opts) {
11-
opts = objectAssign({strategy: 'mobile'}, opts);
10+
opts = Object.assign({strategy: 'mobile'}, opts);
1211
opts.nokey = opts.key === undefined;
1312
opts.url = prependHttp(url);
1413
return opts;
1514
}
1615

17-
var psi = module.exports = function (url, opts) {
18-
return Promise.resolve().then(function () {
16+
const psi = module.exports = (url, opts) => {
17+
return Promise.resolve().then(() => {
1918
if (!url) {
2019
throw new Error('URL required');
2120
}
@@ -24,8 +23,4 @@ var psi = module.exports = function (url, opts) {
2423
});
2524
};
2625

27-
module.exports.output = function (url, opts) {
28-
return psi(url, opts).then(function (data) {
29-
return output(handleOpts(url, opts), data);
30-
});
31-
};
26+
module.exports.output = (url, opts) => psi(url, opts).then(data => output(handleOpts(url, opts), data));

lib/formats/cli.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
'use strict';
2-
var _ = require('lodash');
3-
var chalk = require('chalk');
4-
var utils = require('../utils');
2+
const _ = require('lodash');
3+
const chalk = require('chalk');
4+
const utils = require('../utils');
55

6-
module.exports = function (overview, statistics, ruleSetResults) {
7-
var renderOverview = function (item) {
8-
var color = item.label === 'Score' ? utils.scoreColor(item.value) : chalk.cyan;
6+
module.exports = (overview, statistics, ruleSetResults) => {
7+
const renderOverview = item => {
8+
const color = item.label === 'Score' ? utils.scoreColor(item.value) : chalk.cyan;
99
return item.label + ':' + utils.buffer(item.label, 11) + color(item.value);
1010
};
1111

12-
var renderSection = function (item) {
13-
return utils.labelize(item.label) + chalk.cyan(item.value);
14-
};
12+
const renderSection = item => utils.labelize(item.label) + chalk.cyan(item.value);
1513

1614
return [
1715
utils.divider,
1816
_.map(overview, renderOverview).join('\n') + '\n',
1917
_.map(statistics, renderSection).join('\n'),
2018
'',
21-
_.map(ruleSetResults.filter(function (el) {
22-
return el.value > 0;
23-
}), renderSection).join('\n'),
19+
_.map(ruleSetResults.filter(x => x.value > 0), renderSection).join('\n'),
2420
utils.divider
2521
].join('\n');
2622
};

lib/formats/json.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
'use strict';
2-
var _ = require('lodash');
2+
const _ = require('lodash');
33

4-
module.exports = function (overview, statistics, ruleSetResults) {
5-
var mapSection = function (section) {
6-
return section.label;
7-
};
8-
9-
var zip = function (section, labelMapping) {
10-
return _.zipObject(_.map(section, labelMapping), _.map(section, 'value'));
11-
};
4+
module.exports = (overview, statistics, ruleSetResults) => {
5+
const mapSection = section => section.label;
6+
const zip = (section, labelMapping) => _.zipObject(_.map(section, labelMapping), _.map(section, 'value'));
127

138
overview = zip(overview, 'label');
149
statistics = zip(statistics, mapSection);
1510
ruleSetResults = zip(ruleSetResults, mapSection);
1611

1712
return JSON.stringify({
18-
overview: overview,
19-
statistics: statistics,
13+
overview,
14+
statistics,
2015
ruleResults: ruleSetResults
2116
}, null, 2);
2217
};

lib/formats/tap.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
2-
module.exports = function (overview, statistics, ruleSetResults, threshold) {
3-
var outputTest = function (overview, threshold) {
4-
var score = overview[1].value;
5-
var result = score < threshold ? 'not ok' : 'ok';
6-
return result + ' 1 - psi';
2+
module.exports = (overview, statistics, ruleSetResults, threshold) => {
3+
const outputTest = (overview, threshold) => {
4+
const score = overview[1].value;
5+
const result = score < threshold ? 'not ok' : 'ok';
6+
return `${result} 1 - psi`;
77
};
88

99
return [

lib/output.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
'use strict';
2-
var Promise = require('pinkie-promise');
3-
var prettyBytes = require('pretty-bytes');
4-
var sortOn = require('sort-on');
5-
var humanizeUrl = require('humanize-url');
6-
var Download = require('download');
7-
var querystring = require('querystring');
8-
var pify = require('pify');
9-
var THRESHOLD = 70;
10-
var RESOURCE_URL = 'https://developers.google.com/speed/pagespeed/insights/optimizeContents?';
2+
const querystring = require('querystring');
3+
const prettyBytes = require('pretty-bytes');
4+
const sortOn = require('sort-on');
5+
const humanizeUrl = require('humanize-url');
6+
const Download = require('download');
7+
const pify = require('pify');
8+
9+
const THRESHOLD = 70;
10+
const RESOURCE_URL = 'https://developers.google.com/speed/pagespeed/insights/optimizeContents?';
1111

1212
function overview(url, strategy, scores) {
13-
var ret = [];
13+
const ret = [];
1414

1515
ret.push({
1616
label: 'URL',
@@ -38,10 +38,10 @@ function overview(url, strategy, scores) {
3838
}
3939

4040
function ruleSetResults(rulesets) {
41-
var ret = [];
41+
const ret = [];
4242

43-
for (var title in rulesets) {
44-
if (rulesets.hasOwnProperty(title)) {
43+
for (const title in rulesets) {
44+
if (Object.prototype.hasOwnProperty.call(rulesets, title)) {
4545
ret.push({
4646
label: title,
4747
value: Math.ceil(rulesets[title].ruleImpact * 100) / 100
@@ -53,10 +53,10 @@ function ruleSetResults(rulesets) {
5353
}
5454

5555
function statistics(stats) {
56-
var ret = [];
56+
const ret = [];
5757

58-
for (var title in stats) {
59-
if (stats.hasOwnProperty(title)) {
58+
for (const title in stats) {
59+
if (Object.prototype.hasOwnProperty.call(stats, title)) {
6060
ret.push({
6161
label: title,
6262
value: title.indexOf('Bytes') === -1 ? stats[title] : prettyBytes(Number(stats[title]))
@@ -69,14 +69,14 @@ function statistics(stats) {
6969

7070
function getReporter(format) {
7171
format = ['cli', 'json', 'tap'].indexOf(format) === -1 ? 'cli' : format;
72-
return require('./formats/' + format);
72+
return require(`./formats/${format}`); // eslint-disable-line import/no-dynamic-require
7373
}
7474

75-
module.exports = function (parameters, response) {
76-
return Promise.resolve().then(function () {
77-
var renderer = getReporter(parameters.format);
78-
var threshold = typeof parameters.threshold === 'number' ? parameters.threshold : THRESHOLD;
79-
var optimizedResourceURL = RESOURCE_URL + querystring.stringify({url: response.id, strategy: parameters.strategy});
75+
module.exports = (parameters, response) => {
76+
return Promise.resolve().then(() => {
77+
const renderer = getReporter(parameters.format);
78+
const threshold = typeof parameters.threshold === 'number' ? parameters.threshold : THRESHOLD;
79+
const optimizedResourceURL = RESOURCE_URL + querystring.stringify({url: response.id, strategy: parameters.strategy});
8080

8181
console.log(renderer(
8282
overview(humanizeUrl(response.id), parameters.strategy, response.ruleGroups),
@@ -86,21 +86,20 @@ module.exports = function (parameters, response) {
8686
));
8787

8888
if (parameters.optimized) {
89-
console.log('\nHere are your optimized images: ', humanizeUrl(optimizedResourceURL));
89+
console.log('\nHere are your optimized images:', humanizeUrl(optimizedResourceURL));
9090
}
9191

9292
if (response.ruleGroups.SPEED.score < threshold) {
93-
var err = new Error('Threshold of ' + threshold + ' not met with score of ' + response.ruleGroups.SPEED.score);
94-
throw err;
93+
throw new Error(`Threshold of ${threshold} not met with score of ${response.ruleGroups.SPEED.score}`);
9594
}
9695

9796
if (parameters.download) {
98-
var download = new Download()
97+
const download = new Download()
9998
.get(optimizedResourceURL)
10099
.dest('.')
101100
.rename('./optimized.zip');
102101

103-
return pify(download.run.bind(download), Promise)();
102+
return pify(download.run.bind(download))();
104103
}
105104
});
106105
};

0 commit comments

Comments
 (0)