@@ -3,24 +3,35 @@ import { render } from 'ink';
33import { EventEmitter } from 'node:events' ;
44import React from 'react' ;
55
6- import { ctx , Event } from '../../events/context.js' ;
6+ import { ctx , Event , ProcessName , SubProcessName } from '../../events/context.js' ;
77import { DefaultComponent } from '../components/default-component.js' ;
8+ import { ProgressState , ProgressStatus } from '../components/progress/progress-display.js' ;
89import { DisplayPlanStateTransition , RenderEvent , RenderState , Reporter } from './reporter.js' ;
910
11+ const ProgressLabelMapping = {
12+ [ ProcessName . APPLY ] : 'Applying plan...' ,
13+ [ ProcessName . PLAN ] : 'Generating plan...' ,
14+ [ SubProcessName . APPLY_RESOURCE ] : 'Applying resource' ,
15+ [ SubProcessName . GENERATE_PLAN ] : 'Generating plan' ,
16+ [ SubProcessName . INITIALIZE_PLUGINS ] : 'Initializing plugins' ,
17+ [ SubProcessName . PARSE ] : 'Parsing configs' ,
18+ [ SubProcessName . VALIDATE ] : 'Validating configs' ,
19+ }
20+
1021export class DefaultReporter implements Reporter {
1122
1223 private renderEmitter = new EventEmitter ( ) ;
13- private staticOutput = new Array < any > ( )
24+ private staticOutput = new Array < string > ( )
25+ private progressState : ProgressState | null = null
1426
1527 constructor ( ) {
1628 ctx . on ( Event . OUTPUT , ( ...args ) => this . renderLog ( ...args ) ) ;
17- ctx . on ( Event . PROCESS_START , ( name ) => this . onProcessEvent ( name ) )
29+ ctx . on ( Event . PROCESS_START , ( name ) => this . onProcessStartEvent ( name ) )
1830 ctx . on ( Event . PROCESS_FINISH , ( name ) => this . onProcessFinishEvent ( name ) )
19- ctx . on ( Event . SUB_PROCESS_START , ( name , processName ) => this . onSubprocessStartEvent ( name , processName ) ) ;
20- ctx . on ( Event . SUB_PROCESS_FINISH , ( name , processName ) => this . onSubprocessFinishEvent ( name , processName ) )
31+ ctx . on ( Event . SUB_PROCESS_START , ( name ) => this . onSubprocessStartEvent ( name ) ) ;
32+ ctx . on ( Event . SUB_PROCESS_FINISH , ( name ) => this . onSubprocessFinishEvent ( name ) )
2133
2234 render ( < DefaultComponent emitter = { this . renderEmitter } /> )
23-
2435 }
2536
2637 async promptConfirmation ( ) : Promise < boolean > {
@@ -44,67 +55,63 @@ export class DefaultReporter implements Reporter {
4455 } as DisplayPlanStateTransition ) ;
4556 }
4657
47- private renderLog ( ...args : unknown [ ] ) {
58+ private renderLog ( ...args : string [ ] ) {
4859 this . staticOutput . push ( ...args ) ;
4960 this . renderEmitter . emit ( RenderEvent . LOG , this . staticOutput ) ;
5061 }
5162
52- private onProcessEvent ( name : string ) : void {
53- this . processState . process . push ( {
63+ private onProcessStartEvent ( name : ProcessName ) : void {
64+ const label = ProgressLabelMapping [ name ] ;
65+
66+ this . progressState = {
5467 name,
55- status : ProcessStatus . IN_PROGRESS ,
56- subprocess : [ ] ,
57- } )
68+ label,
69+ status : ProgressStatus . IN_PROGRESS ,
70+ subProgresses : [ ] ,
71+ } ;
5872
59- this . renderLog ( `${ name } started` )
60- this . renderEmitter . emit ( RenderEvent . PROCESS_UPDATE , this . processState ) ;
73+ this . renderLog ( `${ label } started` )
74+ this . renderEmitter . emit ( RenderEvent . PROGRESS_UPDATE , this . progressState ) ;
6175 }
6276
63- private onProcessFinishEvent ( name : string ) : void {
64- const process = this . processState . process
65- . find ( ( process ) => process . name === name ) ;
66- if ( ! process ) {
67- return ;
68- }
77+ private onProcessFinishEvent ( name : ProcessName ) : void {
78+ const label = ProgressLabelMapping [ name ] ;
6979
70- process . status = ProcessStatus . FINISHED ;
80+ this . progressState ! . status = ProgressStatus . FINISHED ;
7181
72- this . renderLog ( `${ name } finished successfully` )
73- this . renderEmitter . emit ( RenderEvent . PROCESS_UPDATE , this . processState . process ) ;
82+ this . renderLog ( `${ label } finished successfully` )
83+ this . renderEmitter . emit ( RenderEvent . PROGRESS_UPDATE , this . progressState ) ;
7484
7585 }
7686
77- private onSubprocessStartEvent ( name : string , processName : string ) : void {
78- const process = this . processState . process
79- . find ( ( process ) => process . name === processName ) ;
80-
81- if ( ! process ) return ;
87+ private onSubprocessStartEvent ( name : SubProcessName ) : void {
88+ const label = ProgressLabelMapping [ name ] ;
8289
83- process . subprocess . push ( {
90+ this . progressState ?. subProgresses ? .push ( {
8491 name,
85- status : ProcessStatus . IN_PROGRESS ,
86- } )
92+ label,
93+ status : ProgressStatus . IN_PROGRESS ,
94+ } ) ;
8795
88- this . renderLog ( `${ name } started` )
89- this . renderEmitter . emit ( RenderEvent . PROCESS_UPDATE , this . processState ) ;
96+ this . renderLog ( `${ label } started` )
97+ this . renderEmitter . emit ( RenderEvent . PROGRESS_UPDATE , this . progressState ) ;
9098 }
9199
92- private onSubprocessFinishEvent ( name : string , processName : string ) : void {
93- const process = this . processState . process
94- . find ( ( process ) => process . name === processName ) ;
95- if ( ! process ) {
96- return ;
97- }
100+ private onSubprocessFinishEvent ( name : SubProcessName ) : void {
101+ const label = ProgressLabelMapping [ name ] ;
102+
103+ const subProgress = this . progressState
104+ ?. subProgresses
105+ ?. find ( ( p ) => p . name === name ) ;
98106
99- const subprocess = process . subprocess . find ( ( subprocess ) => subprocess . name === name )
100- if ( ! subprocess ) {
107+ if ( ! subProgress ) {
101108 return ;
102109 }
103110
104- subprocess . status = ProcessStatus . FINISHED ;
111+ subProgress . status = ProgressStatus . FINISHED ;
105112
106- this . renderLog ( `${ name } finished successfully` )
107- this . renderEmitter . emit ( RenderEvent . PROCESS_UPDATE , this . processState ) ;
113+ this . renderLog ( `${ label } finished successfully` )
114+ this . renderEmitter . emit ( RenderEvent . PROGRESS_UPDATE , this . progressState ) ;
108115 }
109116
110117}
0 commit comments