@@ -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
@@ -331,11 +331,9 @@ const processField = (
331331 break ;
332332 }
333333 }
334-
335-
336-
337334} ;
338335
336+
339337// Helper function to clean up brackets in non-numeric lang_value
340338const cleanBrackets = ( lang_value : any ) => {
341339 const myJSON = JSON . stringify ( lang_value ) ;
@@ -439,11 +437,11 @@ const saveAsset = async (
439437 const publishDetails : { environment : any ; version : number ; locale : any } [ ] =
440438 [ ] ;
441439 const assetsSave = path . join ( DATA , destination_stack_id , ASSETS_DIR_NAME ) ;
442- 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 ) ?? { } ;
443441 const localeId = await readFile ( path . join ( DATA , destination_stack_id , LOCALE_DIR_NAME ) , LOCALE_CF_LANGUAGE ) ;
444442
445443 if ( assets . fields . file && assets . fields . title ) {
446- Object . values ( environmentsId ) . forEach ( ( env : any ) => {
444+ Object ?. values ?. ( environmentsId ) ?. length && Object . values ( environmentsId ) . forEach ( ( env : any ) => {
447445 if ( env ?. name === assets ?. sys ?. environment ?. sys ?. id ) {
448446 Object . values ( localeId ) . forEach ( ( locale : any ) => {
449447 publishDetails . push ( {
@@ -699,6 +697,18 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
699697 }
700698} ;
701699
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+
702712/**
703713 * Creates and processes entries from a given package file and saves them to the destination stack directory.
704714 *
@@ -725,7 +735,8 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
725735 *
726736 * @throws Will log errors encountered during file reading, processing, or writing of entries.
727737 */
728- 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 )
729740 const srcFunc = 'createEntry' ;
730741 try {
731742 const entriesSave = path . join ( DATA , destination_stack_id , ENTRIES_DIR_NAME ) ;
@@ -738,7 +749,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
738749 if ( entries && entries . length > 0 ) {
739750 const assetId = await readFile ( assetsSave , ASSETS_SCHEMA_FILE ) ?? [ ] ;
740751 const entryId = await readFile ( path . join ( DATA , destination_stack_id , REFERENCES_DIR_NAME ) , REFERENCES_FILE_NAME ) ;
741- const environmentsId = await readFile ( environmentSave , ENVIRONMENTS_FILE_NAME ) ;
752+ const environmentsId = await readFile ( environmentSave , ENVIRONMENTS_FILE_NAME ) ?? { } ;
742753 const displayField : { [ key : string ] : any } = { }
743754 content . map ( ( item : any ) => {
744755 displayField [ item . name . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] + / g, "_" ) ] =
@@ -784,13 +795,15 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
784795 } ) ;
785796 const pathName = getDisplayName ( name , displayField ) ;
786797 locales . forEach ( ( locale ) => {
787- 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 )
788801 . filter ( ( env : any ) => env ?. name === environment_id )
789802 ?. map ( ( env : any ) => ( {
790803 environment : env ?. uid ,
791804 version : 1 ,
792- locale : locale . toLowerCase ( ) ,
793- } ) ) ;
805+ locale : locale ? .toLowerCase ( ) ,
806+ } ) ) : [ ] ;
794807
795808 const title = entryData [ name ] [ locale ] [ id ] [ pathName ] || "" ;
796809 const urlTitle = title
@@ -817,21 +830,18 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
817830 ) ;
818831 } ) ;
819832 } ) ;
820-
821833 return entryData ;
822834 } ,
823835 { }
824836 ) ;
825- const writePromises = [ ] ;
826-
827- for ( const [ newKey , values ] of Object . entries ( result ) ) {
837+ for await ( const [ newKey , values ] of Object . entries ( result ) ) {
828838 const currentCT = contentTypes ?. find ( ( ct : any ) => ct ?. otherCmsUid === newKey ) ;
829839 const ctName = currentCT ?. contentstackUid in mapperKeys ?
830840 mapperKeys ?. [ currentCT ?. contentstackUid ] : ( currentCT ?. contentstackUid ?? newKey . replace ( / ( [ A - Z ] ) / g, "_$1" ) . toLowerCase ( ) ) ;
831- for ( const [ localeKey , localeValues ] of Object . entries (
841+ for await ( const [ localeKey , localeValues ] of Object . entries (
832842 values as { [ key : string ] : any }
833843 ) ) {
834- const chunks = await makeChunks ( localeValues ) ;
844+ const chunks = makeChunks ( localeValues ) ;
835845 for ( const [ entryKey , entryValue ] of Object . entries ( localeValues ) ) {
836846 const message = getLogMessage (
837847 srcFunc ,
@@ -846,14 +856,13 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
846856 entriesSave ,
847857 ctName , localeKey . toLowerCase ( )
848858 ) ;
849- for ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
859+ for await ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
850860 refs [ chunkIndex ++ ] = `${ chunkId } -entries.json` ;
851- writePromises . push ( writeFile ( filePath , `${ chunkId } -entries.json` , chunkData ) )
861+ await writeFile ( filePath , `${ chunkId } -entries.json` , chunkData )
852862 }
853- writePromises . push ( writeFile ( filePath , ENTRIES_MASTER_FILE , refs ) ) ;
863+ await writeFile ( filePath , ENTRIES_MASTER_FILE , refs ) ;
854864 }
855865 }
856- await Promise . all ( writePromises ) ;
857866 } else {
858867 const message = getLogMessage (
859868 srcFunc ,
0 commit comments