1
1
import * as Debug from 'debug' ;
2
2
import * as ζinquirer from 'inquirer' ;
3
+ import * as ζlogUpdate from 'log-update' ;
3
4
4
5
import { Colors , DEFAULT_COLORS } from './colors' ;
5
6
import { ICON_FAILURE , ICON_SUCCESS , Spinner , TaskChain } from './tasks' ;
@@ -13,8 +14,6 @@ export interface OutputStrategy {
13
14
14
15
export interface RedrawLine {
15
16
redrawLine ( msg ?: string ) : void ;
16
- open ( ) : void ;
17
- close ( ) : void ;
18
17
}
19
18
20
19
export interface StreamOutputStrategyOptions {
@@ -50,6 +49,63 @@ export class StreamOutputStrategy implements OutputStrategy {
50
49
}
51
50
}
52
51
52
+ export interface LogUpdateOutputStrategyOptions {
53
+ readonly LogUpdate : typeof ζlogUpdate ;
54
+ readonly stream ?: NodeJS . WritableStream ;
55
+ readonly colors ?: Colors ;
56
+ }
57
+
58
+ export class LogUpdateOutputStrategy implements OutputStrategy , RedrawLine {
59
+ readonly stream : NodeJS . WritableStream ;
60
+
61
+ protected readonly colors : Colors ;
62
+ protected readonly logUpdate : typeof ζlogUpdate ;
63
+
64
+ constructor ( { LogUpdate, stream = process . stdout , colors = DEFAULT_COLORS } : LogUpdateOutputStrategyOptions ) {
65
+ this . stream = stream ;
66
+ this . colors = colors ;
67
+ this . logUpdate = LogUpdate . create ( stream ) ;
68
+ }
69
+
70
+ redrawLine ( msg = '' ) : void {
71
+ this . logUpdate ( msg ) ;
72
+ }
73
+
74
+ createTaskChain ( ) : TaskChain {
75
+ const { failure, strong, success } = this . colors ;
76
+ const chain = new TaskChain ( { taskOptions : { tickInterval : 50 } } ) ;
77
+
78
+ chain . on ( 'next' , task => {
79
+ task . on ( 'success' , ( ) => {
80
+ this . stream . write ( `${ success ( ICON_SUCCESS ) } ${ task . msg } - done!\n` ) ;
81
+ } ) ;
82
+
83
+ task . on ( 'failure' , ( ) => {
84
+ this . stream . write ( `${ failure ( ICON_FAILURE ) } ${ task . msg } - failed!\n` ) ;
85
+ } ) ;
86
+
87
+ const spinner = new Spinner ( ) ;
88
+
89
+ task . on ( 'tick' , ( ) => {
90
+ const progress = task . progressRatio ? ( task . progressRatio * 100 ) . toFixed ( 2 ) : '' ;
91
+ const frame = spinner . frame ( ) ;
92
+
93
+ this . redrawLine ( `${ strong ( frame ) } ${ task . msg } ${ progress ? ' (' + strong ( String ( progress ) + '%' ) + ')' : '' } ` ) ;
94
+ } ) ;
95
+
96
+ task . on ( 'clear' , ( ) => {
97
+ this . logUpdate . clear ( ) ;
98
+ } ) ;
99
+ } ) ;
100
+
101
+ chain . on ( 'end' , ( ) => {
102
+ this . logUpdate . done ( ) ;
103
+ } ) ;
104
+
105
+ return chain ;
106
+ }
107
+ }
108
+
53
109
export interface BottomBarOutputStrategyOptions {
54
110
readonly BottomBar : typeof ζinquirer . ui . BottomBar ;
55
111
readonly input ?: NodeJS . ReadableStream ;
@@ -112,7 +168,7 @@ export class BottomBarOutputStrategy implements OutputStrategy, RedrawLine {
112
168
}
113
169
}
114
170
115
- createTaskChain ( ) {
171
+ createTaskChain ( ) : TaskChain {
116
172
const { failure, strong, success } = this . colors ;
117
173
const chain = new TaskChain ( { taskOptions : { tickInterval : 50 } } ) ;
118
174
0 commit comments