"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const handlebars_1 = __importDefault(require("handlebars"));
const fs_1 = __importDefault(require("fs"));
const logger_1 = __importDefault(require("../logger"));
const path_1 = __importDefault(require("path"));
const existingHandlebars = ["now", "randomValue", "capture", "num_between", "file", "code", "inject"];
/**
* - If file exists read file and parse it to a JSONObject of type CustomHandleBar
* - For each custom handlebar, check if the name equals any of the inbuilt handlebars
* - If not, register helpers by their name and executing the IIFE code provided under logic
* - Create request and logger objects under the scope of each custom handlebar
* @param {string} extHelpers location of the external handlebars json file
*/
const registerCustomHandlebars = (extHelpers) => {
if (fs_1.default.existsSync(path_1.default.resolve(extHelpers))) {
logger_1.default.info(`Loading custom handlebar helpers from ${extHelpers}`);
const customHandleBarDefinition = fs_1.default.readFileSync(path_1.default.resolve(extHelpers)).toString();
const customHandlebars = JSON.parse(customHandleBarDefinition);
customHandlebars.forEach((customHandlebar) => {
if (customHandlebar.name in existingHandlebars) {
logger_1.default.error(`Cannot override custom helper ${customHandlebar.name}`);
}
else {
logger_1.default.info(`Registering custom handlebars: ${customHandlebar.name}`);
handlebars_1.default.registerHelper(customHandlebar.name, (context) => {
/* eslint-disable no-unused-vars */
const request = context.data.root.request;
const logger = context.data.root.logger;
/* eslint-disable no-unused-vars */
const result = eval(customHandlebar.logic);
return result;
});
}
});
}
else {
logger_1.default.error(`Loading custom handlebar helpers from ${extHelpers} failed. File not found.`);
}
};
exports.default = registerCustomHandlebars;
//# sourceMappingURL=loadCustomHandlebars.js.map