@@ -73,8 +73,9 @@ export class QueryExporter {
7373 const branch = await this . stackAPIClient
7474 . branch ( branchName )
7575 . fetch ( { include_settings : true } as Record < string , unknown > ) ;
76- const linked = ( branch as { settings ?: { am_v2 ?: { linked_workspaces ?: QueryExportConfig [ 'linkedWorkspaces' ] } } } )
77- ?. settings ?. am_v2 ?. linked_workspaces ;
76+ const linked = (
77+ branch as { settings ?: { am_v2 ?: { linked_workspaces ?: QueryExportConfig [ 'linkedWorkspaces' ] } } }
78+ ) ?. settings ?. am_v2 ?. linked_workspaces ;
7879 this . exportQueryConfig . linkedWorkspaces = Array . isArray ( linked ) ? linked : [ ] ;
7980 log . debug (
8081 `Linked workspaces for Contentstack Assets: ${ this . exportQueryConfig . linkedWorkspaces ?. length ?? 0 } ` ,
@@ -90,10 +91,7 @@ export class QueryExporter {
9091 }
9192
9293 private isCsAssetsExport ( ) : boolean {
93- return (
94- ( this . exportQueryConfig . linkedWorkspaces ?. length ?? 0 ) > 0 &&
95- Boolean ( this . exportQueryConfig . csAssetsUrl )
96- ) ;
94+ return ( this . exportQueryConfig . linkedWorkspaces ?. length ?? 0 ) > 0 && Boolean ( this . exportQueryConfig . csAssetsUrl ) ;
9795 }
9896
9997 /**
@@ -156,14 +154,8 @@ export class QueryExporter {
156154 log . info ( 'Starting export of referenced content types and dependent modules...' , this . exportQueryConfig . context ) ;
157155
158156 try {
159- const ctPath = path . join (
160- sanitizePath ( this . exportQueryConfig . exportDir ) ,
161- 'content_types' ,
162- ) ;
163- const gfPath = path . join (
164- sanitizePath ( this . exportQueryConfig . exportDir ) ,
165- 'global_fields' ,
166- ) ;
157+ const ctPath = path . join ( sanitizePath ( this . exportQueryConfig . exportDir ) , 'content_types' ) ;
158+ const gfPath = path . join ( sanitizePath ( this . exportQueryConfig . exportDir ) , 'global_fields' ) ;
167159
168160 const referencedHandler = new ReferencedContentTypesHandler ( this . exportQueryConfig ) ;
169161 const dependenciesHandler = new ContentTypeDependenciesHandler ( this . stackAPIClient , this . exportQueryConfig ) ;
@@ -265,15 +257,21 @@ export class QueryExporter {
265257 }
266258
267259 if ( ! foundNewCTs && ! foundNewGFs ) {
268- log . info ( 'Schema closure complete, no new content types or global fields found' , this . exportQueryConfig . context ) ;
260+ log . info (
261+ 'Schema closure complete, no new content types or global fields found' ,
262+ this . exportQueryConfig . context ,
263+ ) ;
269264 break ;
270265 }
271266 }
272267
273268 // Personalize is a single global module exported once after the closure stabilises.
274269 await this . moduleExporter . exportModule ( 'personalize' ) ;
275270
276- log . success ( 'Referenced content types and dependent modules exported successfully' , this . exportQueryConfig . context ) ;
271+ log . success (
272+ 'Referenced content types and dependent modules exported successfully' ,
273+ this . exportQueryConfig . context ,
274+ ) ;
277275 } catch ( error ) {
278276 handleAndLogError ( error , this . exportQueryConfig . context , 'Error during schema closure expansion' ) ;
279277 throw error ;
@@ -374,11 +372,7 @@ export class QueryExporter {
374372 * Export referenced assets into stack assets/ via CMA export module.
375373 */
376374 private async exportReferencedStackAssets ( assetUIDs : string [ ] ) : Promise < void > {
377- const assetsDir = path . join (
378- sanitizePath ( this . exportQueryConfig . exportDir ) ,
379- sanitizePath ( this . exportQueryConfig . branchName || '' ) ,
380- 'assets' ,
381- ) ;
375+ const assetsDir = path . join ( sanitizePath ( this . exportQueryConfig . exportDir ) , 'assets' ) ;
382376
383377 const metadataFilePath = path . join ( assetsDir , 'metadata.json' ) ;
384378 const assetFilePath = path . join ( assetsDir , 'assets.json' ) ;
@@ -388,101 +382,101 @@ export class QueryExporter {
388382 const tempAssetFilePath = path . join ( assetsDir , 'assets_temp.json' ) ;
389383
390384 try {
391- fs . mkdirSync ( assetsDir , { recursive : true } ) ;
392-
393- // Define batch size - can be configurable through exportQueryConfig
394- const batchSize = this . exportQueryConfig . assetBatchSize || 100 ;
395-
396- // if asset size is bigger than batch size, then we need to export in batches
397- // Calculate number of batches
398- const totalBatches = Math . ceil ( assetUIDs . length / batchSize ) ;
399- log . info ( `Processing assets in ${ totalBatches } batches of ${ batchSize } ` , this . exportQueryConfig . context ) ;
385+ fs . mkdirSync ( assetsDir , { recursive : true } ) ;
386+
387+ // Define batch size - can be configurable through exportQueryConfig
388+ const batchSize = this . exportQueryConfig . assetBatchSize || 100 ;
389+
390+ // if asset size is bigger than batch size, then we need to export in batches
391+ // Calculate number of batches
392+ const totalBatches = Math . ceil ( assetUIDs . length / batchSize ) ;
393+ log . info ( `Processing assets in ${ totalBatches } batches of ${ batchSize } ` , this . exportQueryConfig . context ) ;
394+
395+ // Process assets in batches
396+ for ( let i = 0 ; i < totalBatches ; i ++ ) {
397+ const start = i * batchSize ;
398+ const end = Math . min ( start + batchSize , assetUIDs . length ) ;
399+ const batchAssetUIDs = assetUIDs . slice ( start , end ) ;
400+
401+ log . info (
402+ `Exporting batch ${ i + 1 } /${ totalBatches } (${ batchAssetUIDs . length } assets)...` ,
403+ this . exportQueryConfig . context ,
404+ ) ;
405+
406+ const query = {
407+ modules : {
408+ assets : {
409+ uid : { $in : batchAssetUIDs } ,
410+ } ,
411+ } ,
412+ } ;
413+
414+ await this . moduleExporter . exportModule ( 'assets' , { query } ) ;
415+
416+ // Read the current batch's metadata.json and assets.json files
417+ const currentMetadata : any = fsUtil . readFile ( sanitizePath ( metadataFilePath ) ) ;
418+ const currentAssets : any = fsUtil . readFile ( sanitizePath ( assetFilePath ) ) ;
419+
420+ // Check if this is the first batch
421+ if ( i === 0 ) {
422+ // For first batch, initialize temp files with current content
423+ fsUtil . writeFile ( sanitizePath ( tempMetadataFilePath ) , currentMetadata ) ;
424+ fsUtil . writeFile ( sanitizePath ( tempAssetFilePath ) , currentAssets ) ;
425+ log . info ( `Initialized temporary files with first batch data` , this . exportQueryConfig . context ) ;
426+ } else {
427+ // For subsequent batches, append to temp files with incremented keys
428+
429+ // Handle metadata (which contains arrays of asset info)
430+ const tempMetadata : any = fsUtil . readFile ( sanitizePath ( tempMetadataFilePath ) ) || { } ;
431+
432+ // Merge metadata by combining arrays
433+ if ( currentMetadata ) {
434+ Object . keys ( currentMetadata ) . forEach ( ( key : string ) => {
435+ if ( ! tempMetadata [ key ] ) {
436+ tempMetadata [ key ] = currentMetadata [ key ] ;
437+ }
438+ } ) ;
439+ }
400440
401- // Process assets in batches
402- for ( let i = 0 ; i < totalBatches ; i ++ ) {
403- const start = i * batchSize ;
404- const end = Math . min ( start + batchSize , assetUIDs . length ) ;
405- const batchAssetUIDs = assetUIDs . slice ( start , end ) ;
441+ // Write updated metadata back to temp file
442+ fsUtil . writeFile ( sanitizePath ( tempMetadataFilePath ) , tempMetadata ) ;
406443
407- log . info (
408- `Exporting batch ${ i + 1 } /${ totalBatches } (${ batchAssetUIDs . length } assets)...` ,
409- this . exportQueryConfig . context ,
410- ) ;
444+ // Handle assets (which is an object with numeric keys)
445+ const tempAssets : any = fsUtil . readFile ( sanitizePath ( tempAssetFilePath ) ) || { } ;
446+ let nextIndex = Object . keys ( tempAssets ) . length + 1 ;
411447
412- const query = {
413- modules : {
414- assets : {
415- uid : { $in : batchAssetUIDs } ,
416- } ,
417- } ,
418- } ;
419-
420- await this . moduleExporter . exportModule ( 'assets' , { query } ) ;
421-
422- // Read the current batch's metadata.json and assets.json files
423- const currentMetadata : any = fsUtil . readFile ( sanitizePath ( metadataFilePath ) ) ;
424- const currentAssets : any = fsUtil . readFile ( sanitizePath ( assetFilePath ) ) ;
425-
426- // Check if this is the first batch
427- if ( i === 0 ) {
428- // For first batch, initialize temp files with current content
429- fsUtil . writeFile ( sanitizePath ( tempMetadataFilePath ) , currentMetadata ) ;
430- fsUtil . writeFile ( sanitizePath ( tempAssetFilePath ) , currentAssets ) ;
431- log . info ( `Initialized temporary files with first batch data` , this . exportQueryConfig . context ) ;
432- } else {
433- // For subsequent batches, append to temp files with incremented keys
434-
435- // Handle metadata (which contains arrays of asset info)
436- const tempMetadata : any = fsUtil . readFile ( sanitizePath ( tempMetadataFilePath ) ) || { } ;
437-
438- // Merge metadata by combining arrays
439- if ( currentMetadata ) {
440- Object . keys ( currentMetadata ) . forEach ( ( key : string ) => {
441- if ( ! tempMetadata [ key ] ) {
442- tempMetadata [ key ] = currentMetadata [ key ] ;
443- }
444- } ) ;
445- }
446-
447- // Write updated metadata back to temp file
448- fsUtil . writeFile ( sanitizePath ( tempMetadataFilePath ) , tempMetadata ) ;
449-
450- // Handle assets (which is an object with numeric keys)
451- const tempAssets : any = fsUtil . readFile ( sanitizePath ( tempAssetFilePath ) ) || { } ;
452- let nextIndex = Object . keys ( tempAssets ) . length + 1 ;
453-
454- // Add current assets with incremented keys
455- Object . values ( currentAssets ) . forEach ( ( value : any ) => {
456- tempAssets [ nextIndex . toString ( ) ] = value ;
457- nextIndex ++ ;
458- } ) ;
448+ // Add current assets with incremented keys
449+ Object . values ( currentAssets ) . forEach ( ( value : any ) => {
450+ tempAssets [ nextIndex . toString ( ) ] = value ;
451+ nextIndex ++ ;
452+ } ) ;
459453
460- fsUtil . writeFile ( sanitizePath ( tempAssetFilePath ) , tempAssets ) ;
454+ fsUtil . writeFile ( sanitizePath ( tempAssetFilePath ) , tempAssets ) ;
461455
462- log . info ( `Updated temporary files with batch ${ i + 1 } data` , this . exportQueryConfig . context ) ;
463- }
456+ log . info ( `Updated temporary files with batch ${ i + 1 } data` , this . exportQueryConfig . context ) ;
457+ }
464458
465- // Optional: Add delay between batches to avoid rate limiting
466- if ( i < totalBatches - 1 && this . exportQueryConfig . batchDelayMs ) {
467- await new Promise ( ( resolve ) => setTimeout ( resolve , this . exportQueryConfig . batchDelayMs ) ) ;
468- }
459+ // Optional: Add delay between batches to avoid rate limiting
460+ if ( i < totalBatches - 1 && this . exportQueryConfig . batchDelayMs ) {
461+ await new Promise ( ( resolve ) => setTimeout ( resolve , this . exportQueryConfig . batchDelayMs ) ) ;
469462 }
463+ }
470464
471- // After all batches are processed, copy temp files back to original files
472- const finalMetadata = fsUtil . readFile ( sanitizePath ( tempMetadataFilePath ) ) ;
473- const finalAssets = fsUtil . readFile ( sanitizePath ( tempAssetFilePath ) ) ;
465+ // After all batches are processed, copy temp files back to original files
466+ const finalMetadata = fsUtil . readFile ( sanitizePath ( tempMetadataFilePath ) ) ;
467+ const finalAssets = fsUtil . readFile ( sanitizePath ( tempAssetFilePath ) ) ;
474468
475- fsUtil . writeFile ( sanitizePath ( metadataFilePath ) , finalMetadata ) ;
476- fsUtil . writeFile ( sanitizePath ( assetFilePath ) , finalAssets ) ;
469+ fsUtil . writeFile ( sanitizePath ( metadataFilePath ) , finalMetadata ) ;
470+ fsUtil . writeFile ( sanitizePath ( assetFilePath ) , finalAssets ) ;
477471
478- log . info ( `Final data written back to original files` , this . exportQueryConfig . context ) ;
472+ log . info ( `Final data written back to original files` , this . exportQueryConfig . context ) ;
479473
480- // Clean up temp files
481- fsUtil . removeFile ( sanitizePath ( tempMetadataFilePath ) ) ;
482- fsUtil . removeFile ( sanitizePath ( tempAssetFilePath ) ) ;
474+ // Clean up temp files
475+ fsUtil . removeFile ( sanitizePath ( tempMetadataFilePath ) ) ;
476+ fsUtil . removeFile ( sanitizePath ( tempAssetFilePath ) ) ;
483477
484- log . info ( `Temporary files cleaned up` , this . exportQueryConfig . context ) ;
485- log . success ( 'Referenced assets exported successfully' , this . exportQueryConfig . context ) ;
478+ log . info ( `Temporary files cleaned up` , this . exportQueryConfig . context ) ;
479+ log . success ( 'Referenced assets exported successfully' , this . exportQueryConfig . context ) ;
486480 } catch ( error ) {
487481 handleAndLogError ( error , this . exportQueryConfig . context , 'Error exporting stack referenced assets' ) ;
488482 throw error ;
0 commit comments