|
1 |
| -import * as Debug from 'debug'; |
2 |
| -import * as ζinquirer from 'inquirer'; |
3 | 1 | import logUpdate = require('log-update');
|
4 | 2 | import { LogUpdate } from 'log-update';
|
5 | 3 |
|
6 | 4 | import { Colors, DEFAULT_COLORS } from './colors';
|
7 | 5 | import { ICON_FAILURE, ICON_SUCCESS, Spinner, TaskChain } from './tasks';
|
8 | 6 |
|
9 |
| -const debug = Debug('ionic:cli-framework:lib:output'); |
10 |
| - |
11 | 7 | export interface OutputStrategy {
|
12 | 8 | readonly stream: NodeJS.WritableStream;
|
13 | 9 | createTaskChain(): TaskChain;
|
@@ -105,104 +101,3 @@ export class LogUpdateOutputStrategy implements OutputStrategy, RedrawLine {
|
105 | 101 | return chain;
|
106 | 102 | }
|
107 | 103 | }
|
108 |
| - |
109 |
| -export interface BottomBarOutputStrategyOptions { |
110 |
| - readonly BottomBar: typeof ζinquirer.ui.BottomBar; |
111 |
| - readonly input?: NodeJS.ReadableStream; |
112 |
| - readonly output?: NodeJS.WritableStream; |
113 |
| - readonly colors?: Colors; |
114 |
| -} |
115 |
| - |
116 |
| -export class BottomBarOutputStrategy implements OutputStrategy, RedrawLine { |
117 |
| - protected bottomBar?: ζinquirer.ui.BottomBar; |
118 |
| - |
119 |
| - protected readonly BottomBar: typeof ζinquirer.ui.BottomBar; |
120 |
| - protected readonly rawinput: NodeJS.ReadableStream; |
121 |
| - protected readonly rawoutput: NodeJS.WritableStream; |
122 |
| - protected readonly colors: Colors; |
123 |
| - |
124 |
| - constructor({ BottomBar, input = process.stdin, output = process.stdout, colors = DEFAULT_COLORS }: BottomBarOutputStrategyOptions) { |
125 |
| - this.BottomBar = BottomBar; |
126 |
| - this.rawinput = input; |
127 |
| - this.rawoutput = output; |
128 |
| - this.colors = colors; |
129 |
| - } |
130 |
| - |
131 |
| - get stream(): NodeJS.WritableStream { |
132 |
| - const bottomBar = this.get(); |
133 |
| - return bottomBar.log; |
134 |
| - } |
135 |
| - |
136 |
| - redrawLine(msg = ''): void { |
137 |
| - const bottomBar = this.get(); |
138 |
| - bottomBar.updateBottomBar(msg); |
139 |
| - } |
140 |
| - |
141 |
| - get(): typeof ζinquirer.ui.BottomBar { |
142 |
| - if (!this.bottomBar) { |
143 |
| - this.bottomBar = new this.BottomBar({ input: this.rawinput, output: this.rawoutput } as any); |
144 |
| - |
145 |
| - try { |
146 |
| - // the mute() call appears to be necessary, otherwise when answering |
147 |
| - // inquirer prompts upon pressing enter, a copy of the prompt is |
148 |
| - // printed to the screen and looks gross |
149 |
| - (this.bottomBar as any).rl.output.mute(); |
150 |
| - } catch (e) { |
151 |
| - debug('Error while muting bottomBar output: %o', e); |
152 |
| - } |
153 |
| - } |
154 |
| - |
155 |
| - return this.bottomBar; |
156 |
| - } |
157 |
| - |
158 |
| - open(): void { |
159 |
| - this.get(); |
160 |
| - } |
161 |
| - |
162 |
| - close(): void { |
163 |
| - if (this.bottomBar) { |
164 |
| - // instantiating inquirer.ui.BottomBar hangs, so when close() is called, |
165 |
| - // close BottomBar streams |
166 |
| - this.bottomBar.close(); |
167 |
| - this.bottomBar = undefined; |
168 |
| - } |
169 |
| - } |
170 |
| - |
171 |
| - createTaskChain(): TaskChain { |
172 |
| - const { failure, strong, success } = this.colors; |
173 |
| - const chain = new TaskChain({ taskOptions: { tickInterval: 50 } }); |
174 |
| - |
175 |
| - this.open(); |
176 |
| - |
177 |
| - chain.on('next', task => { |
178 |
| - this.open(); |
179 |
| - |
180 |
| - task.on('success', () => { |
181 |
| - this.stream.write(`${success(ICON_SUCCESS)} ${task.msg} - done!`); |
182 |
| - }); |
183 |
| - |
184 |
| - task.on('failure', () => { |
185 |
| - this.stream.write(`${failure(ICON_FAILURE)} ${task.msg} - failed!`); |
186 |
| - }); |
187 |
| - |
188 |
| - const spinner = new Spinner(); |
189 |
| - |
190 |
| - task.on('tick', () => { |
191 |
| - const progress = task.progressRatio ? (task.progressRatio * 100).toFixed(2) : ''; |
192 |
| - const frame = spinner.frame(); |
193 |
| - |
194 |
| - this.redrawLine(`${strong(frame)} ${task.msg}${progress ? ' (' + strong(String(progress) + '%') + ')' : ''} `); |
195 |
| - }); |
196 |
| - |
197 |
| - task.on('clear', () => { |
198 |
| - this.redrawLine(''); |
199 |
| - }); |
200 |
| - }); |
201 |
| - |
202 |
| - chain.on('end', () => { |
203 |
| - this.close(); |
204 |
| - }); |
205 |
| - |
206 |
| - return chain; |
207 |
| - } |
208 |
| -} |
0 commit comments