This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.js
executable file
·103 lines (82 loc) · 2.86 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /usr/bin/env node
'use strict';
const dns = require('dns');
const chalk = require('chalk');
const isURL = require('is-url');
const ora = require('ora');
const argv = require('minimist')(process.argv.slice(2));
const logUpdate = require('log-update');
const logSymbols = require('log-symbols');
const updateNotifier = require('update-notifier');
const pagespeed = require('./pagespeed');
const pkg = require('./package.json');
updateNotifier({pkg}).notify();
const url = argv._;
if (argv.version && pkg.version !== false) {
console.log(pkg.version);
process.exit(0);
}
if (!url.length || argv.help === true) {
console.log(`
${chalk.cyan.bold('Usage:')}
pagespeed-score <url> <options>
${chalk.green.bold('Options:')}
--mobile Analyze the URL for mobile devices.
--filter-third-party Indicates if third party resources should be filtered out before PageSpeed analysis
${chalk.blue.bold('Example:')}
pagespeed-score https://google.com
`);
process.exit(0);
}
if (isURL(url) === false) {
console.log(`${chalk.bold.red('The given URL was not valid.')}`);
process.exit(1);
}
const spinner = ora();
dns.lookup('www.google.com', err => {
if (err && err.code === 'ENOTFOUND') {
spinner.stop();
console.log(chalk.bold.red('Please check you internet connection.'));
process.exit(1);
}
});
const strategy = (argv.mobile === true) ? 'mobile' : 'desktop';
const locale = ((typeof argv.locale === 'string')) ? argv.locale : 'en_US';
const filterThirdParty = (argv['filter-third-party'] === true) ? 'true' : 'false';
spinner.start();
spinner.text = chalk.cyan.bold('Calculating, please wait...');
pagespeed(url, strategy, locale, filterThirdParty).then(response => {
if (response.error && response.error.code === 400) {
spinner.stop();
logUpdate(
chalk.red.bold(`Could not resolve the URL: ${chalk.blue.bold(url)}
Please, check the spelling or make sure is accessible.'`)
);
}
const isToShowUsabilityScore = (strategy === 'mobile');
const speedScore = response.ruleGroups.SPEED.score;
let message;
if (speedScore < 21) {
message = chalk.red.bold(`${logSymbols.error} Score: ${speedScore}`);
} else if (speedScore < 80) {
message = chalk.yellow.bold(`${logSymbols.warning} Score: ${speedScore}`);
} else {
message = chalk.green.bold(`${logSymbols.success} Score: ${speedScore}`);
}
if (isToShowUsabilityScore) {
const usabilityScore = response.ruleGroups.USABILITY.score;
if (usabilityScore < 21) {
message += chalk.red.bold(`
${logSymbols.error} Usability: ${usabilityScore}`
);
} else if (usabilityScore < 80) {
message += chalk.yellow.bold(`
${logSymbols.warning} Usability: ${usabilityScore}`);
} else {
message += chalk.green.bold(`
${logSymbols.success} Usability: ${usabilityScore}`);
}
}
spinner.stop();
logUpdate(message);
});