@@ -8,9 +8,7 @@ import jsonpath from "jsonpath";
88import pLimit from 'p-limit' ;
99import { JSDOM } from "jsdom" ;
1010import { jsonToHtml , jsonToMarkdown , htmlToJson } from '@contentstack/json-rte-serializer' ;
11-
12-
13- import { CHUNK_SIZE , MIGRATION_DATA_CONFIG , LOCALE_MAPPER } from "../constants/index.js" ;
11+ import { CHUNK_SIZE , LOCALE_MAPPER , MIGRATION_DATA_CONFIG } from "../constants/index.js" ;
1412import { Locale } from "../models/types.js" ;
1513import jsonRTE from "./contentful/jsonRTE.js" ;
1614import { getAllLocales , getLogMessage } from "../utils/index.js" ;
@@ -86,6 +84,18 @@ function makeChunks(assetData: any) {
8684 return chunks ;
8785}
8886
87+ const mapLocales = ( { masterLocale, locale, locales } : any ) => {
88+ if ( locales ?. masterLocale ?. [ masterLocale ?? '' ] === locale ) {
89+ return Object ?. keys ( locales ?. masterLocale ) ?. [ 0 ]
90+ }
91+ for ( const [ key , value ] of Object ?. entries ?.( locales ) ?? { } ) {
92+ if ( typeof value !== 'object' && value === locale ) {
93+ return key ;
94+ }
95+ }
96+ return locale . toLowerCase ( ) ;
97+ }
98+
8999const transformCloudinaryObject = ( input : any ) => {
90100 const result : any = [ ] ;
91101 if ( ! Array . isArray ( input ) ) {
@@ -122,7 +132,7 @@ const transformCloudinaryObject = (input: any) => {
122132 id : uuidv4 ( ) ,
123133 folder : "" ,
124134 cs_metadata : {
125- config_label : "config "
135+ config_label : "default_multi_config_key "
126136 }
127137 } ) ;
128138 }
@@ -702,17 +712,6 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
702712 }
703713} ;
704714
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- }
716715
717716/**
718717 * Creates and processes entries from a given package file and saves them to the destination stack directory.
@@ -740,7 +739,7 @@ const mapLocales = ({ masterLocale, locale, locales }: any) => {
740739 *
741740 * @throws Will log errors encountered during file reading, processing, or writing of entries.
742741 */
743- const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any , master_locale : string ) : Promise < void > => {
742+ const createEntry = async ( packagePath : any , destination_stack_id : string , projectId : string , contentTypes : any , mapperKeys : any , master_locale : string , project : any ) : Promise < void > => {
744743 const srcFunc = 'createEntry' ;
745744 try {
746745 const entriesSave = path . join ( DATA , destination_stack_id , ENTRIES_DIR_NAME ) ;
@@ -749,7 +748,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
749748 const data = await fs . promises . readFile ( packagePath , "utf8" ) ;
750749 const entries = JSON . parse ( data ) ?. entries ;
751750 const content = JSON . parse ( data ) ?. contentTypes ;
752-
751+ const LocaleMapper = { masterLocale : project ?. master_locale ?? LOCALE_MAPPER ?. masterLocale , ... project ?. locales ?? { } } ;
753752 if ( entries && entries . length > 0 ) {
754753 const assetId = await readFile ( assetsSave , ASSETS_SCHEMA_FILE ) ?? [ ] ;
755754 const entryId = await readFile ( path . join ( DATA , destination_stack_id , REFERENCES_DIR_NAME ) , REFERENCES_FILE_NAME ) ;
@@ -799,13 +798,13 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
799798 } ) ;
800799 const pathName = getDisplayName ( name , displayField ) ;
801800 locales . forEach ( ( locale ) => {
802- const localeCode = mapLocales ( { masterLocale : master_locale , locale, locales : LOCALE_MAPPER } ) ;
801+ const localeCode = mapLocales ( { masterLocale : master_locale , locale, locales : LocaleMapper } ) ;
803802 const publishDetails = Object ?. values ?.( environmentsId ) ?. length ? Object ?. values ?.( environmentsId )
804803 . filter ( ( env : any ) => env ?. name === environment_id )
805804 ?. map ( ( env : any ) => ( {
806805 environment : env ?. uid ,
807806 version : 1 ,
808- locale : locale ?. toLowerCase ?. ( ) ,
807+ locale : localeCode ,
809808 } ) ) : [ ] ;
810809 const title = fields ?. [ pathName ] ?. [ locale ] || "" ;
811810 const urlTitle = title
@@ -816,7 +815,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
816815 title : title ?. trim ?.( ) === "" ? ( urlTitle || id ) : title ,
817816 uid : id ,
818817 url : `/${ name ?. toLowerCase ?.( ) } /${ urlTitle } ` ,
819- locale : locale ?. toLowerCase ?. ( ) ,
818+ locale : localeCode ,
820819 publish_details : publishDetails ,
821820 } ;
822821 // Format object keys to snake_case
@@ -843,11 +842,12 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
843842 for await ( const [ localeKey , localeValues ] of Object . entries (
844843 values as { [ key : string ] : any }
845844 ) ) {
845+ const localeCode = mapLocales ( { masterLocale : master_locale , locale : localeKey , locales : LocaleMapper } ) ;
846846 const chunks = makeChunks ( localeValues ) ;
847847 for ( const [ entryKey , entryValue ] of Object . entries ( localeValues ) ) {
848848 const message = getLogMessage (
849849 srcFunc ,
850- `Entry title "${ ( entryValue as { title : string } ) ?. title } "(${ ctName } ) in the ${ localeKey } locale has been successfully transformed.` ,
850+ `Entry title "${ ( entryValue as { title : string } ) ?. title } "(${ ctName } ) in the ${ localeCode } locale has been successfully transformed.` ,
851851 { }
852852 ) ;
853853 await customLogger ( projectId , destination_stack_id , "info" , message ) ;
@@ -856,7 +856,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
856856 let chunkIndex = 1 ;
857857 const filePath = path . join (
858858 entriesSave ,
859- ctName , localeKey . toLowerCase ( )
859+ ctName , localeCode
860860 ) ;
861861 for await ( const [ chunkId , chunkData ] of Object . entries ( chunks ) ) {
862862 refs [ chunkIndex ++ ] = `${ chunkId } -entries.json` ;
@@ -874,7 +874,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
874874 await customLogger ( projectId , destination_stack_id , 'info' , message ) ;
875875 }
876876 } catch ( err ) {
877- console . info ( "🚀 ~ createEntry ~ err:" , err )
877+ console . error ( "🚀 ~ createEntry ~ err:" , err )
878878 const message = getLogMessage (
879879 srcFunc ,
880880 `Error encountered while creating entries.` ,
@@ -885,6 +885,10 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
885885 }
886886} ;
887887
888+ function getKeyByValue ( obj : Record < string , string > , targetValue : string ) : string | undefined {
889+ return Object . entries ( obj ) . find ( ( [ _ , value ] ) => value === targetValue ) ?. [ 0 ] ;
890+ }
891+
888892/**
889893 * Processes and creates locale configurations from a given package file and saves them to the destination stack directory.
890894 *
@@ -909,7 +913,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
909913 *
910914 * @throws Will log errors encountered during file reading, processing, or writing of locale configurations.
911915 */
912- const createLocale = async ( packagePath : string , destination_stack_id : string , projectId : string ) => {
916+ const createLocale = async ( packagePath : string , destination_stack_id : string , projectId : string , project : any ) => {
913917 const srcFunc = 'createLocale' ;
914918 const localeSave = path . join ( DATA , destination_stack_id , LOCALE_DIR_NAME ) ;
915919 const globalFieldSave = path . join ( DATA , destination_stack_id , GLOBAL_FIELDS_DIR_NAME ) ;
@@ -933,30 +937,30 @@ const createLocale = async (packagePath: string, destination_stack_id: string, p
933937 )
934938 await customLogger ( projectId , destination_stack_id , 'error' , message ) ;
935939 }
936-
937- await Promise . all ( locales . map ( async ( localeData : any ) => {
938- const title = localeData . sys . id ;
940+ const fallbackMapLocales : any = { ...project ?. master_locale ?? { } , ...project ?. locales ?? { } }
941+ await Promise ?. all ( locales ?. map ?.( async ( localeData : any ) => {
942+ const currentMapLocale = getKeyByValue ?.( fallbackMapLocales , localeData ?. code ) ?? `${ localeData . code . toLowerCase ( ) } ` ;
943+ const title = localeData ?. sys ?. id ;
939944 const newLocale : Locale = {
940- code : ` ${ localeData . code . toLowerCase ( ) } ` ,
941- name : localeCodes ?. [ localeData . code . toLowerCase ( ) ] || "English - United States" ,
942- fallback_locale : "" ,
945+ code : currentMapLocale ,
946+ name : localeCodes ?. [ currentMapLocale ] || "English - United States" ,
947+ fallback_locale : getKeyByValue ( fallbackMapLocales , localeData ?. fallbackCode ) ?? '' ,
943948 uid : `${ title } ` ,
944949 } ;
945950
946951 if ( localeData . default === true ) {
947952 msLocale [ title ] = newLocale ;
948953 const message = getLogMessage (
949954 srcFunc ,
950- `Master Locale ${ newLocale . code } has been successfully transformed.` ,
955+ `Master Locale ${ newLocale ? .code } has been successfully transformed.` ,
951956 { }
952957 )
953958 await customLogger ( projectId , destination_stack_id , 'info' , message ) ;
954959 } else {
955- newLocale . name = `${ localeData . name } ` ;
956960 allLocales [ title ] = newLocale ;
957961 const message = getLogMessage (
958962 srcFunc ,
959- `Locale ${ newLocale . code } has been successfully transformed.` ,
963+ `Locale ${ newLocale ? .code } has been successfully transformed.` ,
960964 { }
961965 )
962966 await customLogger ( projectId , destination_stack_id , 'info' , message ) ;
0 commit comments