From 01a7ce28d2eb09ddfd14e71e3be4a1c27f1776ae Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 8 Jan 2015 23:10:33 +0800 Subject: [PATCH] lots of tweaks --- .editorconfig | 7 ++----- .gitignore | 2 +- .jshintrc | 10 ++-------- README.md | 9 ++++----- bin/cli.js => cli.js | 4 ++-- index.js | 11 ++--------- lib/formats/cli.js | 4 +--- lib/formats/json.js | 20 +++++++------------- lib/formats/tap.js | 2 -- lib/output.js | 14 ++++++++------ lib/utils.js | 40 +++++++++++++--------------------------- package.json | 30 ++++++++++++------------------ 12 files changed, 54 insertions(+), 99 deletions(-) rename bin/cli.js => cli.js (95%) diff --git a/.editorconfig b/.editorconfig index 86c8f59..4a7ea30 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/.gitignore b/.gitignore index 91fa8cf..93f1361 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/node_modules/ +node_modules npm-debug.log diff --git a/.jshintrc b/.jshintrc index 39a8579..804f8af 100644 --- a/.jshintrc +++ b/.jshintrc @@ -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 } diff --git a/README.md b/README.md index 5338314..4cf5b40 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 --key= --prettyprint= --userIp= --locale= --strategy= --format diff --git a/bin/cli.js b/cli.js similarity index 95% rename from bin/cli.js rename to cli.js index 0701768..2e43924 100755 --- a/bin/cli.js +++ b/cli.js @@ -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 = {}; diff --git a/index.js b/index.js index c06b1ac..dc82c53 100755 --- a/index.js +++ b/index.js @@ -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'); @@ -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); }); }); }; diff --git a/lib/formats/cli.js b/lib/formats/cli.js index 70b6c40..f8fda99 100644 --- a/lib/formats/cli.js +++ b/lib/formats/cli.js @@ -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); diff --git a/lib/formats/json.js b/lib/formats/json.js index a0767fd..af24511 100644 --- a/lib/formats/json.js +++ b/lib/formats/json.js @@ -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); }; @@ -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)); }; diff --git a/lib/formats/tap.js b/lib/formats/tap.js index 35816a8..54cc246 100644 --- a/lib/formats/tap.js +++ b/lib/formats/tap.js @@ -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'; diff --git a/lib/output.js b/lib/output.js index 360e532..bfcedb7 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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 */ @@ -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}); @@ -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}); @@ -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; @@ -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; } }; }; diff --git a/lib/utils.js b/lib/utils.js index bdc3ee2..bcfd48c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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; @@ -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('| '); }; diff --git a/package.json b/package.json index 450e78c..c8397c9 100644 --- a/package.json +++ b/package.json @@ -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": "addyosmani@gmail.com", "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",