66 ExportAudiences ,
77 AnyProperty ,
88} from '@contentstack/cli-variants' ;
9- import { handleAndLogError , messageHandler , log } from '@contentstack/cli-utilities' ;
9+ import { handleAndLogError , messageHandler , log , CLIProgressManager } from '@contentstack/cli-utilities' ;
1010
1111import { ModuleClassParams , ExportConfig } from '../../types' ;
1212import BaseClass from './base-class' ;
@@ -15,6 +15,20 @@ export default class ExportPersonalize extends BaseClass {
1515 public exportConfig : ExportConfig ;
1616 public personalizeConfig : { dirName : string ; baseURL : Record < string , string > } & AnyProperty ;
1717
18+ private readonly moduleInstanceMapper = {
19+ events : ExportEvents ,
20+ attributes : ExportAttributes ,
21+ audiences : ExportAudiences ,
22+ experiences : ExportExperiences ,
23+ } ;
24+
25+ private readonly moduleDisplayMapper = {
26+ events : 'Events' ,
27+ attributes : 'Attributes' ,
28+ audiences : 'Audiences' ,
29+ experiences : 'Experiences' ,
30+ } ;
31+
1832 constructor ( { exportConfig, stackAPIClient } : ModuleClassParams ) {
1933 super ( { exportConfig, stackAPIClient } ) ;
2034 this . exportConfig = exportConfig ;
@@ -32,6 +46,12 @@ export default class ExportPersonalize extends BaseClass {
3246 async ( ) => {
3347 const canProceed = this . validatePersonalizeSetup ( ) ;
3448 const moduleCount = canProceed ? this . getPersonalizeModuleCount ( ) : 0 ;
49+
50+ log . debug (
51+ `Personalize validation - canProceed: ${ canProceed } , moduleCount: ${ moduleCount } ` ,
52+ this . exportConfig . context ,
53+ ) ;
54+
3555 return [ canProceed , moduleCount ] ;
3656 } ,
3757 ) ;
@@ -41,61 +61,20 @@ export default class ExportPersonalize extends BaseClass {
4161 return ;
4262 }
4363
64+ log . debug ( `Creating personalize progress with moduleCount: ${ moduleCount } ` , this . exportConfig . context ) ;
4465 const progress = this . createNestedProgress ( this . currentModuleName ) ;
4566
46- // Add projects export process (always runs first)
47- progress . addProcess ( 'Projects' , 1 ) ;
48-
49- // Add personalize modules processes if enabled
50- if ( this . exportConfig . personalizationEnabled && moduleCount > 0 ) {
51- progress . addProcess ( 'Personalize Modules' , moduleCount ) ;
52- }
67+ this . addProjectProcess ( progress ) ;
68+ this . addModuleProcesses ( progress , moduleCount ) ;
5369
5470 try {
55- // Process projects export
56- progress . startProcess ( 'Projects' ) . updateStatus ( 'Exporting personalization projects...' , 'Projects' ) ;
57- log . debug ( 'Starting projects export for personalization...' , this . exportConfig . context ) ;
58- await new ExportProjects ( this . exportConfig ) . start ( ) ;
59- this . progressManager ?. tick ( true , 'projects export' , null , 'Projects' ) ;
60- progress . completeProcess ( 'Projects' , true ) ;
61-
62- if ( this . exportConfig . personalizationEnabled && moduleCount > 0 ) {
63- progress
64- . startProcess ( 'Personalize Modules' )
65- . updateStatus ( 'Processing personalize modules...' , 'Personalize Modules' ) ;
66- log . debug ( 'Personalization is enabled, processing personalize modules...' , this . exportConfig . context ) ;
67-
68- const moduleMapper = {
69- events : new ExportEvents ( this . exportConfig ) ,
70- attributes : new ExportAttributes ( this . exportConfig ) ,
71- audiences : new ExportAudiences ( this . exportConfig ) ,
72- experiences : new ExportExperiences ( this . exportConfig ) ,
73- } ;
74-
75- const order : ( keyof typeof moduleMapper ) [ ] = this . exportConfig . modules . personalize
76- . exportOrder as ( keyof typeof moduleMapper ) [ ] ;
77-
78- log . debug ( `Personalize export order: ${ order . join ( ', ' ) } ` , this . exportConfig . context ) ;
79-
80- for ( const module of order ) {
81- log . debug ( `Processing personalize module: ${ module } ` , this . exportConfig . context ) ;
82-
83- if ( moduleMapper [ module ] ) {
84- log . debug ( `Starting export for module: ${ module } ` , this . exportConfig . context ) ;
85- await moduleMapper [ module ] . start ( ) ;
86- this . progressManager ?. tick ( true , `module: ${ module } ` , null , 'Personalize Modules' ) ;
87- log . debug ( `Completed export for module: ${ module } ` , this . exportConfig . context ) ;
88- } else {
89- log . debug ( `Module not implemented: ${ module } ` , this . exportConfig . context ) ;
90- this . progressManager ?. tick ( false , `module: ${ module } ` , 'Module not implemented' , 'Personalize Modules' ) ;
91- log . info ( messageHandler . parse ( 'PERSONALIZE_MODULE_NOT_IMPLEMENTED' , module ) , this . exportConfig . context ) ;
92- }
93- }
94-
95- progress . completeProcess ( 'Personalize Modules' , true ) ;
96- log . debug ( 'Completed all personalize module exports' , this . exportConfig . context ) ;
71+ await this . exportProjects ( progress ) ;
72+
73+ if ( moduleCount > 0 ) {
74+ log . debug ( 'Processing personalize modules...' , this . exportConfig . context ) ;
75+ await this . exportModules ( progress ) ;
9776 } else {
98- log . debug ( 'Personalization is disabled, skipping personalize module exports ' , this . exportConfig . context ) ;
77+ log . debug ( 'No personalize modules configured for processing ' , this . exportConfig . context ) ;
9978 }
10079
10180 this . completeProgress ( true ) ;
@@ -105,7 +84,7 @@ export default class ExportPersonalize extends BaseClass {
10584 log . debug ( 'Personalize access forbidden, personalization not enabled' , this . exportConfig . context ) ;
10685 log . info ( messageHandler . parse ( 'PERSONALIZE_NOT_ENABLED' ) , this . exportConfig . context ) ;
10786 this . exportConfig . personalizationEnabled = false ;
108- this . completeProgress ( true ) ; // Complete successfully but with personalization disabled
87+ this . completeProgress ( true ) ; // considered successful even if skipped
10988 } else {
11089 log . debug ( 'Error occurred during personalize module processing' , this . exportConfig . context ) ;
11190 this . completeProgress ( false , moduleError ?. message || 'Personalize module processing failed' ) ;
@@ -142,4 +121,87 @@ export default class ExportPersonalize extends BaseClass {
142121 const order = this . exportConfig . modules ?. personalize ?. exportOrder ;
143122 return Array . isArray ( order ) ? order . length : 0 ;
144123 }
124+
125+ private addProjectProcess ( progress : CLIProgressManager ) {
126+ progress . addProcess ( 'Projects' , 1 ) ;
127+ log . debug ( 'Added Projects process to personalize progress' , this . exportConfig . context ) ;
128+ }
129+
130+ private addModuleProcesses ( progress : CLIProgressManager , moduleCount : number ) {
131+ if ( moduleCount > 0 ) {
132+ // talisman-ignore-start
133+ const order : ( keyof typeof this . moduleDisplayMapper ) [ ] = this . exportConfig . modules . personalize
134+ . exportOrder as ( keyof typeof this . moduleDisplayMapper ) [ ] ;
135+ // talisman-ignore-end
136+
137+ log . debug ( `Adding ${ order . length } personalize module processes: ${ order . join ( ', ' ) } ` , this . exportConfig . context ) ;
138+
139+ for ( const module of order ) {
140+ const processName = this . moduleDisplayMapper [ module ] ;
141+ progress . addProcess ( processName , 1 ) ;
142+ log . debug ( `Added ${ processName } process to personalize progress` , this . exportConfig . context ) ;
143+ }
144+ } else {
145+ log . debug ( 'No personalize modules to add to progress' , this . exportConfig . context ) ;
146+ }
147+ }
148+
149+ private async exportProjects ( progress : CLIProgressManager ) {
150+ progress . startProcess ( 'Projects' ) . updateStatus ( 'Exporting personalization projects...' , 'Projects' ) ;
151+ log . debug ( 'Starting projects export for personalization...' , this . exportConfig . context ) ;
152+
153+ const projectsExporter = new ExportProjects ( this . exportConfig ) ;
154+ projectsExporter . setParentProgressManager ( progress ) ;
155+ await projectsExporter . start ( ) ;
156+
157+ progress . completeProcess ( 'Projects' , true ) ;
158+ }
159+
160+ private async exportModules ( progress : CLIProgressManager ) {
161+ // Set parent progress for all module instances
162+ Object . entries ( this . moduleInstanceMapper ) . forEach ( ( [ _ , ModuleClass ] ) => {
163+ const instance = new ModuleClass ( this . exportConfig ) ;
164+ instance . setParentProgressManager ( progress ) ;
165+ } ) ;
166+
167+ // talisman-ignore-start
168+ const order : ( keyof typeof this . moduleInstanceMapper ) [ ] = this . exportConfig . modules . personalize
169+ . exportOrder as ( keyof typeof this . moduleInstanceMapper ) [ ] ;
170+ // talisman-ignore-end
171+
172+ log . debug ( `Personalize export order: ${ order . join ( ', ' ) } ` , this . exportConfig . context ) ;
173+
174+ for ( const module of order ) {
175+ log . debug ( `Processing personalize module: ${ module } ` , this . exportConfig . context ) ;
176+ const processName = this . moduleDisplayMapper [ module ] ;
177+ const ModuleClass = this . moduleInstanceMapper [ module ] ;
178+
179+ if ( ModuleClass ) {
180+ progress . startProcess ( processName ) . updateStatus ( `Exporting ${ module } ...` , processName ) ;
181+ log . debug ( `Starting export for module: ${ module } ` , this . exportConfig . context ) ;
182+
183+ if ( this . exportConfig . personalizationEnabled ) {
184+ const exporter = new ModuleClass ( this . exportConfig ) ;
185+ exporter . setParentProgressManager ( progress ) ;
186+ await exporter . start ( ) ;
187+
188+ progress . completeProcess ( processName , true ) ;
189+ log . debug ( `Completed export for module: ${ module } ` , this . exportConfig . context ) ;
190+ } else {
191+ log . debug ( `Skipping ${ module } - personalization not enabled` , this . exportConfig . context ) ;
192+ this . progressManager ?. tick ( true , `${ module } skipped (no project)` , null , processName ) ;
193+ progress . completeProcess ( processName , true ) ;
194+ log . info ( `Skipped ${ module } export - no personalize project found` , this . exportConfig . context ) ;
195+ }
196+ } else {
197+ log . debug ( `Module not implemented: ${ module } ` , this . exportConfig . context ) ;
198+ progress . startProcess ( processName ) . updateStatus ( `Module not implemented: ${ module } ` , processName ) ;
199+ this . progressManager ?. tick ( false , `module: ${ module } ` , 'Module not implemented' , processName ) ;
200+ progress . completeProcess ( processName , false ) ;
201+ log . info ( messageHandler . parse ( 'PERSONALIZE_MODULE_NOT_IMPLEMENTED' , module ) , this . exportConfig . context ) ;
202+ }
203+ }
204+
205+ log . debug ( 'Completed all personalize module processing' , this . exportConfig . context ) ;
206+ }
145207}
0 commit comments