UNPKG

kist

Version:

Package Pipeline Processor

96 lines (95 loc) 5.07 kB
"use strict"; // ============================================================================ // Imports // ============================================================================ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateRenderAction = void 0; const promises_1 = require("fs/promises"); const glob_1 = require("glob"); const nunjucks_1 = __importDefault(require("nunjucks")); const path_1 = __importDefault(require("path")); const Action_1 = require("../../core/pipeline/Action"); const nunjucks_config_js_1 = __importDefault(require("./nunjucks.config.js")); // ============================================================================ // Classes // ============================================================================ class TemplateRenderAction extends Action_1.Action { execute(options) { return __awaiter(this, void 0, void 0, function* () { const { templatesDir = "./templates", outputDir = "./dist", templates = [], context = {}, contextFiles = [], renderAllFromDir = false, customConfig = {}, } = options; const config = Object.assign(Object.assign({}, nunjucks_config_js_1.default), customConfig); nunjucks_1.default.configure(templatesDir, config); // Load and merge all JSON context files const mergedContext = yield this.mergeContextFiles(context, contextFiles); try { if (renderAllFromDir) { this.logInfo(`Auto-rendering all templates from ${templatesDir}...`); const templateFiles = yield (0, glob_1.glob)("**/*.jinja", { cwd: templatesDir, }); for (const templateRelPath of templateFiles) { const outputFile = path_1.default.join(outputDir, templateRelPath.replace(/\.jinja$/, "")); yield this.renderTemplate(templateRelPath, outputFile, mergedContext, templatesDir); } } else { if (!Array.isArray(templates) || templates.length === 0) { throw new Error("Option 'templates' must be provided if 'renderAllFromDir' is false."); } for (const { template, outputFile } of templates) { yield this.renderTemplate(template, outputFile, mergedContext, templatesDir); } } this.logInfo("✓ All templates rendered successfully."); } catch (error) { this.logError("Error rendering templates.", error); throw error; } }); } renderTemplate(template, outputFile, context, templatesDir) { return __awaiter(this, void 0, void 0, function* () { this.logInfo(`Rendering: ${template}${outputFile}`); const content = nunjucks_1.default.render(template, context); const dir = path_1.default.dirname(outputFile); yield (0, promises_1.mkdir)(dir, { recursive: true }); yield (0, promises_1.writeFile)(outputFile, content, "utf-8"); this.logInfo(`✓ Rendered: ${outputFile}`); }); } mergeContextFiles(baseContext, files) { return __awaiter(this, void 0, void 0, function* () { let merged = Object.assign({}, baseContext); for (const file of files) { try { const content = yield (0, promises_1.readFile)(file, "utf-8"); const parsed = JSON.parse(content); merged = Object.assign(Object.assign({}, merged), parsed); } catch (error) { this.logWarn(`Skipping context file due to error: ${file}`); this.logError("Context file parsing failed", error); } } return merged; }); } describe() { return "Renders one or many Nunjucks templates using a shared context or context files. Supports folder-wide auto-rendering."; } } exports.TemplateRenderAction = TemplateRenderAction; exports.default = TemplateRenderAction;