import { i18n } from '@kbn/i18n' ; import JSON5 from 'json5' ; import { Serializer } from '.' ; const ESCAPE_SINGLE_QUOTE_REGEX = /\\([\s\S])|(')/g ; export const serializeToJson5 = ( messages , formats = i18n . formats ) => { let jsonBuffer = Buffer . from ( JSON5 . stringify ( { formats , messages : { } } , { quote : `'` , space : 2 } ) . slice ( 0 , - 4 ) . concat ( '\n' ) ) ; for ( const [ mapKey , mapValue ] of messages ) { const formattedMessage = mapValue . message . replace ( ESCAPE_SINGLE_QUOTE_REGEX , '\\$1$2' ) ; const formattedDescription = mapValue . description ? mapValue . description . replace ( ESCAPE_SINGLE_QUOTE_REGEX , '\\$1$2' ) : '' ; jsonBuffer = Buffer . concat ( [ jsonBuffer , Buffer . from ( `    '${ mapKey }': '${ formattedMessage }',` ) , Buffer . from ( formattedDescription ? ` // ${ formattedDescription }\n` : '\n' ) , ] ) ; } jsonBuffer = Buffer . concat ( [ jsonBuffer , Buffer . from ( '  },\n}\n' ) ] ) ; return jsonBuffer . toString ( ) ; } ;