forked from sindresorhus/speed-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·169 lines (142 loc) · 3.8 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node
'use strict';
const url = require('url');
const meow = require('meow');
const speedtest = require('speedtest-net');
const updateNotifier = require('update-notifier');
const roundTo = require('round-to');
const chalk = require('chalk');
const logUpdate = require('log-update');
const logSymbols = require('log-symbols');
const Ora = require('ora');
const cli = meow(`
Usage
$ speed-test
Options
--json -j Output the result as JSON
--bytes -b Output the result in megabytes per second (MBps)
--verbose -v Output more detailed information
`, {
flags: {
json: {
type: 'boolean',
alias: 'j'
},
bytes: {
type: 'boolean',
alias: 'b'
},
verbose: {
type: 'boolean',
alias: 'v'
}
}
});
updateNotifier({pkg: cli.pkg}).notify();
const stats = {
ping: '',
download: '',
upload: ''
};
let state = 'ping';
const spinner = new Ora();
const unit = cli.flags.bytes ? 'MBps' : 'Mbps';
const multiplier = cli.flags.bytes ? 1 / 8 : 1;
const getSpinner = x => state === x ? chalk.gray.dim(spinner.frame()) : ' ';
const logError = error => {
if (cli.flags.json) {
console.error(JSON.stringify({error}));
} else {
console.error(logSymbols.error, error);
}
};
function render() {
if (cli.flags.json) {
console.log(JSON.stringify(stats));
return;
}
let output = `
Ping ${getSpinner('ping')}${stats.ping}
Download ${getSpinner('download')}${stats.download}
Upload ${getSpinner('upload')}${stats.upload}`;
if (cli.flags.verbose) {
output += [
'',
' Server ' + (stats.data === undefined ? '' : chalk.cyan(stats.data.server.host)),
' Location ' + (stats.data === undefined ? '' : chalk.cyan(stats.data.server.location + chalk.dim(' (' + stats.data.server.country + ')'))),
' Distance ' + (stats.data === undefined ? '' : chalk.cyan(roundTo(stats.data.server.distance, 1) + chalk.dim(' km')))
].join('\n');
}
logUpdate(output);
}
function setState(s) {
state = s;
if (s && s.length > 0) {
stats[s] = chalk.yellow(`0 ${chalk.dim(unit)}`);
}
}
function map(server) {
/* eslint-disable prefer-destructuring */
server.host = url.parse(server.url).host;
server.location = server.name;
server.distance = server.dist;
return server;
}
const st = speedtest({maxTime: 20000});
if (!cli.flags.json) {
setInterval(render, 50);
}
st.once('testserver', server => {
if (cli.flags.verbose) {
stats.data = {
server: map(server)
};
}
setState('download');
const ping = Math.round(server.bestPing);
stats.ping = cli.flags.json ? ping : chalk.cyan(ping + chalk.dim(' ms'));
});
st.on('downloadspeedprogress', speed => {
if (state === 'download' && cli.flags.json !== true) {
speed *= multiplier;
const download = roundTo(speed, speed >= 10 ? 0 : 1);
stats.download = chalk.yellow(`${download} ${chalk.dim(unit)}`);
}
});
st.on('uploadspeedprogress', speed => {
if (state === 'upload' && cli.flags.json !== true) {
speed *= multiplier;
const upload = roundTo(speed, speed >= 10 ? 0 : 1);
stats.upload = chalk.yellow(`${upload} ${chalk.dim(unit)}`);
}
});
st.once('downloadspeed', speed => {
setState('upload');
speed *= multiplier;
const download = roundTo(speed, speed >= 10 && !cli.flags.json ? 0 : 1);
stats.download = cli.flags.json ? download : chalk.cyan(download + ' ' + chalk.dim(unit));
});
st.once('uploadspeed', speed => {
setState('');
speed *= multiplier;
const upload = roundTo(speed, speed >= 10 && !cli.flags.json ? 0 : 1);
stats.upload = cli.flags.json ? upload : chalk.cyan(upload + ' ' + chalk.dim(unit));
});
st.on('data', data => {
if (cli.flags.verbose) {
stats.data = data;
}
render();
});
st.on('done', () => {
console.log();
process.exit();
});
st.on('error', err => {
if (err.code === 'ENOTFOUND') {
logError('Please check your internet connection');
} else {
logError(err);
}
process.exit(1);
});