forked from sindresorhus/fast-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
167 lines (144 loc) · 3.51 KB
/
ui.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
'use strict';
const {promises: dns} = require('dns');
const React = require('react');
const {useState, useEffect} = require('react');
const {Box, Text, Newline, useApp, useStdout} = require('ink');
const Spinner = require('ink-spinner').default;
const api = require('./api.js');
const {convertToMpbs} = require('./utilities.js');
const FixedSpacer = ({size}) => (
<>{' '.repeat(size)}</>
);
const ErrorMessage = ({text}) => (
<Box>
<Text bold color="red">
›
<FixedSpacer size={1}/>
</Text>
<Text dimColor>
{text}
</Text>
<Newline count={2}/>
</Box>
);
const Spacer = ({singleLine}) => {
if (singleLine) {
return null;
}
return (
<Text>
<Newline count={1}/>
</Text>
);
};
const DownloadSpeed = ({isDone, downloadSpeed, uploadSpeed, downloadUnit} = {}) => {
const color = (isDone || uploadSpeed) ? 'green' : 'cyan';
return (
<Text color={color}>
{downloadSpeed}
<FixedSpacer size={1}/>
<Text dimColor>{downloadUnit}</Text>
<FixedSpacer size={1}/>
↓
</Text>
);
};
const UploadSpeed = ({isDone, uploadSpeed, uploadUnit} = {}) => {
const color = isDone ? 'green' : 'cyan';
if (uploadSpeed) {
return (
<Text color={color}>
{uploadSpeed}
<Text dimColor>
{` ${uploadUnit} ↑`}
</Text>
</Text>
);
}
return <Text dimColor color={color}>{' - Mbps ↑'}</Text>;
};
const Speed = ({upload, data}) => upload ? (
<>
<DownloadSpeed {...data}/>
<Text dimColor>{' / '}</Text>
<UploadSpeed {...data}/>
</>
) : (<DownloadSpeed {...data}/>);
const Fast = ({singleLine, upload, json}) => {
const [error, setError] = useState('');
const [data, setData] = useState({});
const [isDone, setIsDone] = useState(false);
const {exit} = useApp();
const {write} = useStdout();
useEffect(() => {
(async () => {
try {
await dns.lookup('fast.com');
} catch (error) {
setError(error.code === 'ENOTFOUND' ?
'Please check your internet connection' :
`Something happened ${JSON.stringify(error)}`
);
exit();
return;
}
// eslint-disable-next-line unicorn/no-array-for-each
api({measureUpload: upload}).forEach(result => {
if (!upload) {
delete result.uploaded;
delete result.uploadUnit;
delete result.uploadSpeed;
}
setData(result);
}).catch(error_ => { // eslint-disable-line promise/prefer-await-to-then
setError(error_.message);
exit();
});
})();
}, [exit, upload]);
useEffect(() => {
if (data.isDone || (!upload && data.uploadSpeed)) {
setIsDone(true);
}
}, [data.isDone, upload, data.uploadSpeed]);
useEffect(() => {
if (isDone) {
if (json) {
delete data.isDone;
data.downloadSpeed = convertToMpbs(data.downloadSpeed, data.downloadUnit);
delete data.downloadUnit;
if (upload) {
data.uploadSpeed = convertToMpbs(data.uploadSpeed, data.uploadUnit);
delete data.uploadUnit;
}
write(JSON.stringify(data, undefined, '\t'));
}
exit();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isDone, exit]);
if (error) {
return <ErrorMessage text={error}/>;
}
if (json) {
return null;
}
return (
<>
<Spacer singleLine={singleLine}/>
<Box>
{!isDone && (
<>
{!singleLine && <Text><FixedSpacer size={2}/></Text>}
<Text color="cyan"><Spinner/></Text>
<Text><FixedSpacer size={1}/></Text>
</>
)}
{isDone && <Text><FixedSpacer size={4}/></Text>}
{Object.keys(data).length > 0 && <Speed upload={upload} data={data}/>}
</Box>
<Spacer singleLine={singleLine}/>
</>
);
};
module.exports = Fast;