import { KameletModel , Property } from "../model/KameletModels" ; import * as yaml from 'js-yaml' ; export const Kamelets = [ ] ; export const KameletApi = { getKameletProperties : ( kameletName ) : Property [ ] => { const kamelet = KameletApi . findKameletByName ( kameletName ) ; const properties = [ ] ; try { if ( kamelet !== undefined ) { const map = kamelet . spec . definition . properties ? new Map ( Object . entries ( kamelet . spec . definition . properties ) ) : new Map ( ) ; map . forEach ( ( value , key , map ) => { const prop = new Property ( ) ; prop . id = key ; prop . title = value . title ; prop . default = value . default ; prop . description = value . description ; prop . format = value . format ; prop . example = value . example ; prop . type = value . type ; if ( value . default ) prop . value = value . default ; prop [ "x-descriptors" ] = value [ "x-descriptors" ] ; properties . push ( prop ) ; } ) ; } } finally { return properties ; } } , getKamelets : ( ) : KameletModel [ ] => { return Kamelets . sort ( ( a , b ) => { if ( a . title ( ) . toLowerCase ( ) < b . title ( ) . toLowerCase ( ) ) { return - 1 ; } return a . title ( ) . toLowerCase ( ) > b . title ( ) . toLowerCase ( ) ? 1 : 0 ; } ) ; } , jsonToKamelet : ( json ) => { const fromJson = JSON . parse ( json ) as KameletModel ; const k = new KameletModel ( fromJson ) ; return k ; } , findKameletByName : ( name ) : KameletModel | undefined => { return Kamelets . find ( ( k ) => k . metadata . name === name ) ; } , findKameletByUri : ( uri ) : KameletModel | undefined => { return KameletApi . findKameletByName ( uri . split ( ":" ) [ 1 ] ) ; } , yamlToKamelet : ( text ) : KameletModel => { const fromYaml = yaml . load ( text ) ; return KameletApi . jsonToKamelet ( JSON . stringify ( fromYaml ) ) ; } , saveKamelets : ( kameletYamls ) => { const kamelets = kameletYamls . map ( text => KameletApi . yamlToKamelet ( text ) ) ; Kamelets . push ( ... kamelets . sort ( ( a , b ) => { if ( a . spec . definition . title . toLowerCase ( ) < b . spec . definition . title . toLowerCase ( ) ) { return - 1 ; } return a . spec . definition . title . toLowerCase ( ) > b . spec . definition . title . toLowerCase ( ) ? 1 : 0 ; } ) ) ; } , saveKamelet : ( yaml ) => { const kamelet = KameletApi . yamlToKamelet ( yaml ) ; Kamelets . push ( kamelet ) ; } } ;