import * as yaml from 'js-yaml' ; import { Integration , CamelElement , } from "../model/CamelModel" ; import { CamelUtil } from "./CamelUtil" ; import { CamelYamlStep } from "./CamelYamlSteps" ; const saveKebabCase = true ; export class CamelYaml { static integrationToYaml = ( integration ) : string => { const clone = Object . assign ( { } , integration ) ; const flows = integration . spec . flows ; clone . spec . flows = flows . map ( ( f ) => CamelYaml . cleanupElement ( f ) ) ; if ( integration . crd ) { delete clone . crd ; const i = JSON . parse ( JSON . stringify ( clone , null , 3 ) ) ; const text = CamelYaml . yamlDump ( i ) ; return text ; } else { const f = JSON . parse ( JSON . stringify ( clone . spec . flows , null , 3 ) ) ; const text = CamelYaml . yamlDump ( f ) ; return text ; } } ; static cleanupElement = ( element ) : CamelElement => { const result = { } ; const object = Object . assign ( { } , element ) ; if ( object . dslName === 'expression' ) { delete object . language ; } delete object . uuid ; Object . keys ( object ) . sort ( ( a , b ) => { if ( a === 'uri' ) return - 1 ; else if ( a === 'steps' ) return 1 ; else return 0 ; } ) . forEach ( key => { const rkey = saveKebabCase ? key . replace ( /([a-z0-9]|(?=[A-Z]))([A-Z])/g , '$1-$2' ) . toLowerCase ( ) : key ; if ( object [ key ] instanceof CamelElement ) { result [ rkey ] = CamelYaml . cleanupElement ( object [ key ] ) ; } else if ( Array . isArray ( object [ key ] ) ) { if ( object [ key ] . length > 0 ) result [ rkey ] = CamelYaml . cleanupElements ( object [ key ] ) ; } else if ( key === 'parameters' && typeof ( object [ key ] ) === 'object' ) { const obj = object [ key ] ; const parameters = Object . keys ( obj || { } ) . reduce ( ( x , k ) => { if ( obj [ k ] !== null && obj [ k ] !== undefined && obj [ k ] . toString ( ) . trim ( ) . length > 0 ) { x [ k ] = obj [ k ] ; } return x ; } , { } ) ; if ( Object . keys ( parameters ) . length > 0 ) result [ rkey ] = parameters ; } else { if ( object [ key ] !== undefined && object [ key ] . toString ( ) . trim ( ) . length > 0 ) result [ rkey ] = object [ key ] ; } } ) ; return result as CamelElement ; } ; static cleanupElements = ( elements ) : CamelElement [ ] => { const result = [ ] ; elements . forEach ( element => { const newElement = CamelYaml . cleanupElement ( element ) ; result . push ( newElement ) ; } ) ; return result ; } ; static yamlDump = ( integration ) : string => { return yaml . dump ( integration , { noRefs : false , noArrayIndent : false , sortKeys : function ( a , b ) { if ( a === 'uri' ) return - 1 ; else if ( b === 'uri' ) return 0 ; else if ( a === 'steps' && b !== 'uri' ) return - 1 ; else if ( b === 'steps' && a !== 'uri' ) return - 1 ; else if ( a > b ) return 1 ; else return 0 ; } , replacer : CamelUtil . replacer } ) ; } ; static yamlToIntegration = ( filename , text ) : Integration => { const i = Integration . createNew ( filename ) ; const fromYaml = yaml . load ( text ) ; if ( Array . isArray ( fromYaml ) ) { i . crd = false ; const flows = fromYaml ; const froms = flows . filter ( ( e ) => e . hasOwnProperty ( 'from' ) ) ; if ( froms . length > 0 ) { froms . forEach ( ( f ) => i . spec . flows . push ( CamelYamlStep . readFrom ( f ) ) ) ; } } else { i . crd = true ; const int = new Integration ( { ... fromYaml } ) ; int . spec . flows . forEach ( ( f ) => i . spec . flows . push ( CamelYamlStep . readFrom ( f ) ) ) ; } return i ; } ; }