55 */
66
77import Handlebars from 'handlebars' ;
8- import { safeLoad } from 'js-yaml' ;
8+ import { safeLoad , safeDump } from 'js-yaml' ;
99import { DatasourceConfigRecord } from '../../../../common' ;
1010
11+ export function createStream ( variables : DatasourceConfigRecord , streamTemplate : string ) {
12+ const { vars, yamlValues } = buildTemplateVariables ( variables , streamTemplate ) ;
13+
14+ const template = Handlebars . compile ( streamTemplate , { noEscape : true } ) ;
15+ let stream = template ( vars ) ;
16+ stream = replaceRootLevelYamlVariables ( yamlValues , stream ) ;
17+
18+ const yamlFromStream = safeLoad ( stream , { } ) ;
19+
20+ // Hack to keep empty string ('') values around in the end yaml because
21+ // `safeLoad` replaces empty strings with null
22+ const patchedYamlFromStream = Object . entries ( yamlFromStream ) . reduce ( ( acc , [ key , value ] ) => {
23+ if ( value === null && typeof vars [ key ] === 'string' && vars [ key ] . trim ( ) === '' ) {
24+ acc [ key ] = '' ;
25+ } else {
26+ acc [ key ] = value ;
27+ }
28+ return acc ;
29+ } , { } as { [ k : string ] : any } ) ;
30+
31+ return replaceVariablesInYaml ( yamlValues , patchedYamlFromStream ) ;
32+ }
33+
1134function isValidKey ( key : string ) {
1235 return key !== '__proto__' && key !== 'constructor' && key !== 'prototype' ;
1336}
@@ -29,7 +52,7 @@ function replaceVariablesInYaml(yamlVariables: { [k: string]: any }, yaml: any)
2952 return yaml ;
3053}
3154
32- function buildTemplateVariables ( variables : DatasourceConfigRecord ) {
55+ function buildTemplateVariables ( variables : DatasourceConfigRecord , streamTemplate : string ) {
3356 const yamlValues : { [ k : string ] : any } = { } ;
3457 const vars = Object . entries ( variables ) . reduce ( ( acc , [ key , recordEntry ] ) => {
3558 // support variables with . like key.patterns
@@ -64,23 +87,15 @@ function buildTemplateVariables(variables: DatasourceConfigRecord) {
6487 return { vars, yamlValues } ;
6588}
6689
67- export function createStream ( variables : DatasourceConfigRecord , streamTemplate : string ) {
68- const { vars, yamlValues } = buildTemplateVariables ( variables ) ;
69-
70- const template = Handlebars . compile ( streamTemplate , { noEscape : true } ) ;
71- const stream = template ( vars ) ;
72- const yamlFromStream = safeLoad ( stream , { } ) ;
90+ function replaceRootLevelYamlVariables ( yamlVariables : { [ k : string ] : any } , yamlTemplate : string ) {
91+ if ( Object . keys ( yamlVariables ) . length === 0 || ! yamlTemplate ) {
92+ return yamlTemplate ;
93+ }
7394
74- // Hack to keep empty string ('') values around in the end yaml because
75- // `safeLoad` replaces empty strings with null
76- const patchedYamlFromStream = Object . entries ( yamlFromStream ) . reduce ( ( acc , [ key , value ] ) => {
77- if ( value === null && typeof vars [ key ] === 'string' && vars [ key ] . trim ( ) === '' ) {
78- acc [ key ] = '' ;
79- } else {
80- acc [ key ] = value ;
81- }
82- return acc ;
83- } , { } as { [ k : string ] : any } ) ;
95+ let patchedTemplate = yamlTemplate ;
96+ Object . entries ( yamlVariables ) . forEach ( ( [ key , val ] ) => {
97+ patchedTemplate = patchedTemplate . replace ( new RegExp ( `^"${ key } "` , 'gm' ) , safeDump ( val ) ) ;
98+ } ) ;
8499
85- return replaceVariablesInYaml ( yamlValues , patchedYamlFromStream ) ;
100+ return patchedTemplate ;
86101}
0 commit comments