-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
executable file
·62 lines (50 loc) · 1.64 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
#!/usr/bin/env node
'use strict';
const chalk = require('chalk');
const meow = require('meow');
const speedTest = require('speedtest-net');
const Table = require('cli-table3');
const ora = require('ora');
const spinner = ora();
// Help message
meow(`
Usage:
Just run ${chalk.green.bold('speedo')} to start a speed test!
Powered by ${chalk.cyan('speedtest.net')}
`);
// Speed test configuration
const st = speedTest({maxTime: 5250});
// Output in MB/s
st.on('data', async data => {
const download = await (data.speeds.download * 0.125).toFixed(2);
const upload = await (data.speeds.upload * 0.125).toFixed(2);
// Table
const table = new Table({
head: [`${chalk.red.bold('Type:')}`, `${chalk.red.bold('Speed:')}`],
colWidths: [25, 25]
});
table.push(
[`${chalk.cyan('Download')}`, `${chalk.green(`${download}`)} MB/s`],
[`${chalk.cyan('Upload')}`, `${chalk.green(`${upload}`)} MB/s`],
[`${chalk.cyan('Latency')}`, `${chalk.green(`${data.server.ping}`)} ms`]
);
// Print the final report table
spinner.succeed('Done! Here is your speed report:\n');
console.log(table.toString());
});
// Download and Upload speed log
st.on('downloadspeedprogress', async speed => {
spinner.text = `Testing download speed... ${chalk.green(`${(await speed * 0.125).toFixed(2)} MB/s`)}`;
spinner.start();
});
st.on('uploadspeedprogress', async speed => {
spinner.text = `Testing upload speed... ${chalk.yellow(`${(await speed * 0.125).toFixed(2)} MB/s`)}`;
spinner.start();
});
// Handle the error
st.on('error', err => {
/* istanbul ignore next */
if (err.code === 'ENOTFOUND') {
console.error(chalk.red.bold('Unable to connect to the server :('));
}
});