-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
251 lines (231 loc) · 6.04 KB
/
demo.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import https from 'node:https';
import path from 'node:path';
import { aForEach, sleep } from '@opentf/std';
import { style } from '@opentf/cli-styles';
import { ProgressBar } from './src';
function template(title, options = {}) {
console.log(style(`$o.bol.und{\n${title}\n}`));
return new ProgressBar(options);
}
function defaultBar() {
return new Promise((resolve) => {
const pBar = template('Default');
pBar.start({ total: 100 });
let value = 0;
const intervalID = setInterval(() => {
value += 10;
pBar.update({ value });
if (value === 100) {
pBar.stop();
clearInterval(intervalID);
resolve();
}
}, 100);
});
}
function mediumBar() {
return new Promise((resolve) => {
const pBar = template('Medium size + Blue bar', {
size: 'MEDIUM',
color: 'b',
});
pBar.start({ total: 100 });
let value = 0;
const intervalID = setInterval(() => {
value += 10;
pBar.update({ value });
if (value === 100) {
pBar.stop();
clearInterval(intervalID);
resolve();
}
}, 100);
});
}
function smallBar() {
return new Promise((resolve) => {
const pBar = template('Small size + Red bar', {
size: 'SMALL',
color: 'r',
});
pBar.start({ total: 100 });
let value = 0;
const intervalID = setInterval(() => {
value += 10;
pBar.update({ value });
if (value === 100) {
pBar.stop();
clearInterval(intervalID);
resolve();
}
}, 100);
});
}
function prefixSuffix() {
return new Promise((resolve) => {
const pBar = template('Prefix + Bar + Suffix');
pBar.start({ total: 100, prefix: 'Installing npm pkgs' });
const pkgs = ['babel', 'rollup', 'webpack', 'vite', 'react'];
let value = 0;
const intervalID = setInterval(() => {
value += 20;
pBar.update({ value, suffix: pkgs.pop() });
if (value === 100) {
pBar.update({ prefix: '', suffix: 'Install completed.' });
pBar.stop();
clearInterval(intervalID);
resolve();
}
}, 500);
});
}
async function downloading() {
const files = [
'https://nodejs.org/dist/v16.0.0/node-v16.0.0-x64.msi',
'https://nodejs.org/dist/v18.16.1/node-v18.16.1-x64.msi',
'https://nodejs.org/dist/v20.0.0/node-v20.0.0-x64.msi',
];
function download(url, pBar) {
return new Promise((resolve) => {
let bar;
let value = 0;
https
.get(url, (res) => {
bar = pBar.add({
total: +res.headers['content-length'],
});
res.on('data', (chunk) => {
value += chunk.length;
bar.update({ value, suffix: path.basename(url) });
});
res.on('end', () => {
resolve();
});
})
.on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
});
}
console.log('Downloading files:');
const downloadBar = template('Multi Progressbar', { size: 'MEDIUM' });
downloadBar.start();
await Promise.all(files.map((f) => download(f, downloadBar)));
downloadBar.stop();
}
async function autoClear() {
const pBar = template('Multi + Auto Clear + Stop msg', { autoClear: true });
console.log();
console.log('Compiling web application...');
console.log();
pBar.start();
const client = pBar.add({
total: 100,
color: 'rgb(0,181,34)',
prefix: style('$rgb(0,181,34){● Client}'),
});
const cfiles = pBar.add({
progress: false,
});
pBar.add({
progress: false,
});
const server = pBar.add({
total: 100,
color: 'hex(#FFB12B)',
prefix: style('$hex(#FFB12B){● Server}'),
});
const sfiles = pBar.add({
progress: false,
});
const pkgs = [
'react',
'zustand',
'recoil',
'lodash',
'@opentf/std',
'emotion',
'material-ui',
'pandacss',
'postcss',
'tailwind',
];
const sFiles = ['login', 'logout', 'products', 'dashboard', 'authorize'];
let cVal = 0;
let sVal = 0;
const intervalID = setInterval(() => {
if (cVal < 100) {
cVal += 10;
client.update({ value: cVal });
cfiles.update({
suffix: `/node_modules/${pkgs.pop()}/index.js`,
});
}
if (sVal < 100) {
sVal += 20;
server.update({ value: sVal });
sfiles.update({ suffix: `/api/${sFiles.pop()}.js` });
}
if (sVal === 100) {
sfiles.update({ suffix: '' });
}
if (cVal === 100) {
cfiles.update({ suffix: '' });
}
if (cVal === 100 && sVal === 100) {
pBar.stop('Successfully compiled, your app is now ready to deploy 🚀');
clearInterval(intervalID);
}
}, 100);
await sleep(1500);
}
async function styledTexts() {
const multiPBar = template('Styled Texts', {});
multiPBar.start();
const b1 = multiPBar.add({ total: 100 });
const t1 = multiPBar.add({ progress: false });
const b2 = multiPBar.add({ total: 100 });
const t2 = multiPBar.add({ progress: false });
const b3 = multiPBar.add({ total: 100 });
const t3 = multiPBar.add({ progress: false });
b1.update({ value: 23, prefix: 'ProgressBar 1', color: 'g' });
t1.update({ prefix: style('$r{This is some long text}') });
b3.update({
value: 35,
prefix: 'Prefix 3',
suffix: 'Suffix 3',
color: 'b',
bgColor: 'y',
});
t2.update({ prefix: 'This is some long text', color: 'bl', bgColor: 'y' });
b2.update({ value: 17, suffix: 'Suffix 2' });
t3.update({
prefix: 'This is some long text with invalid styles.',
color: 'bl2',
bgColor: 'y3',
});
multiPBar.stop();
}
async function plain() {
const arr = ['Apple', 'Mango', 'Orange', 'Grapes', 'Pear', 'Guava'];
const pBar = template('Plain', {
variant: 'PLAIN',
prefix: 'Downloading',
showPercent: true,
showCount: false,
});
pBar.start({ total: arr.length });
await aForEach(arr, async (f) => {
await sleep(500);
pBar.inc({ suffix: f });
});
pBar.stop();
}
await defaultBar();
await mediumBar();
await smallBar();
await prefixSuffix();
// await downloading();
await autoClear();
await styledTexts();
await plain();