/***********************************************************
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License
 **********************************************************/ import { IpcMainInvokeEvent } from 'electron' ; import * as fs from 'fs' ; import { GetInterfaceDefinitionParameters , MODEL_PARSE_ERROR } from '../interfaces/modelRepositoryInterface' ; export const onGetInterfaceDefinition = ( event , { interfaceId , path } ) : object | undefined => { if ( ! interfaceId || ! path ) { throw new Error ( ) ; } const fileNames = fs . readdirSync ( path ) ; const parseErrors = [ ] ; for ( const fileName of fileNames ) { if ( ! isFileExtensionJson ( fileName ) ) { continue ; } try { const definition = getInterfaceDefinition ( path , fileName , interfaceId ) ; if ( definition ) { return definition ; } } catch { parseErrors . push ( fileName ) ; } } if ( parseErrors . length > 0 ) { const error = new Error ( MODEL_PARSE_ERROR ) ; ( error as any ) . fileNames = fileNames ; throw error ; } } ; export const getInterfaceDefinition = ( filePath , fileName , interfaceId ) : object | undefined => { const data = readFileFromLocal ( filePath , fileName ) ; const parsedData = JSON . parse ( data ) ; if ( Array . isArray ( parsedData ) ) { for ( const pd of parsedData ) { if ( pd [ '@id' ] ?. toString ( ) === interfaceId ) { return pd ; } } } else if ( parsedData [ '@id' ] ?. toString ( ) === interfaceId ) { return parsedData ; } } ; const readFileFromLocal = ( filePath , fileName ) => { return fs . readFileSync ( `${ filePath }/${ fileName }` , 'utf-8' ) ; } ; const isFileExtensionJson = ( fileName ) => { const i = fileName . lastIndexOf ( '.' ) ; return i > 0 && fileName . substr ( i ) === '.json' ; } ;