11import * as Debug from 'debug' ;
22import * as ζinquirer from 'inquirer' ;
3+ import * as ζlogUpdate from 'log-update' ;
34
45import { Colors , DEFAULT_COLORS } from './colors' ;
56import { ICON_FAILURE , ICON_SUCCESS , Spinner , TaskChain } from './tasks' ;
@@ -13,8 +14,6 @@ export interface OutputStrategy {
1314
1415export interface RedrawLine {
1516 redrawLine ( msg ?: string ) : void ;
16- open ( ) : void ;
17- close ( ) : void ;
1817}
1918
2019export interface StreamOutputStrategyOptions {
@@ -50,6 +49,63 @@ export class StreamOutputStrategy implements OutputStrategy {
5049 }
5150}
5251
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+
53109export interface BottomBarOutputStrategyOptions {
54110 readonly BottomBar : typeof ζinquirer . ui . BottomBar ;
55111 readonly input ?: NodeJS . ReadableStream ;
@@ -112,7 +168,7 @@ export class BottomBarOutputStrategy implements OutputStrategy, RedrawLine {
112168 }
113169 }
114170
115- createTaskChain ( ) {
171+ createTaskChain ( ) : TaskChain {
116172 const { failure, strong, success } = this . colors ;
117173 const chain = new TaskChain ( { taskOptions : { tickInterval : 50 } } ) ;
118174
0 commit comments