@@ -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
@@ -437,11 +437,11 @@ const saveAsset = async (
437437 const publishDetails : { environment : any ; version : number ; locale : any } [ ] =
438438 [ ] ;
439439 const assetsSave = path . join ( DATA , destination_stack_id , ASSETS_DIR_NAME ) ;
440- 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 ) ?? { } ;
441441 const localeId = await readFile ( path . join ( DATA , destination_stack_id , LOCALE_DIR_NAME ) , LOCALE_CF_LANGUAGE ) ;
442442
443443 if ( assets . fields . file && assets . fields . title ) {
444- Object . values ( environmentsId ) . forEach ( ( env : any ) => {
444+ Object ?. values ?. ( environmentsId ) ?. length && Object . values ( environmentsId ) . forEach ( ( env : any ) => {
445445 if ( env ?. name === assets ?. sys ?. environment ?. sys ?. id ) {
446446 Object . values ( localeId ) . forEach ( ( locale : any ) => {
447447 publishDetails . push ( {
@@ -697,6 +697,18 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
697697 }
698698} ;
699699
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+
700712/**
701713 * Creates and processes entries from a given package file and saves them to the destination stack directory.
702714 *
@@ -723,7 +735,8 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
723735 *
724736 * @throws Will log errors encountered during file reading, processing, or writing of entries.
725737 */
726- 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 )
727740 const srcFunc = 'createEntry' ;
728741 try {
729742 const entriesSave = path . join ( DATA , destination_stack_id , ENTRIES_DIR_NAME ) ;
@@ -736,7 +749,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
736749 if ( entries && entries . length > 0 ) {
737750 const assetId = await readFile ( assetsSave , ASSETS_SCHEMA_FILE ) ?? [ ] ;
738751 const entryId = await readFile ( path . join ( DATA , destination_stack_id , REFERENCES_DIR_NAME ) , REFERENCES_FILE_NAME ) ;
739- const environmentsId = await readFile ( environmentSave , ENVIRONMENTS_FILE_NAME ) ;
752+ const environmentsId = await readFile ( environmentSave , ENVIRONMENTS_FILE_NAME ) ?? { } ;
740753 const displayField : { [ key : string ] : any } = { }
741754 content . map ( ( item : any ) => {
742755 displayField [ item . name . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] + / g, "_" ) ] =
@@ -782,13 +795,15 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
782795 } ) ;
783796 const pathName = getDisplayName ( name , displayField ) ;
784797 locales . forEach ( ( locale ) => {
785- 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 )
786801 . filter ( ( env : any ) => env ?. name === environment_id )
787802 ?. map ( ( env : any ) => ( {
788803 environment : env ?. uid ,
789804 version : 1 ,
790- locale : locale . toLowerCase ( ) ,
791- } ) ) ;
805+ locale : locale ? .toLowerCase ( ) ,
806+ } ) ) : [ ] ;
792807
793808 const title = entryData [ name ] [ locale ] [ id ] [ pathName ] || "" ;
794809 const urlTitle = title
@@ -815,21 +830,18 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
815830 ) ;
816831 } ) ;
817832 } ) ;
818-
819833 return entryData ;
820834 } ,
821835 { }
822836 ) ;
823- const writePromises = [ ] ;
824-
825- for ( const [ newKey , values ] of Object . entries ( result ) ) {
837+ for await ( const [ newKey , values ] of Object . entries ( result ) ) {
826838 const currentCT = contentTypes ?. find ( ( ct : any ) => ct ?. otherCmsUid === newKey ) ;
827839 const ctName = currentCT ?. contentstackUid in mapperKeys ?
828840 mapperKeys ?. [ currentCT ?. contentstackUid ] : ( currentCT ?. contentstackUid ?? newKey . replace ( / ( [ A - Z ] ) / g, "_$1" ) . toLowerCase ( ) ) ;
829- for ( const [ localeKey , localeValues ] of Object . entries (
841+ for await ( const [ localeKey , localeValues ] of Object . entries (
830842 values as { [ key : string ] : any }
831843 ) ) {
832- const chunks = await makeChunks ( localeValues ) ;
844+ const chunks = makeChunks ( localeValues ) ;
833845 for ( const [ entryKey , entryValue ] of Object . entries ( localeValues ) ) {
834846 const message = getLogMessage (
835847 srcFunc ,
@@ -844,14 +856,13 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
844856 entriesSave ,
845857 ctName , localeKey . toLowerCase ( )
846858 ) ;
847- for ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
859+ for await ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
848860 refs [ chunkIndex ++ ] = `${ chunkId } -entries.json` ;
849- writePromises . push ( writeFile ( filePath , `${ chunkId } -entries.json` , chunkData ) )
861+ await writeFile ( filePath , `${ chunkId } -entries.json` , chunkData )
850862 }
851- writePromises . push ( writeFile ( filePath , ENTRIES_MASTER_FILE , refs ) ) ;
863+ await writeFile ( filePath , ENTRIES_MASTER_FILE , refs ) ;
852864 }
853865 }
854- await Promise . all ( writePromises ) ;
855866 } else {
856867 const message = getLogMessage (
857868 srcFunc ,
0 commit comments