forked from sindresorhus/fast-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·101 lines (81 loc) · 2.04 KB
/
cli.js
File metadata and controls
executable file
·101 lines (81 loc) · 2.04 KB
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
#!/usr/bin/env node
'use strict';
const dns = require('dns');
const meow = require('meow');
const chalk = require('chalk');
const logUpdate = require('log-update');
const ora = require('ora');
const api = require('./api');
const cli = meow(`
Usage
$ fast
$ fast > file
Options
--upload, -u Measure upload speed in addition to download speed
Examples
$ fast --upload > file && cat file
17 Mbps
4.4 Mbps
`, {
flags: {
upload: {
type: 'boolean',
alias: 'u'
}
}
});
// Check connections
dns.lookup('fast.com', error => {
if (error && error.code === 'ENOTFOUND') {
console.error(chalk.red('\n Please check your internet connection.\n'));
process.exit(1);
}
});
let data = {};
const spinner = ora();
const downloadSpeed = () =>
`${data.downloadSpeed} ${chalk.dim(data.downloadUnit)} ↓`;
const uploadSpeed = () =>
data.uploadSpeed ?
`${data.uploadSpeed} ${chalk.dim(data.uploadUnit)} ↑` :
chalk.dim('- Mbps ↑');
const uploadColor = string => (data.isDone ? chalk.green(string) : chalk.cyan(string));
const downloadColor = string => ((data.isDone || data.uploadSpeed) ? chalk.green(string) : chalk.cyan(string));
const speedText = () =>
cli.flags.upload ?
`${downloadColor(downloadSpeed())} ${chalk.dim('/')} ${uploadColor(uploadSpeed())}` :
downloadColor(downloadSpeed());
const speed = () => speedText() + '\n\n';
function exit() {
if (process.stdout.isTTY) {
logUpdate(`\n\n ${speed()}`);
} else {
let output = `${data.downloadSpeed} ${data.downloadUnit}`;
if (cli.flags.upload) {
output += `\n${data.uploadSpeed} ${data.uploadUnit}`;
}
console.log(output);
}
process.exit();
}
if (process.stdout.isTTY) {
setInterval(() => {
const pre = '\n\n ' + chalk.gray.dim(spinner.frame());
if (!data.downloadSpeed) {
logUpdate(pre + '\n\n');
return;
}
logUpdate(pre + speed());
}, 50);
}
(async () => {
try {
await api({measureUpload: cli.flags.upload}).forEach(result => {
data = result;
});
exit();
} catch (error) {
console.error(error.message);
process.exit(1);
}
})();