@@ -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 , LOCALE_MAPPER } from "../constants/index.js" ;
13+ import { CHUNK_SIZE , LOCALE_MAPPER , MIGRATION_DATA_CONFIG } from "../constants/index.js" ;
1414import { Locale } from "../models/types.js" ;
1515import jsonRTE from "./contentful/jsonRTE.js" ;
1616import { getAllLocales , getLogMessage } from "../utils/index.js" ;
@@ -86,6 +86,18 @@ function makeChunks(assetData: any) {
8686 return chunks ;
8787}
8888
89+ const mapLocales = ( { masterLocale, locale, locales } : any ) => {
90+ if ( locales ?. masterLocale ?. [ masterLocale ?? '' ] === locale ) {
91+ return Object ?. keys ( locales ?. masterLocale ) ?. [ 0 ]
92+ }
93+ for ( const [ key , value ] of Object ?. entries ?.( locales ) ?? { } ) {
94+ if ( typeof value !== 'object' && value === locale ) {
95+ return key ;
96+ }
97+ }
98+ return locale . toLowerCase ( ) ;
99+ }
100+
89101const transformCloudinaryObject = ( input : any ) => {
90102 const result : any = [ ] ;
91103 if ( ! Array . isArray ( input ) ) {
@@ -122,7 +134,7 @@ const transformCloudinaryObject = (input: any) => {
122134 id : uuidv4 ( ) ,
123135 folder : "" ,
124136 cs_metadata : {
125- config_label : "config "
137+ config_label : "default_multi_config_key "
126138 }
127139 } ) ;
128140 }
@@ -702,17 +714,6 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
702714 }
703715} ;
704716
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- }
716717
717718/**
718719 * Creates and processes entries from a given package file and saves them to the destination stack directory.
@@ -740,7 +741,7 @@ const mapLocales = ({ masterLocale, locale, locales }: any) => {
740741 *
741742 * @throws Will log errors encountered during file reading, processing, or writing of entries.
742743 */
743- const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any , master_locale : string ) : Promise < void > => {
744+ const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any , master_locale : string , project : any ) : Promise < void > => {
744745 const srcFunc = 'createEntry' ;
745746 try {
746747 const entriesSave = path . join ( DATA , destination_stack_id , ENTRIES_DIR_NAME ) ;
@@ -749,7 +750,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
749750 const data = await fs . promises . readFile ( packagePath , "utf8" ) ;
750751 const entries = JSON . parse ( data ) ?. entries ;
751752 const content = JSON . parse ( data ) ?. contentTypes ;
752-
753+ const LocaleMapper = { masterLocale : project ?. master_locale ?? LOCALE_MAPPER ?. masterLocale , ... project ?. locales ?? { } } ;
753754 if ( entries && entries . length > 0 ) {
754755 const assetId = await readFile ( assetsSave , ASSETS_SCHEMA_FILE ) ?? [ ] ;
755756 const entryId = await readFile ( path . join ( DATA , destination_stack_id , REFERENCES_DIR_NAME ) , REFERENCES_FILE_NAME ) ;
@@ -799,13 +800,13 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
799800 } ) ;
800801 const pathName = getDisplayName ( name , displayField ) ;
801802 locales . forEach ( ( locale ) => {
802- const localeCode = mapLocales ( { masterLocale : master_locale , locale, locales : LOCALE_MAPPER } ) ;
803+ const localeCode = mapLocales ( { masterLocale : master_locale , locale, locales : LocaleMapper } ) ;
803804 const publishDetails = Object ?. values ?.( environmentsId ) ?. length ? Object ?. values ?.( environmentsId )
804805 . filter ( ( env : any ) => env ?. name === environment_id )
805806 ?. map ( ( env : any ) => ( {
806807 environment : env ?. uid ,
807808 version : 1 ,
808- locale : locale ?. toLowerCase ?. ( ) ,
809+ locale : localeCode ,
809810 } ) ) : [ ] ;
810811 const title = fields ?. [ pathName ] ?. [ locale ] || "" ;
811812 const urlTitle = title
@@ -816,7 +817,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
816817 title : title ?. trim ?.( ) === "" ? ( urlTitle || id ) : title ,
817818 uid : id ,
818819 url : `/${ name ?. toLowerCase ?.( ) } /${ urlTitle } ` ,
819- locale : locale ?. toLowerCase ?. ( ) ,
820+ locale : localeCode ,
820821 publish_details : publishDetails ,
821822 } ;
822823 // Format object keys to snake_case
@@ -843,11 +844,12 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
843844 for await ( const [ localeKey , localeValues ] of Object . entries (
844845 values as { [ key : string ] : any }
845846 ) ) {
847+ const localeCode = mapLocales ( { masterLocale : master_locale , locale : localeKey , locales : LocaleMapper } ) ;
846848 const chunks = makeChunks ( localeValues ) ;
847849 for ( const [ entryKey , entryValue ] of Object . entries ( localeValues ) ) {
848850 const message = getLogMessage (
849851 srcFunc ,
850- `Entry title "${ ( entryValue as { title : string } ) ?. title } "(${ ctName } ) in the ${ localeKey } locale has been successfully transformed.` ,
852+ `Entry title "${ ( entryValue as { title : string } ) ?. title } "(${ ctName } ) in the ${ localeCode } locale has been successfully transformed.` ,
851853 { }
852854 ) ;
853855 await customLogger ( projectId , destination_stack_id , "info" , message ) ;
@@ -856,7 +858,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
856858 let chunkIndex = 1 ;
857859 const filePath = path . join (
858860 entriesSave ,
859- ctName , localeKey . toLowerCase ( )
861+ ctName , localeCode
860862 ) ;
861863 for await ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
862864 refs [ chunkIndex ++ ] = `${ chunkId } -entries.json` ;
@@ -874,7 +876,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
874876 await customLogger ( projectId , destination_stack_id , 'info' , message ) ;
875877 }
876878 } catch ( err ) {
877- console . info ( "🚀 ~ createEntry ~ err:" , err )
879+ console . error ( "🚀 ~ createEntry ~ err:" , err )
878880 const message = getLogMessage (
879881 srcFunc ,
880882 `Error encountered while creating entries.` ,
@@ -885,6 +887,10 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
885887 }
886888} ;
887889
890+ function getKeyByValue ( obj : Record < string , string > , targetValue : string ) : string | undefined {
891+ return Object . entries ( obj ) . find ( ( [ _ , value ] ) => value === targetValue ) ?. [ 0 ] ;
892+ }
893+
888894/**
889895 * Processes and creates locale configurations from a given package file and saves them to the destination stack directory.
890896 *
@@ -909,7 +915,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
909915 *
910916 * @throws Will log errors encountered during file reading, processing, or writing of locale configurations.
911917 */
912- const createLocale = async ( packagePath : string , destination_stack_id : string , projectId : string ) => {
918+ const createLocale = async ( packagePath : string , destination_stack_id : string , projectId : string , project : any ) => {
913919 const srcFunc = 'createLocale' ;
914920 const localeSave = path . join ( DATA , destination_stack_id , LOCALE_DIR_NAME ) ;
915921 const globalFieldSave = path . join ( DATA , destination_stack_id , GLOBAL_FIELDS_DIR_NAME ) ;
@@ -933,30 +939,30 @@ const createLocale = async (packagePath: string, destination_stack_id: string, p
933939 )
934940 await customLogger ( projectId , destination_stack_id , 'error' , message ) ;
935941 }
936-
937- await Promise . all ( locales . map ( async ( localeData : any ) => {
938- const title = localeData . sys . id ;
942+ const fallbackMapLocales : any = { ...project ?. master_locale ?? { } , ...project ?. locales ?? { } }
943+ await Promise ?. all ( locales ?. map ?.( async ( localeData : any ) => {
944+ const currentMapLocale = getKeyByValue ?.( fallbackMapLocales , localeData ?. code ) ?? `${ localeData . code . toLowerCase ( ) } ` ;
945+ const title = localeData ?. sys ?. id ;
939946 const newLocale : Locale = {
940- code : ` ${ localeData . code . toLowerCase ( ) } ` ,
941- name : localeCodes ?. [ localeData . code . toLowerCase ( ) ] || "English - United States" ,
942- fallback_locale : "" ,
947+ code : currentMapLocale ,
948+ name : localeCodes ?. [ currentMapLocale ] || "English - United States" ,
949+ fallback_locale : getKeyByValue ( fallbackMapLocales , localeData ?. fallbackCode ) ?? '' ,
943950 uid : `${ title } ` ,
944951 } ;
945952
946953 if ( localeData . default === true ) {
947954 msLocale [ title ] = newLocale ;
948955 const message = getLogMessage (
949956 srcFunc ,
950- `Master Locale ${ newLocale . code } has been successfully transformed.` ,
957+ `Master Locale ${ newLocale ? .code } has been successfully transformed.` ,
951958 { }
952959 )
953960 await customLogger ( projectId , destination_stack_id , 'info' , message ) ;
954961 } else {
955- newLocale . name = `${ localeData . name } ` ;
956962 allLocales [ title ] = newLocale ;
957963 const message = getLogMessage (
958964 srcFunc ,
959- `Locale ${ newLocale . code } has been successfully transformed.` ,
965+ `Locale ${ newLocale ? .code } has been successfully transformed.` ,
960966 { }
961967 )
962968 await customLogger ( projectId , destination_stack_id , 'info' , message ) ;
0 commit comments