@@ -3,53 +3,88 @@ import { SudoRequestData, SudoRequestResponseData } from 'codify-schemas';
33import readline from 'node:readline' ;
44
55import { Plan } from '../../entities/plan.js' ;
6+ import { ResourceConfig } from '../../entities/resource-config.js' ;
7+ import { ResourceInfo } from '../../entities/resource-info.js' ;
68import { Event , ctx } from '../../events/context.js' ;
7- import { ImportResult , RequiredParameters , UserSuppliedParameters } from '../../orchestrators/import.js' ;
9+ import { ImportResult } from '../../orchestrators/import.js' ;
10+ import { FileModificationResult } from '../../utils/file-modification-calculator.js' ;
811import { SudoUtils } from '../../utils/sudo.js' ;
912import { prettyFormatPlan } from '../plan-pretty-printer.js' ;
10- import { Reporter } from './reporter.js' ;
13+ import { PromptType , Reporter } from './reporter.js' ;
1114
1215export class PlainReporter implements Reporter {
1316 private readonly rl = readline . createInterface ( process . stdin , process . stdout ) ;
1417
15- constructor ( ) {
16- ctx . on ( Event . OUTPUT , ( ...args ) => console . log ( ...args ) )
17- ctx . on ( Event . PROCESS_START , ( name ) => console . log ( name ) )
18- ctx . on ( Event . PROCESS_FINISH , ( name ) => console . log ( name ) )
19- ctx . on ( Event . SUB_PROCESS_START , ( name ) => console . log ( name ) )
20- ctx . on ( Event . SUB_PROCESS_FINISH , ( name ) => console . log ( name ) )
18+ constructor ( attachListeners = true ) {
19+ if ( attachListeners ) {
20+ ctx . on ( Event . OUTPUT , ( ...args ) => console . log ( ...args ) )
21+ ctx . on ( Event . PROCESS_START , ( name ) => console . log ( name ) )
22+ ctx . on ( Event . PROCESS_FINISH , ( name ) => console . log ( name ) )
23+ ctx . on ( Event . SUB_PROCESS_START , ( name ) => console . log ( name ) )
24+ ctx . on ( Event . SUB_PROCESS_FINISH , ( name ) => console . log ( name ) )
25+ }
2126 }
2227
23- async promptUserForParameterValues (
24- requiredParameters : RequiredParameters
25- ) : Promise < UserSuppliedParameters > {
26- if ( requiredParameters . size > 0 || [ ...requiredParameters . values ( ) ] . reduce (
27- ( total , arr ) => arr . length + total , 0
28- ) ) {
29- console . log ( 'Some required information is needed for the import' ) ;
28+ async promptOptions ( message : string , options : string [ ] ) : Promise < number > {
29+ console . log ( message ) ;
30+ console . log ( '' )
31+
32+ const response = await new Promise ( ( resolve ) => {
33+ this . rl . question ( `${ options . map ( ( o , idx ) => `[${ idx } ] ${ o } ` ) . join ( ' ' ) } \n` , ( answer ) => resolve ( answer ) ) ;
34+ } ) ;
35+
36+ const parsedNumber = Number . parseInt ( response as string , 10 ) ;
37+ if ( ! Number . isInteger ( parsedNumber ) || parsedNumber < 0 || parsedNumber > options . length - 1 ) {
38+ throw new Error ( `Invalid response ${ response } ` )
39+ }
40+
41+ return Number . parseInt ( response as string , 10 ) ;
42+ }
43+
44+ displayFileModifications ( diffs : { file : string ; modification : FileModificationResult ; } [ ] ) : void {
45+ console . log ( chalk . bold ( 'File modifications\n' ) )
46+
47+ for ( const diff of diffs ) {
48+ console . log ( chalk . bold ( diff . file ) )
49+ console . log ( '' )
50+ console . log ( diff . modification . diff )
51+ console . log ( '' )
3052 }
53+ }
3154
32- const parameterInput = new Map < string , Record < string , unknown > > ( ) ;
55+ displayMessage ( message : string ) : void {
56+ console . log ( message ) ;
57+ }
3358
34- for ( const [ type , requiredParameter ] of requiredParameters . entries ( ) ) {
35- if ( requiredParameter . length > 0 ) {
36- console . log ( `Resource: "${ type } " requires additional information:` )
59+ async promptUserForValues (
60+ resourceInfoList : ResourceInfo [ ] ,
61+ promptType : PromptType
62+ ) : Promise < ResourceConfig [ ] > {
63+ const requiredParameters = resourceInfoList . flatMap ( ( r ) => r . getRequiredParameters ( ) )
64+ if ( requiredParameters . length > 0 ) {
65+ console . log ( 'Some required information is needed for the import' ) ;
66+ }
67+
68+ const result : ResourceConfig [ ] = [ ] ;
69+ for ( const resourceInfo of resourceInfoList ) {
70+ if ( resourceInfo . getRequiredParameters ( ) . length > 0 ) {
71+ console . log ( `Resource: "${ resourceInfo . type } " requires additional information:` )
3772 }
3873
74+ const requiredParameter = resourceInfo . getRequiredParameters ( )
75+ const configJson : Record < string , unknown > = { type : resourceInfo . type }
3976 for ( const parameter of requiredParameter ) {
4077 const response = await new Promise ( ( resolve ) => {
4178 this . rl . question ( `${ parameter . name } [${ parameter . type } ]: ` , ( answer ) => resolve ( answer ) ) ;
4279 } ) ;
4380
44- if ( ! parameterInput . has ( type ) ) {
45- parameterInput . set ( type , { } ) ;
46- }
47-
48- parameterInput . get ( type ) ! [ parameter . name ] = response ;
81+ configJson [ parameter . name ] = response ;
4982 }
83+
84+ result . push ( new ResourceConfig ( configJson as any ) )
5085 }
5186
52- return parameterInput ;
87+ return result ;
5388 }
5489
5590 displayImportResult ( importResult : ImportResult ) {
0 commit comments