Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build failure #105

Merged
merged 18 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
'use strict';
const {google} = require('googleapis');
const prependHttp = require('prepend-http');
const pify = require('pify');
const output = require('./lib/output');
const {getOptions} = require('./lib/options-handler');

const {runpagespeed} = pify(google.pagespeedonline('v5').pagespeedapi);

function handleOpts(url, options) {
options = Object.assign({strategy: 'mobile'}, options);
options.nokey = options.key === undefined;
options.url = prependHttp(url);
return options;
}

const psi = (url, options) => Promise.resolve().then(() => {
if (!url) {
throw new Error('URL required');
}

return runpagespeed(handleOpts(url, options));
return runpagespeed(getOptions(url, options));
});

module.exports = psi;

module.exports.output = (url, options) => psi(url, options).then(data => output(handleOpts(url, options), data.data));
module.exports.output = (url, options) => psi(url, options).then(data => output(getOptions(url, options), data.data));
20 changes: 20 additions & 0 deletions lib/options-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const prependHttp = require('prepend-http');

const THRESHOLD = 70;

const getOptions = (url, options) => {
options = Object.assign({strategy: 'mobile'}, options);
options.nokey = options.key === undefined;
options.url = prependHttp(url);
return options;
};

const getThreshold = threshold => (typeof threshold === 'number' && threshold && threshold < 100) ? threshold : THRESHOLD;

const getReporter = format => ['cli', 'json', 'tap'].includes(format) ? format : 'cli';

module.exports = {
getOptions,
getThreshold,
getReporter
};
12 changes: 2 additions & 10 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
const sortOn = require('sort-on');
const humanizeUrl = require('humanize-url');
const prettyMs = require('pretty-ms');

const THRESHOLD = 70;
const {getThreshold, getReporter} = require('./../lib/options-handler');

function overview(url, strategy, scores) {
const ret = [];
Expand Down Expand Up @@ -84,18 +83,11 @@ const opportunities = lighthouseResult => {
return sortOn(ret, 'label');
};

function getReporter(format) {
const outputFormat = ['cli', 'json', 'tap'].includes(format) ? format : 'cli';
return require(`./formats/${outputFormat}`);
}

const convertToPercentum = num => num * 100;

const getThreshold = threshold => (typeof threshold === 'number' && threshold && threshold < 100) ? threshold : THRESHOLD;
JuanMaRuiz marked this conversation as resolved.
Show resolved Hide resolved

module.exports = (parameters, response) => {
return Promise.resolve().then(() => {
const renderer = getReporter(parameters.format);
const renderer = require(`./formats/${getReporter(parameters.format)}`);
const threshold = getThreshold(parameters.threshold);
const {lighthouseResult, loadingExperience, id} = response;

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": {
"name": "Addy Osmani",
"email": "addyosmani@gmail.com",
"url": "addyosmani.com"
"url": "https://addyosmani.com"
},
"bin": "cli.js",
"engines": {
Expand Down Expand Up @@ -35,7 +35,7 @@
"chalk": "^2.4.2",
"download": "^7.0.0",
"googleapis": "^38.0.0",
"humanize-url": "^1.0.1",
"humanize-url": "^2.1.0",
"lodash": "^4.17.11",
"meow": "^5.0.0",
"pify": "^4.0.0",
Expand All @@ -49,7 +49,7 @@
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.0.2",
"xo": "^0.24.0"
"xo": "^0.25.3"
},
"xo": {
"space": true
Expand Down
14 changes: 14 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-env mocha */
const {expect} = require('chai');

const psi = require('..');

describe('Index file', () => {
it('should throw an error if no url is passed to cli', async () => {
await psi('')
.then(result => result)
.catch(error => {
expect(error.message).to.eql('URL required');
});
});
});
49 changes: 49 additions & 0 deletions test/options-handler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-env mocha */
const {expect} = require('chai');

const {getOptions, getThreshold, getReporter} = require('../lib/options-handler');

describe('Options handler method', () => {
describe('getOptions method', () => {
const defaultConfig = {
nokey: true,
url: 'http://addyosmani.com'
};
it('should return strategy "mobile" if no strategy is passed', () => {
const expectedOutput = {
...defaultConfig,
strategy: 'mobile'
};
expect(getOptions('http://addyosmani.com')).to.eql(expectedOutput);
});
it('should respect passed strategy options', () => {
const expectedOutput = {
...defaultConfig,
strategy: 'desktop'
};
expect(getOptions('http://addyosmani.com', {strategy: 'desktop'})).to.eql(expectedOutput);
});
});

describe('getThreshold method', () => {
it('should return "70" if no valid value is passed', () => {
expect(getThreshold('string')).to.equal(70);
expect(getThreshold(false)).to.equal(70);
expect(getThreshold(101)).to.equal(70);
});
it('should respect passed threshold values', () => {
expect(getThreshold(80)).to.equal(80);
});
});

describe('getReporter method', () => {
it('should return "cli" if no valid format is passed', () => {
expect(getReporter('foo')).to.equal('cli');
});
it('should respect passed format if valid', () => {
expect(getReporter('cli')).to.equal('cli');
expect(getReporter('tap')).to.equal('tap');
expect(getReporter('json')).to.equal('json');
});
});
});