UNPKG

alapa

Version:

A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.

79 lines (78 loc) 3.15 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateEngine = void 0; const fs = __importStar(require("fs")); const path_1 = __importDefault(require("path")); const logger_1 = require("../../logger"); /** * An abstract class that defines a template rendering and compilation interface. * * Implementations of this class should provide specific logic for rendering * templates and compiling source code into strings. */ class TemplateEngine { /** * Renders a template with the provided data. * * @param template - The path to the file to read throw error if file now found. * @param data - Optional data to be used for rendering the template. The * shape of this data depends on the specific implementation * and the template being used. * @returns The rendered string resulting from applying the data to the template. */ async render(template, data) { try { const source = await this.readFileToString(template); return this.compile(source, data); // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (err) { throw logger_1.Logger.throw(`Error rendering HTML template: ${err.message}`); } } readFileToString(filePath) { /** * Reads a file at the given path and returns its contents as a string. * * @param filePath - The path to the file to read. * @returns A Promise that resolves to the file contents as a string. */ return new Promise((resolve, reject) => { fs.readFile(path_1.default.resolve(`${filePath}${this.extension}`), "utf8", (err, data) => { if (err) { reject(err); } else { resolve(data); } }); }); } } exports.TemplateEngine = TemplateEngine;