11import * as path from 'path' ;
2- import {
3- ContentstackClient ,
4- FsUtility ,
5- handleAndLogError ,
6- messageHandler ,
7- log ,
8- } from '@contentstack/cli-utilities' ;
2+ import { ContentstackClient , FsUtility , handleAndLogError , messageHandler , log } from '@contentstack/cli-utilities' ;
93import { Export , ExportProjects } from '@contentstack/cli-variants' ;
104import { sanitizePath } from '@contentstack/cli-utilities' ;
115
@@ -65,15 +59,18 @@ export default class EntriesExport extends BaseClass {
6559 try {
6660 log . debug ( 'Starting entries export process...' , this . exportConfig . context ) ;
6761 const locales = fsUtil . readFile ( this . localesFilePath ) as Array < Record < string , unknown > > ;
68- log . debug ( `Loaded ${ locales . length } locales from ${ this . localesFilePath } ` , this . exportConfig . context ) ;
69-
62+ if ( ! Array . isArray ( locales ) || locales ?. length === 0 ) {
63+ log . debug ( `No locales found in ${ this . localesFilePath } ` , this . exportConfig . context ) ;
64+ } else {
65+ log . debug ( `Loaded ${ locales ?. length } locales from ${ this . localesFilePath } ` , this . exportConfig . context ) ;
66+ }
67+
7068 const contentTypes = fsUtil . readFile ( this . schemaFilePath ) as Array < Record < string , unknown > > ;
71- log . debug ( `Loaded ${ contentTypes . length } content types from ${ this . schemaFilePath } ` , this . exportConfig . context ) ;
72-
73- if ( contentTypes . length === 0 ) {
69+ if ( contentTypes ?. length === 0 ) {
7470 log . info ( messageHandler . parse ( 'CONTENT_TYPE_NO_TYPES' ) , this . exportConfig . context ) ;
7571 return ;
7672 }
73+ log . debug ( `Loaded ${ contentTypes ?. length } content types from ${ this . schemaFilePath } ` , this . exportConfig . context ) ;
7774
7875 // NOTE Check if variant is enabled in specific stack
7976 if ( this . exportConfig . personalizationEnabled ) {
@@ -95,18 +92,20 @@ export default class EntriesExport extends BaseClass {
9592 }
9693
9794 const entryRequestOptions = this . createRequestObjects ( locales , contentTypes ) ;
98- log . debug ( `Created ${ entryRequestOptions . length } entry request objects for processing` , this . exportConfig . context ) ;
99-
95+ log . debug (
96+ `Created ${ entryRequestOptions . length } entry request objects for processing` ,
97+ this . exportConfig . context ,
98+ ) ;
99+
100100 for ( let entryRequestOption of entryRequestOptions ) {
101- log . debug ( `Processing entries for content type: ${ entryRequestOption . contentType } , locale: ${ entryRequestOption . locale } ` , this . exportConfig . context ) ;
101+ log . debug (
102+ `Processing entries for content type: ${ entryRequestOption . contentType } , locale: ${ entryRequestOption . locale } ` ,
103+ this . exportConfig . context ,
104+ ) ;
102105 await this . getEntries ( entryRequestOption ) ;
103106 this . entriesFileHelper ?. completeFile ( true ) ;
104107 log . success (
105- messageHandler . parse (
106- 'ENTRIES_EXPORT_COMPLETE' ,
107- entryRequestOption . contentType ,
108- entryRequestOption . locale ,
109- ) ,
108+ messageHandler . parse ( 'ENTRIES_EXPORT_COMPLETE' , entryRequestOption . contentType , entryRequestOption . locale ) ,
110109 this . exportConfig . context ,
111110 ) ;
112111 }
@@ -120,8 +119,18 @@ export default class EntriesExport extends BaseClass {
120119 locales : Array < Record < string , unknown > > ,
121120 contentTypes : Array < Record < string , unknown > > ,
122121 ) : Array < Record < string , any > > {
123- log . debug ( `Creating request objects for ${ contentTypes . length } content types and ${ locales . length } locales` , this . exportConfig . context ) ;
124-
122+ if ( ! Array . isArray ( locales ) || locales ?. length === 0 ) {
123+ log . debug ( 'No locales found, using master locale only' , this . exportConfig . context ) ;
124+ } else {
125+ log . debug ( `Found ${ locales . length } locales for export` , this . exportConfig . context ) ;
126+ }
127+ if ( ! Array . isArray ( contentTypes ) || contentTypes . length === 0 ) {
128+ log . debug ( 'No content types found, skipping entries export' , this . exportConfig . context ) ;
129+ return [ ] ;
130+ } else {
131+ log . debug ( `Found ${ contentTypes . length } content types for export` , this . exportConfig . context ) ;
132+ }
133+
125134 let requestObjects : Array < Record < string , any > > = [ ] ;
126135 contentTypes . forEach ( ( contentType ) => {
127136 if ( Object . keys ( locales ) . length !== 0 ) {
@@ -165,17 +174,21 @@ export default class EntriesExport extends BaseClass {
165174 . entry ( )
166175 . query ( requestObject )
167176 . find ( ) ;
168-
169- log . debug ( `Fetched ${ entriesSearchResponse . items ?. length || 0 } entries out of total ${ entriesSearchResponse . count } ` , this . exportConfig . context ) ;
177+
178+ log . debug (
179+ `Fetched ${ entriesSearchResponse . items ?. length || 0 } entries out of total ${ entriesSearchResponse . count } ` ,
180+ this . exportConfig . context ,
181+ ) ;
170182 } catch ( error ) {
171183 handleAndLogError ( error , {
172184 ...this . exportConfig . context ,
173185 contentType : options . contentType ,
174186 locale : options . locale ,
175187 } ) ;
188+ throw error ;
176189 }
177190
178- if ( Array . isArray ( entriesSearchResponse . items ) && entriesSearchResponse . items . length > 0 ) {
191+ if ( Array . isArray ( entriesSearchResponse ? .items ) && entriesSearchResponse ? .items ? .length > 0 ) {
179192 if ( options . skip === 0 ) {
180193 const entryBasePath = path . join (
181194 sanitizePath ( this . entriesDirPath ) ,
@@ -194,10 +207,10 @@ export default class EntriesExport extends BaseClass {
194207 } ) ;
195208 log . debug ( 'Initialized FsUtility for writing entries' , this . exportConfig . context ) ;
196209 }
197-
210+
198211 log . debug ( `Writing ${ entriesSearchResponse . items . length } entries to file` , this . exportConfig . context ) ;
199212 this . entriesFileHelper . writeIntoFile ( entriesSearchResponse . items , { mapKeyVal : true } ) ;
200-
213+
201214 if ( this . entriesConfig . exportVersions ) {
202215 log . debug ( 'Exporting entry versions is enabled' , this . exportConfig . context ) ;
203216 let versionedEntryPath = path . join (
@@ -227,7 +240,10 @@ export default class EntriesExport extends BaseClass {
227240
228241 options . skip += this . entriesConfig . limit || 100 ;
229242 if ( options . skip >= entriesSearchResponse . count ) {
230- log . debug ( `Completed fetching all entries for content type: ${ options . contentType } , locale: ${ options . locale } ` , this . exportConfig . context ) ;
243+ log . debug (
244+ `Completed fetching all entries for content type: ${ options . contentType } , locale: ${ options . locale } ` ,
245+ this . exportConfig . context ,
246+ ) ;
231247 return Promise . resolve ( true ) ;
232248 }
233249 log . debug ( `Continuing to fetch entries with skip: ${ options . skip } ` , this . exportConfig . context ) ;
@@ -240,7 +256,7 @@ export default class EntriesExport extends BaseClass {
240256 options : { locale : string ; contentType : string ; versionedEntryPath : string } ,
241257 ) : Promise < void > {
242258 log . debug ( `Fetching versions for ${ entries . length } entries` , this . exportConfig . context ) ;
243-
259+
244260 const onSuccess = ( { response, apiData : entry } : any ) => {
245261 const versionFilePath = path . join ( sanitizePath ( options . versionedEntryPath ) , sanitizePath ( `${ entry . uid } .json` ) ) ;
246262 log . debug ( `Writing versioned entry to: ${ versionFilePath } ` , this . exportConfig . context ) ;
@@ -262,7 +278,10 @@ export default class EntriesExport extends BaseClass {
262278 ) ;
263279 } ;
264280
265- log . debug ( `Starting concurrent calls for versioned entries with batch limit: ${ this . entriesConfig . batchLimit } ` , this . exportConfig . context ) ;
281+ log . debug (
282+ `Starting concurrent calls for versioned entries with batch limit: ${ this . entriesConfig . batchLimit } ` ,
283+ this . exportConfig . context ,
284+ ) ;
266285 return await this . makeConcurrentCall (
267286 {
268287 apiBatches : [ entries ] ,
@@ -289,7 +308,7 @@ export default class EntriesExport extends BaseClass {
289308 isLastRequest : boolean ;
290309 } ) {
291310 log . debug ( `Processing versioned entry: ${ entry . uid } ` , this . exportConfig . context ) ;
292-
311+
293312 return new Promise ( async ( resolve , reject ) => {
294313 return await this . getEntryByVersion ( apiParams . queryParam , entry )
295314 . then ( ( response ) => {
@@ -323,21 +342,27 @@ export default class EntriesExport extends BaseClass {
323342 } ,
324343 version : entry . _version ,
325344 } ;
326-
345+
327346 log . debug ( `Fetching entry version ${ entry . _version } for uid: ${ entry . uid } ` , this . exportConfig . context ) ;
328-
347+
329348 const entryResponse = await this . stackAPIClient
330349 . contentType ( options . contentType )
331350 . entry ( entry . uid )
332351 . fetch ( queryRequestObject ) ;
333352 entries . push ( entryResponse ) ;
334-
353+
335354 if ( -- entry . _version > 0 ) {
336- log . debug ( `Continuing to fetch previous version ${ entry . _version } for entry: ${ entry . uid } ` , this . exportConfig . context ) ;
355+ log . debug (
356+ `Continuing to fetch previous version ${ entry . _version } for entry: ${ entry . uid } ` ,
357+ this . exportConfig . context ,
358+ ) ;
337359 return await this . getEntryByVersion ( options , entry , entries ) ;
338360 }
339-
340- log . debug ( `Completed fetching all versions for entry: ${ entry . uid } , total versions: ${ entries . length } ` , this . exportConfig . context ) ;
361+
362+ log . debug (
363+ `Completed fetching all versions for entry: ${ entry . uid } , total versions: ${ entries . length } ` ,
364+ this . exportConfig . context ,
365+ ) ;
341366 return entries ;
342367 }
343368}
0 commit comments