@@ -49,6 +49,18 @@ const AssetsPathSpliter = ({ path, id }: any) => {
4949 return newPath ;
5050}
5151
52+ const mapLocales = ( { masterLocale, locale, locales } : any ) => {
53+ if ( locales ?. masterLocale ?. [ masterLocale ?? '' ] === locale ) {
54+ return Object ?. keys ( locales ?. masterLocale ) ?. [ 0 ]
55+ }
56+ for ( const [ key , value ] of Object ?. entries ?.( locales ) ?? { } ) {
57+ if ( typeof value !== 'object' && value === locale ) {
58+ return key ;
59+ }
60+ }
61+ return locale ?. toLowerCase ?.( ) ;
62+ }
63+
5264
5365
5466async function writeOneFile ( indexPath : string , fileMeta : any ) {
@@ -194,7 +206,7 @@ const cretaeAssets = async ({ packagePath, baseDir, destinationStackId, projectI
194206 return allAssetJSON ;
195207}
196208
197- const createEntry = async ( { packagePath, contentTypes, master_locale, destinationStackId, projectId, keyMapper } : { packagePath : any ; contentTypes : any ; master_locale ?: string , destinationStackId : string , projectId : string , keyMapper : any } ) => {
209+ const createEntry = async ( { packagePath, contentTypes, master_locale, destinationStackId, projectId, keyMapper, project } : { packagePath : any ; contentTypes : any ; master_locale ?: string , destinationStackId : string , projectId : string , keyMapper : any , project : any } ) => {
198210 try {
199211 const srcFunc = 'createEntry' ;
200212 const baseDir = path . join ( baseDirName , destinationStackId ) ;
@@ -215,7 +227,11 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
215227 const templateIndex = entriesData ?. findIndex ( ( ele : any ) => ele ?. template === template ) ;
216228 if ( templateIndex >= 0 ) {
217229 const entry = entriesData ?. [ templateIndex ] ?. locale ?. [ language ] ;
218- entry [ id ] = { meta : jsonData ?. item ?. $ , fields : jsonData ?. item ?. fields } ;
230+ if ( entry !== undefined ) {
231+ entry [ id ] = { meta : jsonData ?. item ?. $ , fields : jsonData ?. item ?. fields } ;
232+ } else {
233+ entriesData [ templateIndex ] . locale [ language ] = entries ;
234+ }
219235 } else {
220236 const locale : any = { } ;
221237 locale [ language ] = entries ;
@@ -227,61 +243,59 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
227243 for await ( const ctType of contentTypes ) {
228244 const message = getLogMessage (
229245 srcFunc ,
230- `Transforming entries of Content Type ${ keyMapper [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } has begun.` ,
246+ `Transforming entries of Content Type ${ keyMapper ?. [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } has begun.` ,
231247 { }
232248 )
233249 await customLogger ( projectId , destinationStackId , 'info' , message ) ;
234250 const entryPresent : any = entriesData ?. find ( ( item : any ) => uidCorrector ( { uid : item ?. template } ) === ctType ?. contentstackUid )
235251 if ( entryPresent ) {
236252 const locales : any = Object ?. keys ( entryPresent ?. locale ) ;
253+ const allLocales : any = { masterLocale : project ?. master_locale ?? LOCALE_MAPPER ?. masterLocale , ...project ?. locales ?? { } }
237254 for await ( const locale of locales ) {
238- let newLocale = locale ;
255+ const newLocale = mapLocales ( { masterLocale : master_locale , locale, locales : allLocales } ) ;
239256 const entryLocale : any = { } ;
240- if ( typeof LOCALE_MAPPER ?. masterLocale === 'object' && LOCALE_MAPPER ?. masterLocale !== null && LOCALE_MAPPER ?. masterLocale ?. [ master_locale ?? '' ] === locale ) {
241- newLocale = Object ?. keys ( LOCALE_MAPPER ?. masterLocale ) ?. [ 0 ] ;
242- Object . entries ( entryPresent ?. locale ?. [ locale ] || { } ) . map ( async ( [ uid , entry ] : any ) => {
243- const entryObj : any = { } ;
244- entryObj . uid = uid ;
245- for await ( const field of entry ?. fields ?. field ?? [ ] ) {
246- for await ( const fsc of ctType ?. fieldMapping ?? [ ] ) {
247- if ( fsc ?. contentstackFieldType !== 'group' && ! field ?. $ ?. key ?. includes ( '__' ) ) {
248- if ( fsc ?. contentstackFieldUid === 'title' ) {
249- entryObj [ fsc ?. contentstackFieldUid ] = entry ?. meta ?. name ;
250- }
251- if ( fsc ?. contentstackFieldUid === 'url' ) {
252- entryObj [ fsc ?. contentstackFieldUid ] = `/${ entry ?. meta ?. key } ` ;
253- }
254- if ( getLastKey ( fsc ?. uid ) === field ?. $ ?. key ) {
255- const content : any = await entriesFieldCreator ( { field : fsc , content : field ?. content , idCorrector, allAssetJSON, contentTypes, entriesData, locale } ) ;
256- const gpData : any = ctType ?. fieldMapping ?. find ( ( elemant : any ) => elemant ?. uid === fsc ?. uid ?. split ( '.' ) ?. [ 0 ] ) ;
257- if ( gpData ?. uid ) {
258- const ctUid = uidCorrector ( { uid : gpData ?. uid } ) ;
259- if ( ctUid !== gpData ?. contentstackFieldUid && fsc ?. contentstackFieldUid ?. includes ( ctUid ) ) {
260- const newUid : any = fsc ?. contentstackFieldUid ?. replace ( ctUid , gpData ?. contentstackFieldUid ) ;
261- entryObj [ newUid ] = content ;
262- } else {
263- entryObj [ fsc ?. contentstackFieldUid ] = content ;
264- }
257+ Object . entries ( entryPresent ?. locale ?. [ locale ] || { } ) . map ( async ( [ uid , entry ] : any ) => {
258+ const entryObj : any = { } ;
259+ entryObj . uid = uid ;
260+ for await ( const field of entry ?. fields ?. field ?? [ ] ) {
261+ for await ( const fsc of ctType ?. fieldMapping ?? [ ] ) {
262+ if ( fsc ?. contentstackFieldType !== 'group' && ! field ?. $ ?. key ?. includes ( '__' ) ) {
263+ if ( fsc ?. contentstackFieldUid === 'title' ) {
264+ entryObj [ fsc ?. contentstackFieldUid ] = entry ?. meta ?. name ;
265+ }
266+ if ( fsc ?. contentstackFieldUid === 'url' ) {
267+ entryObj [ fsc ?. contentstackFieldUid ] = `/${ entry ?. meta ?. key } ` ;
268+ }
269+ if ( getLastKey ( fsc ?. uid ) === field ?. $ ?. key ) {
270+ const content : any = await entriesFieldCreator ( { field : fsc , content : field ?. content , idCorrector, allAssetJSON, contentTypes, entriesData, locale } ) ;
271+ const gpData : any = ctType ?. fieldMapping ?. find ( ( elemant : any ) => elemant ?. uid === fsc ?. uid ?. split ( '.' ) ?. [ 0 ] ) ;
272+ if ( gpData ?. uid ) {
273+ const ctUid = uidCorrector ( { uid : gpData ?. uid } ) ;
274+ if ( ctUid !== gpData ?. contentstackFieldUid && fsc ?. contentstackFieldUid ?. includes ( ctUid ) ) {
275+ const newUid : any = fsc ?. contentstackFieldUid ?. replace ( ctUid , gpData ?. contentstackFieldUid ) ;
276+ entryObj [ newUid ] = content ;
265277 } else {
266278 entryObj [ fsc ?. contentstackFieldUid ] = content ;
267279 }
280+ } else {
281+ entryObj [ fsc ?. contentstackFieldUid ] = content ;
268282 }
269283 }
270284 }
271285 }
272- entryObj . publish_details = [ ] ;
273- if ( Object . keys ?. ( entryObj ) ?. length > 1 ) {
274- entryLocale [ uid ] = unflatten ( entryObj ) ?? { } ;
275- const message = getLogMessage (
276- srcFunc ,
277- `Entry title " ${ entryObj ?. title } "( ${ keyMapper [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } ) in the ${ newLocale } locale has been successfully transformed.` ,
278- { }
279- )
280- await customLogger ( projectId , destinationStackId , 'info' , message )
281- }
282- } ) ;
283- }
284- const mapperCt : string = ( keyMapper [ ctType ?. contentstackUid ] !== "" && keyMapper [ ctType ?. contentstackUid ] !== undefined ) ? keyMapper [ ctType ?. contentstackUid ]
286+ }
287+ entryObj . publish_details = [ ] ;
288+ if ( Object . keys ?. ( entryObj ) ?. length > 1 ) {
289+ entryLocale [ uid ] = unflatten ( entryObj ) ?? { } ;
290+ const message = getLogMessage (
291+ srcFunc ,
292+ `Entry title " ${ entryObj ?. title } "( ${ keyMapper ?. [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } ) in the ${ newLocale } locale has been successfully transformed.` ,
293+ { }
294+ )
295+ await customLogger ( projectId , destinationStackId , 'info' , message )
296+ }
297+ } ) ;
298+ const mapperCt : string = ( keyMapper ?. [ ctType ?. contentstackUid ] !== "" && keyMapper ?. [ ctType ?. contentstackUid ] !== undefined ) ? keyMapper ?. [ ctType ?. contentstackUid ]
285299 : ctType ?. contentstackUid ;
286300 const fileMeta = { "1" : `${ newLocale } .json` } ;
287301 const entryPath = path . join (
@@ -295,11 +309,11 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
295309 } else {
296310 const message = getLogMessage (
297311 srcFunc ,
298- `No entries found for the content type ${ keyMapper [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } .` ,
312+ `No entries found for the content type ${ keyMapper ?. [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } .` ,
299313 { }
300314 )
301315 await customLogger ( projectId , destinationStackId , 'error' , message )
302- console . info ( 'Entries missing for' , keyMapper [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid )
316+ console . info ( 'Entries missing for' , keyMapper ?. [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid )
303317 }
304318 }
305319 return true ;
@@ -308,13 +322,13 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
308322 }
309323}
310324
311- const createLocale = async ( req : any , destinationStackId : string , projectId : string ) => {
325+ const createLocale = async ( req : any , destinationStackId : string , projectId : string , project : any ) => {
312326 const srcFunc = 'createLocale' ;
313327 try {
314328 const baseDir = path . join ( baseDirName , destinationStackId ) ;
315329 const localeSave = path . join ( baseDir , LOCALE_DIR_NAME ) ;
316330 const allLocalesResp = await orgService . getLocales ( req )
317- const masterLocale = Object ?. keys ?.( LOCALE_MAPPER ?. masterLocale ) ?. [ 0 ] ;
331+ const masterLocale = Object ?. keys ?.( project ?. master_locale ?? LOCALE_MAPPER ?. masterLocale ) ?. [ 0 ] ;
318332 const msLocale : any = { } ;
319333 const uid = uuidv4 ( ) ;
320334 msLocale [ uid ] = {
@@ -330,14 +344,14 @@ const createLocale = async (req: any, destinationStackId: string, projectId: str
330344 )
331345 await customLogger ( projectId , destinationStackId , 'info' , message ) ;
332346 const allLocales : any = { } ;
333- for ( const [ key , value ] of Object . entries ( LOCALE_MAPPER ) ) {
347+ for ( const [ key , value ] of Object . entries ( project ?. locales ?? LOCALE_MAPPER ) ) {
334348 const localeUid = uuidv4 ( ) ;
335349 if ( key !== 'masterLocale' && typeof value === 'string' ) {
336350 allLocales [ localeUid ] = {
337- "code" : value ,
351+ "code" : key ,
338352 "fallback_locale" : masterLocale ,
339353 "uid" : localeUid ,
340- "name" : allLocalesResp ?. data ?. locales ?. [ value ] ?? ''
354+ "name" : allLocalesResp ?. data ?. locales ?. [ key ] ?? ''
341355 }
342356 const message = getLogMessage (
343357 srcFunc ,
0 commit comments