@@ -9,8 +9,7 @@ import pLimit from 'p-limit';
99import { JSDOM } from "jsdom" ;
1010import { jsonToHtml , jsonToMarkdown , htmlToJson } from '@contentstack/json-rte-serializer' ;
1111
12-
13- import { CHUNK_SIZE , MIGRATION_DATA_CONFIG , LOCALE_MAPPER } from "../constants/index.js" ;
12+ import { CHUNK_SIZE , LOCALE_MAPPER , MIGRATION_DATA_CONFIG } from "../constants/index.js" ;
1413import { Locale } from "../models/types.js" ;
1514import jsonRTE from "./contentful/jsonRTE.js" ;
1615import { getAllLocales , getLogMessage } from "../utils/index.js" ;
@@ -86,6 +85,18 @@ function makeChunks(assetData: any) {
8685 return chunks ;
8786}
8887
88+ const mapLocales = ( { masterLocale, locale, locales } : any ) => {
89+ if ( locales ?. masterLocale ?. [ masterLocale ?? '' ] === locale ) {
90+ return Object ?. keys ( locales ?. masterLocale ) ?. [ 0 ]
91+ }
92+ for ( const [ key , value ] of Object ?. entries ?.( locales ) ?? { } ) {
93+ if ( typeof value !== 'object' && value === locale ) {
94+ return key ;
95+ }
96+ }
97+ return locale . toLowerCase ( ) ;
98+ }
99+
89100const transformCloudinaryObject = ( input : any ) => {
90101 const result : any = [ ] ;
91102 if ( ! Array . isArray ( input ) ) {
@@ -122,7 +133,7 @@ const transformCloudinaryObject = (input: any) => {
122133 id : uuidv4 ( ) ,
123134 folder : "" ,
124135 cs_metadata : {
125- config_label : "config "
136+ config_label : "default_multi_config_key "
126137 }
127138 } ) ;
128139 }
@@ -702,17 +713,6 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
702713 }
703714} ;
704715
705- const mapLocales = ( { masterLocale, locale, locales } : any ) => {
706- if ( locales ?. masterLocale ?. [ masterLocale ?? '' ] === locale ) {
707- return Object ?. keys ( locales ?. masterLocale ) ?. [ 0 ]
708- }
709- for ( const [ key , value ] of Object ?. entries ?.( locales ) ?? { } ) {
710- if ( typeof value !== 'object' && value === locale ) {
711- return key ;
712- }
713- }
714- return locale . toLowerCase ( ) ;
715- }
716716
717717/**
718718 * Creates and processes entries from a given package file and saves them to the destination stack directory.
@@ -740,7 +740,7 @@ const mapLocales = ({ masterLocale, locale, locales }: any) => {
740740 *
741741 * @throws Will log errors encountered during file reading, processing, or writing of entries.
742742 */
743- const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any , master_locale : string ) : Promise < void > => {
743+ const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any , master_locale : string , project : any ) : Promise < void > => {
744744 const srcFunc = 'createEntry' ;
745745 try {
746746 const entriesSave = path . join ( DATA , destination_stack_id , ENTRIES_DIR_NAME ) ;
@@ -749,7 +749,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
749749 const data = await fs . promises . readFile ( packagePath , "utf8" ) ;
750750 const entries = JSON . parse ( data ) ?. entries ;
751751 const content = JSON . parse ( data ) ?. contentTypes ;
752-
752+ const LocaleMapper = { masterLocale : project ?. master_locale ?? LOCALE_MAPPER ?. masterLocale , ... project ?. locales ?? { } } ;
753753 if ( entries && entries . length > 0 ) {
754754 const assetId = await readFile ( assetsSave , ASSETS_SCHEMA_FILE ) ?? [ ] ;
755755 const entryId = await readFile ( path . join ( DATA , destination_stack_id , REFERENCES_DIR_NAME ) , REFERENCES_FILE_NAME ) ;
@@ -799,13 +799,13 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
799799 } ) ;
800800 const pathName = getDisplayName ( name , displayField ) ;
801801 locales . forEach ( ( locale ) => {
802- const localeCode = mapLocales ( { masterLocale : master_locale , locale, locales : LOCALE_MAPPER } ) ;
802+ const localeCode = mapLocales ( { masterLocale : master_locale , locale, locales : LocaleMapper } ) ;
803803 const publishDetails = Object ?. values ?.( environmentsId ) ?. length ? Object ?. values ?.( environmentsId )
804804 . filter ( ( env : any ) => env ?. name === environment_id )
805805 ?. map ( ( env : any ) => ( {
806806 environment : env ?. uid ,
807807 version : 1 ,
808- locale : locale ?. toLowerCase ?. ( ) ,
808+ locale : localeCode ,
809809 } ) ) : [ ] ;
810810 const title = fields ?. [ pathName ] ?. [ locale ] || "" ;
811811 const urlTitle = title
@@ -816,7 +816,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
816816 title : title ?. trim ?.( ) === "" ? ( urlTitle || id ) : title ,
817817 uid : id ,
818818 url : `/${ name ?. toLowerCase ?.( ) } /${ urlTitle } ` ,
819- locale : locale ?. toLowerCase ?. ( ) ,
819+ locale : localeCode ,
820820 publish_details : publishDetails ,
821821 } ;
822822 // Format object keys to snake_case
@@ -843,11 +843,12 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
843843 for await ( const [ localeKey , localeValues ] of Object . entries (
844844 values as { [ key : string ] : any }
845845 ) ) {
846+ const localeCode = mapLocales ( { masterLocale : master_locale , locale : localeKey , locales : LocaleMapper } ) ;
846847 const chunks = makeChunks ( localeValues ) ;
847848 for ( const [ entryKey , entryValue ] of Object . entries ( localeValues ) ) {
848849 const message = getLogMessage (
849850 srcFunc ,
850- `Entry title "${ ( entryValue as { title : string } ) ?. title } "(${ ctName } ) in the ${ localeKey } locale has been successfully transformed.` ,
851+ `Entry title "${ ( entryValue as { title : string } ) ?. title } "(${ ctName } ) in the ${ localeCode } locale has been successfully transformed.` ,
851852 { }
852853 ) ;
853854 await customLogger ( projectId , destination_stack_id , "info" , message ) ;
@@ -856,7 +857,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
856857 let chunkIndex = 1 ;
857858 const filePath = path . join (
858859 entriesSave ,
859- ctName , localeKey . toLowerCase ( )
860+ ctName , localeCode
860861 ) ;
861862 for await ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
862863 refs [ chunkIndex ++ ] = `${ chunkId } -entries.json` ;
@@ -874,7 +875,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
874875 await customLogger ( projectId , destination_stack_id , 'info' , message ) ;
875876 }
876877 } catch ( err ) {
877- console . info ( "🚀 ~ createEntry ~ err:" , err )
878+ console . error ( "🚀 ~ createEntry ~ err:" , err )
878879 const message = getLogMessage (
879880 srcFunc ,
880881 `Error encountered while creating entries.` ,
@@ -885,6 +886,10 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
885886 }
886887} ;
887888
889+ function getKeyByValue ( obj : Record < string , string > , targetValue : string ) : string | undefined {
890+ return Object . entries ( obj ) . find ( ( [ _ , value ] ) => value === targetValue ) ?. [ 0 ] ;
891+ }
892+
888893/**
889894 * Processes and creates locale configurations from a given package file and saves them to the destination stack directory.
890895 *
@@ -909,7 +914,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
909914 *
910915 * @throws Will log errors encountered during file reading, processing, or writing of locale configurations.
911916 */
912- const createLocale = async ( packagePath : string , destination_stack_id : string , projectId : string ) => {
917+ const createLocale = async ( packagePath : string , destination_stack_id : string , projectId : string , project : any ) => {
913918 const srcFunc = 'createLocale' ;
914919 const localeSave = path . join ( DATA , destination_stack_id , LOCALE_DIR_NAME ) ;
915920 const globalFieldSave = path . join ( DATA , destination_stack_id , GLOBAL_FIELDS_DIR_NAME ) ;
@@ -933,30 +938,30 @@ const createLocale = async (packagePath: string, destination_stack_id: string, p
933938 )
934939 await customLogger ( projectId , destination_stack_id , 'error' , message ) ;
935940 }
936-
937- await Promise . all ( locales . map ( async ( localeData : any ) => {
938- const title = localeData . sys . id ;
941+ const fallbackMapLocales : any = { ...project ?. master_locale ?? { } , ...project ?. locales ?? { } }
942+ await Promise ?. all ( locales ?. map ?.( async ( localeData : any ) => {
943+ const currentMapLocale = getKeyByValue ?.( fallbackMapLocales , localeData ?. code ) ?? `${ localeData . code . toLowerCase ( ) } ` ;
944+ const title = localeData ?. sys ?. id ;
939945 const newLocale : Locale = {
940- code : ` ${ localeData . code . toLowerCase ( ) } ` ,
941- name : localeCodes ?. [ localeData . code . toLowerCase ( ) ] || "English - United States" ,
942- fallback_locale : "" ,
946+ code : currentMapLocale ,
947+ name : localeCodes ?. [ currentMapLocale ] || "English - United States" ,
948+ fallback_locale : getKeyByValue ( fallbackMapLocales , localeData ?. fallbackCode ) ?? '' ,
943949 uid : `${ title } ` ,
944950 } ;
945951
946952 if ( localeData . default === true ) {
947953 msLocale [ title ] = newLocale ;
948954 const message = getLogMessage (
949955 srcFunc ,
950- `Master Locale ${ newLocale . code } has been successfully transformed.` ,
956+ `Master Locale ${ newLocale ? .code } has been successfully transformed.` ,
951957 { }
952958 )
953959 await customLogger ( projectId , destination_stack_id , 'info' , message ) ;
954960 } else {
955- newLocale . name = `${ localeData . name } ` ;
956961 allLocales [ title ] = newLocale ;
957962 const message = getLogMessage (
958963 srcFunc ,
959- `Locale ${ newLocale . code } has been successfully transformed.` ,
964+ `Locale ${ newLocale ? .code } has been successfully transformed.` ,
960965 { }
961966 )
962967 await customLogger ( projectId , destination_stack_id , 'info' , message ) ;
0 commit comments