import { ITerminal , NewlineKind , JsonFile } from '@rushstack/node-core-library' ; import { ILocalizationFile } from '../interfaces' ; import { ResxReader } from './ResxReader' ; import { Constants } from './Constants' ; internal export interface IParseLocFileOptions { terminal : ITerminal ; filePath : string ; content : string ; resxNewlineNormalization : NewlineKind | undefined ; } interface IParseCacheEntry { content : string ; parsedFile : ILocalizationFile ; } const parseCache = new Map < string , IParseCacheEntry > ( ) ; internal export class LocFileParser { public static parseLocFile ( options ) { const fileCacheKey = `${ options . filePath }?${ options . resxNewlineNormalization || 'none' }` ; if ( parseCache . has ( fileCacheKey ) ) { const entry = parseCache . get ( fileCacheKey ) ! ; if ( entry . content === options . content ) { return entry . parsedFile ; } } let parsedFile ; if ( /\.resx$/i . test ( options . filePath ) ) { parsedFile = ResxReader . readResxAsLocFile ( options . content , { terminal : options . terminal , resxFilePath : options . filePath , newlineNormalization : options . resxNewlineNormalization } ) ; } else { parsedFile = JsonFile . parseString ( options . content ) ; try { Constants . LOC_JSON_SCHEMA . validateObject ( parsedFile , options . filePath ) ; } catch ( e ) { options . terminal . writeError ( `The loc file is invalid. Error: ${ e }` ) ; } } parseCache . set ( fileCacheKey , { content : options . content , parsedFile } ) ; return parsedFile ; } }