UNPKG

@mondaycom/apps-cli

Version:

A cli tool to manage apps (and monday-code projects) in monday.com

23 lines (22 loc) 957 B
import Handlebars from 'handlebars'; /** * Process a JSON template file with Handlebars. * @param jsonContent - The JSON content to process. * @param vars - The variables to substitute in the template. * @param options - Additional options. * @returns The processed JSON object. */ export function processTemplate(jsonContent, vars, options = {}) { const { failOnMissingVariable = false } = options; const jsonString = JSON.stringify(jsonContent); Handlebars.registerHelper('getVar', (key) => { if (failOnMissingVariable && !(key in vars)) { throw new Error(`Missing variable: ${key}`); } return vars[key] ?? `{{${key}}}`; }); const transformedTemplate = jsonString.replaceAll(/{{\s*([\w.-]+)\s*}}/g, (_, varName) => `{{getVar "${varName}"}}`); const template = Handlebars.compile(transformedTemplate, { noEscape: true }); const output = template(vars); return JSON.parse(output); }