1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-25 16:29:44 +02:00
portainer/app/react/portainer/custom-templates/components/utils.ts

75 lines
1.7 KiB
TypeScript

import _ from 'lodash';
import Mustache from 'mustache';
import { VariableDefinition } from './CustomTemplatesVariablesDefinitionField/CustomTemplatesVariablesDefinitionField';
export function getTemplateVariables(templateStr: string) {
const template = validateAndParse(templateStr);
if (!template) {
return null;
}
return template
.filter(([type, value]) => type === 'name' && value)
.map(([, value]) => ({
name: value,
label: '',
defaultValue: '',
description: '',
}));
}
function validateAndParse(templateStr: string) {
if (!templateStr) {
return [];
}
try {
return Mustache.parse(templateStr);
} catch (e) {
return null;
}
}
export function intersectVariables(
oldVariables: VariableDefinition[] = [],
newVariables: VariableDefinition[] = []
) {
const oldVariablesWithLabel = oldVariables.filter((v) => !!v.label);
return _.uniqBy(
[
...oldVariablesWithLabel,
...newVariables.filter(
(v) => !oldVariablesWithLabel.find(({ name }) => name === v.name)
),
],
'name'
);
}
export function renderTemplate(
template: string,
variables: Record<string, string>,
definitions: VariableDefinition[]
) {
const state = Object.fromEntries(
_.compact(
Object.entries(variables).map(([name, value]) => {
if (value) {
return [name, value];
}
const definition = definitions.find((def) => def.name === name);
if (!definition) {
return null;
}
return [name, definition.defaultValue || `{{ ${definition.name} }}`];
})
)
);
return Mustache.render(template, state, undefined, { escape: (t) => t });
}