import * as fs from 'fs' ; import * as chalk from 'chalk' ; import { messageKeys } from '@notification/i18n/messages' ; import { ILanguage } from '@notification/features/sms/utils' ; interface IMessageKey { [ key ] : string ; } interface IMessageDescriptions { data : IMessageKey ; } function existsInContentful ( obj , value ) { if ( Object . values ( obj ) . indexOf ( value ) > - 1 ) { return true ; } return false ; } async function extractMessages ( ) { const RESOURCES_PATH = process . argv [ 2 ] ; const COUNTRY_CODE = process . argv [ 3 ] ; const notification = JSON . parse ( fs . readFileSync ( `${ RESOURCES_PATH }/src/${ COUNTRY_CODE }/features/languages/generated/notification/notification.json` ) . toString ( ) ) ; const contentfulIds = JSON . parse ( fs . readFileSync ( `${ RESOURCES_PATH }/src/${ COUNTRY_CODE }/features/languages/generated/notification/contentful-ids.json` ) . toString ( ) ) ; const descriptions = JSON . parse ( fs . readFileSync ( `${ RESOURCES_PATH }/src/${ COUNTRY_CODE }/features/languages/generated/notification/descriptions.json` ) . toString ( ) ) ; const englishTranslations = notification . data . find ( ( obj ) => obj . lang === 'en' ) . messages ; try { console . log ( `${ chalk . yellow ( 'Checking translations in notification service ...' ) }` ) ; Object . keys ( messageKeys ) . forEach ( messageKey => { const contentfulKeysToMigrate = [ ] ; let missingKeys = false ; if ( ! englishTranslations . hasOwnProperty ( messageKey ) ) { missingKeys = true ; console . log ( `${ chalk . red ( `No English translation key exists for messageKey.  Remeber to translate and add for all locales!!!: ${ chalk . white ( messageKey ) } in ${ chalk . white ( `${ RESOURCES_PATH }/src/${ COUNTRY_CODE }/features/languages/generated/notification/notification.json` ) }` ) }` ) ; } if ( contentfulIds && ! existsInContentful ( contentfulIds , messageKey ) ) { console . log ( `${ chalk . red ( `You have set up a Contentful Content Management System.  OpenCRVS core has created this new key in this version: ${ chalk . white ( messageKey ) } in ${ chalk . white ( `${ messageKey }` ) }` ) }` ) ; console . log ( `${ chalk . yellow ( 'This key must be migrated into your Contentful CMS.  Saving to ...' ) } in ${ chalk . white ( `${ RESOURCES_PATH }/src/${ COUNTRY_CODE }/features/languages/generated/notification/contentful-keys-to-migrate.json` ) }` ) ; contentfulKeysToMigrate . push ( messageKey ) ; } if ( ! descriptions . data . hasOwnProperty ( messageKey ) ) { missingKeys = true ; console . log ( `${ chalk . red ( `No description exists for messageKey: ${ chalk . white ( messageKey ) } in ${ chalk . white ( `${ RESOURCES_PATH }/src/${ COUNTRY_CODE }/features/languages/generated/notification/descriptions.json` ) }` ) }` ) ; } if ( missingKeys ) { console . log ( `${ chalk . red ( 'WARNING: ' ) }${ chalk . yellow ( 'Fix missing keys in locale files first.' ) }` ) ; process . exit ( 1 ) ; return ; } fs . writeFileSync ( `${ RESOURCES_PATH }/src/${ COUNTRY_CODE }/features/languages/generated/notification/contentful-keys-to-migrate.json` , JSON . stringify ( contentfulKeysToMigrate , null , 2 ) ) ; } ) ; } catch ( err ) { console . log ( err ) ; process . exit ( 1 ) ; return ; } } extractMessages ( ) ;