1 |
|
2 |
|
3 |
|
4 |
|
5 | const cliSpinners = require('cli-spinners'),
|
6 | inquirer = require('inquirer'),
|
7 | ora = require('ora'),
|
8 | extend = require('extend'),
|
9 | prettyTime = require('pretty-hrtime'),
|
10 | T = require('./tools');
|
11 |
|
12 | class LoadProgress {
|
13 | constructor(startText, endText) {
|
14 | this.startText = startText;
|
15 | this.endText = endText;
|
16 | this._type = 'bouncingBar';
|
17 | this._frames = cliSpinners[this._type].frames;
|
18 | this._interval = cliSpinners[this._type].interval;
|
19 | this.loader = this._frames.map(s => T.msg.yellow(`${s} ${this.startText}`));
|
20 | this.index = 0;
|
21 | this.stin = null;
|
22 | this.init();
|
23 | }
|
24 |
|
25 | init() {
|
26 | return this;
|
27 | }
|
28 |
|
29 | get frames() {
|
30 | return this._frames;
|
31 | }
|
32 |
|
33 | set frames(fams) {
|
34 | this._frames = fams;
|
35 | this.loader = this._frames.map(s => T.msg.yellow(`${s} ${this.startText}`));
|
36 | }
|
37 |
|
38 | get interval() {
|
39 | return this._interval;
|
40 | }
|
41 | set interval(val) {
|
42 | this._interval = val;
|
43 | }
|
44 |
|
45 | get type() {
|
46 | return this._type;
|
47 | }
|
48 | set type(type) {
|
49 | this._type = type;
|
50 | let spinner = cliSpinners[type];
|
51 | if (spinner) {
|
52 | this.frames = spinner.frames;
|
53 | this.interval = spinner.interval;
|
54 | }
|
55 | }
|
56 |
|
57 | setText(text) {
|
58 | this.loader = this._frames.map(s => T.msg.yellow(`${s} ${text || ''}`));
|
59 | }
|
60 |
|
61 | start() {
|
62 | this.ui = new inquirer.ui.BottomBar({ bottomBar: this.loader[this.index % this.loader.length] });
|
63 | this.stin = setInterval(() => {
|
64 | this.ui.updateBottomBar(this.loader[this.index++ % this.loader.length]);
|
65 | }, this._interval);
|
66 | return this;
|
67 | }
|
68 |
|
69 | stop() {
|
70 | clearInterval(this.stin);
|
71 | this.index = 0;
|
72 | this.ui.updateBottomBar(T.msg.green(this.endText) + '\n');
|
73 | return this;
|
74 | }
|
75 |
|
76 | close() {
|
77 | this.stop();
|
78 | this.ui = null;
|
79 | delete this.ui;
|
80 | process.exit();
|
81 | }
|
82 | }
|
83 |
|
84 | class LoadingORA {
|
85 | constructor(options) {
|
86 | this.options = extend(
|
87 | true,
|
88 | {
|
89 | text: T.msg.yellow(' Loading '),
|
90 | spinner: cliSpinners.dots12
|
91 | },
|
92 | options
|
93 | );
|
94 | this.spinner = ora(this.options);
|
95 | }
|
96 |
|
97 | set stream(s) {
|
98 | this.options.stream = s;
|
99 | this.spinner = ora(this.options);
|
100 | }
|
101 |
|
102 | text(txt) {
|
103 | this.spinner.text = T.msg.green(txt);
|
104 | }
|
105 |
|
106 | success(txt) {
|
107 |
|
108 | this.spinner.succeed(T.msg.green(txt.replace(/^√\s*/, '')));
|
109 | }
|
110 |
|
111 | fail(txt) {
|
112 | this.spinner.fail(T.msg.red(txt.replace(/^×\s*/, '')));
|
113 | }
|
114 |
|
115 | start(txt) {
|
116 | this.time = Date.now();
|
117 | this.spinner.start(txt);
|
118 | }
|
119 |
|
120 | stop(txt) {
|
121 |
|
122 | this.spinner.stop();
|
123 | txt && T.log.yellow(txt);
|
124 | }
|
125 | }
|
126 |
|
127 | module.exports = {
|
128 | LoadProgress,
|
129 | LoadingORA,
|
130 | cliSpinners
|
131 | };
|