@@ -10,7 +10,7 @@ import { JSDOM } from "jsdom";
1010import { jsonToHtml , jsonToMarkdown , htmlToJson } from '@contentstack/json-rte-serializer' ;
1111
1212
13- import { CHUNK_SIZE , MIGRATION_DATA_CONFIG } from "../constants/index.js" ;
13+ import { CHUNK_SIZE , MIGRATION_DATA_CONFIG , LOCALE_MAPPER } from "../constants/index.js" ;
1414import { Locale } from "../models/types.js" ;
1515import jsonRTE from "./contentful/jsonRTE.js" ;
1616import { getAllLocales , getLogMessage } from "../utils/index.js" ;
@@ -234,7 +234,7 @@ const processField = (
234234 return assetsData ;
235235 } else {
236236 const id = lang_value ?. sys ?. id ;
237- if ( id in assetId ) {
237+ if ( assetId && id in assetId ) {
238238 return assetId ?. [ id ]
239239 }
240240 return null ;
@@ -253,7 +253,7 @@ const processField = (
253253 return refs ;
254254 }
255255 const id = lang_value ?. sys ?. id ;
256- if ( id in entryId ) return [ [ entryId ?. [ id ] ] ] ;
256+ if ( entryId && id in entryId ) return [ [ entryId ?. [ id ] ] ] ;
257257 return null ;
258258 }
259259
@@ -333,6 +333,7 @@ const processField = (
333333 }
334334} ;
335335
336+
336337// Helper function to clean up brackets in non-numeric lang_value
337338const cleanBrackets = ( lang_value : any ) => {
338339 const myJSON = JSON . stringify ( lang_value ) ;
@@ -436,11 +437,11 @@ const saveAsset = async (
436437 const publishDetails : { environment : any ; version : number ; locale : any } [ ] =
437438 [ ] ;
438439 const assetsSave = path . join ( DATA , destination_stack_id , ASSETS_DIR_NAME ) ;
439- const environmentsId = await readFile ( path . join ( DATA , destination_stack_id , ENVIRONMENTS_DIR_NAME ) , ENVIRONMENTS_FILE_NAME ) ;
440+ const environmentsId = await readFile ( path . join ( DATA , destination_stack_id , ENVIRONMENTS_DIR_NAME ) , ENVIRONMENTS_FILE_NAME ) ?? { } ;
440441 const localeId = await readFile ( path . join ( DATA , destination_stack_id , LOCALE_DIR_NAME ) , LOCALE_CF_LANGUAGE ) ;
441442
442443 if ( assets . fields . file && assets . fields . title ) {
443- Object . values ( environmentsId ) . forEach ( ( env : any ) => {
444+ Object ?. values ?. ( environmentsId ) ?. length && Object . values ( environmentsId ) . forEach ( ( env : any ) => {
444445 if ( env ?. name === assets ?. sys ?. environment ?. sys ?. id ) {
445446 Object . values ( localeId ) . forEach ( ( locale : any ) => {
446447 publishDetails . push ( {
@@ -696,6 +697,18 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
696697 }
697698} ;
698699
700+ const mapLocales = ( { masterLocale, locale, locales } : any ) => {
701+ if ( locales ?. masterLocale ?. [ masterLocale ?? '' ] === locale ) {
702+ return Object ?. keys ( locales ?. masterLocale ) ?. [ 0 ]
703+ }
704+ for ( const [ key , value ] of Object ?. entries ?.( locales ) ?? { } ) {
705+ if ( typeof value !== 'object' && value === locale ) {
706+ return key ;
707+ }
708+ }
709+ return locale . toLowerCase ( ) ;
710+ }
711+
699712/**
700713 * Creates and processes entries from a given package file and saves them to the destination stack directory.
701714 *
@@ -722,7 +735,8 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
722735 *
723736 * @throws Will log errors encountered during file reading, processing, or writing of entries.
724737 */
725- const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any ) : Promise < void > => {
738+ const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any , master_locale : string ) : Promise < void > => {
739+ console . info ( "🚀 ~ createEntry ~ master_locale:" , master_locale )
726740 const srcFunc = 'createEntry' ;
727741 try {
728742 const entriesSave = path . join ( DATA , destination_stack_id , ENTRIES_DIR_NAME ) ;
@@ -735,7 +749,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
735749 if ( entries && entries . length > 0 ) {
736750 const assetId = await readFile ( assetsSave , ASSETS_SCHEMA_FILE ) ?? [ ] ;
737751 const entryId = await readFile ( path . join ( DATA , destination_stack_id , REFERENCES_DIR_NAME ) , REFERENCES_FILE_NAME ) ;
738- const environmentsId = await readFile ( environmentSave , ENVIRONMENTS_FILE_NAME ) ;
752+ const environmentsId = await readFile ( environmentSave , ENVIRONMENTS_FILE_NAME ) ?? { } ;
739753 const displayField : { [ key : string ] : any } = { }
740754 content . map ( ( item : any ) => {
741755 displayField [ item . name . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] + / g, "_" ) ] =
@@ -781,13 +795,15 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
781795 } ) ;
782796 const pathName = getDisplayName ( name , displayField ) ;
783797 locales . forEach ( ( locale ) => {
784- const publishDetails = Object ?. values ?.( environmentsId )
798+ const localeCode = mapLocales ( { masterLocale : master_locale , locale, locales : LOCALE_MAPPER } ) ;
799+ console . info ( "🚀 ~ locales.forEach ~ localeCode:" , localeCode ) ;
800+ const publishDetails = Object ?. values ?.( environmentsId ) ?. length ? Object ?. values ?.( environmentsId )
785801 . filter ( ( env : any ) => env ?. name === environment_id )
786802 ?. map ( ( env : any ) => ( {
787803 environment : env ?. uid ,
788804 version : 1 ,
789- locale : locale . toLowerCase ( ) ,
790- } ) ) ;
805+ locale : locale ? .toLowerCase ( ) ,
806+ } ) ) : [ ] ;
791807
792808 const title = entryData [ name ] [ locale ] [ id ] [ pathName ] || "" ;
793809 const urlTitle = title
@@ -814,21 +830,18 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
814830 ) ;
815831 } ) ;
816832 } ) ;
817-
818833 return entryData ;
819834 } ,
820835 { }
821836 ) ;
822- const writePromises = [ ] ;
823-
824- for ( const [ newKey , values ] of Object . entries ( result ) ) {
837+ for await ( const [ newKey , values ] of Object . entries ( result ) ) {
825838 const currentCT = contentTypes ?. find ( ( ct : any ) => ct ?. otherCmsUid === newKey ) ;
826839 const ctName = currentCT ?. contentstackUid in mapperKeys ?
827840 mapperKeys ?. [ currentCT ?. contentstackUid ] : ( currentCT ?. contentstackUid ?? newKey . replace ( / ( [ A - Z ] ) / g, "_$1" ) . toLowerCase ( ) ) ;
828- for ( const [ localeKey , localeValues ] of Object . entries (
841+ for await ( const [ localeKey , localeValues ] of Object . entries (
829842 values as { [ key : string ] : any }
830843 ) ) {
831- const chunks = await makeChunks ( localeValues ) ;
844+ const chunks = makeChunks ( localeValues ) ;
832845 for ( const [ entryKey , entryValue ] of Object . entries ( localeValues ) ) {
833846 const message = getLogMessage (
834847 srcFunc ,
@@ -843,14 +856,13 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
843856 entriesSave ,
844857 ctName , localeKey . toLowerCase ( )
845858 ) ;
846- for ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
859+ for await ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
847860 refs [ chunkIndex ++ ] = `${ chunkId } -entries.json` ;
848- writePromises . push ( writeFile ( filePath , `${ chunkId } -entries.json` , chunkData ) )
861+ await writeFile ( filePath , `${ chunkId } -entries.json` , chunkData )
849862 }
850- writePromises . push ( writeFile ( filePath , ENTRIES_MASTER_FILE , refs ) ) ;
863+ await writeFile ( filePath , ENTRIES_MASTER_FILE , refs ) ;
851864 }
852865 }
853- await Promise . all ( writePromises ) ;
854866 } else {
855867 const message = getLogMessage (
856868 srcFunc ,
0 commit comments