@@ -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
@@ -436,11 +436,11 @@ const saveAsset = async (
436436 const publishDetails : { environment : any ; version : number ; locale : any } [ ] =
437437 [ ] ;
438438 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 ) ;
439+ const environmentsId = await readFile ( path . join ( DATA , destination_stack_id , ENVIRONMENTS_DIR_NAME ) , ENVIRONMENTS_FILE_NAME ) ?? { } ;
440440 const localeId = await readFile ( path . join ( DATA , destination_stack_id , LOCALE_DIR_NAME ) , LOCALE_CF_LANGUAGE ) ;
441441
442442 if ( assets . fields . file && assets . fields . title ) {
443- Object . values ( environmentsId ) . forEach ( ( env : any ) => {
443+ Object ?. values ?. ( environmentsId ) ?. length && Object . values ( environmentsId ) . forEach ( ( env : any ) => {
444444 if ( env ?. name === assets ?. sys ?. environment ?. sys ?. id ) {
445445 Object . values ( localeId ) . forEach ( ( locale : any ) => {
446446 publishDetails . push ( {
@@ -696,6 +696,18 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
696696 }
697697} ;
698698
699+ const mapLocales = ( { masterLocale, locale, locales } : any ) => {
700+ if ( locales ?. masterLocale ?. [ masterLocale ?? '' ] === locale ) {
701+ return Object ?. keys ( locales ?. masterLocale ) ?. [ 0 ]
702+ }
703+ for ( const [ key , value ] of Object ?. entries ?.( locales ) ?? { } ) {
704+ if ( typeof value !== 'object' && value === locale ) {
705+ return key ;
706+ }
707+ }
708+ return locale . toLowerCase ( ) ;
709+ }
710+
699711/**
700712 * Creates and processes entries from a given package file and saves them to the destination stack directory.
701713 *
@@ -722,7 +734,8 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
722734 *
723735 * @throws Will log errors encountered during file reading, processing, or writing of entries.
724736 */
725- const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any ) : Promise < void > => {
737+ const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any , master_locale : string ) : Promise < void > => {
738+ console . info ( "🚀 ~ createEntry ~ master_locale:" , master_locale )
726739 const srcFunc = 'createEntry' ;
727740 try {
728741 const entriesSave = path . join ( DATA , destination_stack_id , ENTRIES_DIR_NAME ) ;
@@ -735,7 +748,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
735748 if ( entries && entries . length > 0 ) {
736749 const assetId = await readFile ( assetsSave , ASSETS_SCHEMA_FILE ) ?? [ ] ;
737750 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 ) ;
751+ const environmentsId = await readFile ( environmentSave , ENVIRONMENTS_FILE_NAME ) ?? { } ;
739752 const displayField : { [ key : string ] : any } = { }
740753 content . map ( ( item : any ) => {
741754 displayField [ item . name . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] + / g, "_" ) ] =
@@ -781,13 +794,15 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
781794 } ) ;
782795 const pathName = getDisplayName ( name , displayField ) ;
783796 locales . forEach ( ( locale ) => {
784- const publishDetails = Object ?. values ?.( environmentsId )
797+ const localeCode = mapLocales ( { masterLocale : master_locale , locale, locales : LOCALE_MAPPER } ) ;
798+ console . info ( "🚀 ~ locales.forEach ~ localeCode:" , localeCode ) ;
799+ const publishDetails = Object ?. values ?.( environmentsId ) ?. length ? Object ?. values ?.( environmentsId )
785800 . filter ( ( env : any ) => env ?. name === environment_id )
786801 ?. map ( ( env : any ) => ( {
787802 environment : env ?. uid ,
788803 version : 1 ,
789- locale : locale . toLowerCase ( ) ,
790- } ) ) ;
804+ locale : locale ? .toLowerCase ( ) ,
805+ } ) ) : [ ] ;
791806
792807 const title = entryData [ name ] [ locale ] [ id ] [ pathName ] || "" ;
793808 const urlTitle = title
@@ -814,21 +829,18 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
814829 ) ;
815830 } ) ;
816831 } ) ;
817-
818832 return entryData ;
819833 } ,
820834 { }
821835 ) ;
822- const writePromises = [ ] ;
823-
824- for ( const [ newKey , values ] of Object . entries ( result ) ) {
836+ for await ( const [ newKey , values ] of Object . entries ( result ) ) {
825837 const currentCT = contentTypes ?. find ( ( ct : any ) => ct ?. otherCmsUid === newKey ) ;
826838 const ctName = currentCT ?. contentstackUid in mapperKeys ?
827839 mapperKeys ?. [ currentCT ?. contentstackUid ] : ( currentCT ?. contentstackUid ?? newKey . replace ( / ( [ A - Z ] ) / g, "_$1" ) . toLowerCase ( ) ) ;
828- for ( const [ localeKey , localeValues ] of Object . entries (
840+ for await ( const [ localeKey , localeValues ] of Object . entries (
829841 values as { [ key : string ] : any }
830842 ) ) {
831- const chunks = await makeChunks ( localeValues ) ;
843+ const chunks = makeChunks ( localeValues ) ;
832844 for ( const [ entryKey , entryValue ] of Object . entries ( localeValues ) ) {
833845 const message = getLogMessage (
834846 srcFunc ,
@@ -843,14 +855,13 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
843855 entriesSave ,
844856 ctName , localeKey . toLowerCase ( )
845857 ) ;
846- for ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
858+ for await ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
847859 refs [ chunkIndex ++ ] = `${ chunkId } -entries.json` ;
848- writePromises . push ( writeFile ( filePath , `${ chunkId } -entries.json` , chunkData ) )
860+ await writeFile ( filePath , `${ chunkId } -entries.json` , chunkData )
849861 }
850- writePromises . push ( writeFile ( filePath , ENTRIES_MASTER_FILE , refs ) ) ;
862+ await writeFile ( filePath , ENTRIES_MASTER_FILE , refs ) ;
851863 }
852864 }
853- await Promise . all ( writePromises ) ;
854865 } else {
855866 const message = getLogMessage (
856867 srcFunc ,
0 commit comments