-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·53 lines (42 loc) · 1.3 KB
/
index.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
#!/usr/bin/env node
'use strict'
const meow = require('meow')
const chalk = require('chalk')
const speedTest = require('speedtest-net')
const updateNotifier = require('update-notifier')
const pkg = require('./package.json')
updateNotifier({pkg}).notify()
// silly usage
const cli = meow(`
Brought to you by @yuribrunetto (:)
COMMANDS (only one)
--version: to see spdtst's current version.
`, {
alias: {
version: pkg.version
}
})
// st
let st = speedTest({ maxTime: 20000 })
st.on('data', data => {
let download = (data.speeds.download * 125).toFixed(2)
let upload = (data.speeds.upload * 125).toFixed(2)
let ping = data.server.ping
let msg = chalk.magenta('\n-- Final results --\n')
msg += chalk.blue(`Download speed: ${download} kB/s\n`)
msg += chalk.blue(`Upload speed: ${upload} kB/s\n`)
msg += chalk.blue(`Latency: ${ping}ms`)
console.log(msg)
})
st.on('downloadspeedprogress', speed => {
let msg = chalk.green(`Download: ${(speed * 125).toFixed(2)} kB/s`)
console.log(msg)
})
st.on('uploadspeedprogress', speed => {
let msg = chalk.yellow(`Upload: ${(speed * 125).toFixed(2)} kB/s`)
console.log(msg)
})
st.on('error', err => {
if (err.code === 'ENOTFOUND')
console.error(chalk.bgRed.white('Unable to connect to the server. Please, check your internet connection.'))
})