UNPKG

@reactionable/cli

Version:
98 lines 5.24 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateService = void 0; const path_1 = require("path"); const inversify_1 = require("inversify"); const FileFactory_1 = require("../file/FileFactory"); const FileService_1 = require("../file/FileService"); const container_1 = require("./container"); const TemplateFileService_1 = require("./TemplateFileService"); let TemplateService = class TemplateService { constructor(fileService, fileFactory, templateFileService, templateAdapter) { this.fileService = fileService; this.fileFactory = fileFactory; this.templateFileService = templateFileService; this.templateAdapter = templateAdapter; } async renderTemplate(dirPath, namespace, context = {}) { this.fileService.assertDirExists(dirPath); const config = await this.getTemplateConfig(namespace, context); return this.renderTemplateFromConfig(dirPath, namespace, context, config); } async renderTemplateFile(templateKey, context) { return this.templateAdapter.renderTemplateFile(templateKey, context); } async renderTemplateFromConfig(dirPath, namespace, context, config) { if (Array.isArray(config)) { for (const filePath of config) { await this.renderTemplateFromConfig(dirPath, namespace, context, filePath); } return; } if (typeof config === "object") { for (const dir of Object.keys(config)) { const currentPath = (0, path_1.resolve)(dirPath, dir); await this.renderTemplateFromConfig(currentPath, namespace, context, config[dir]); } return; } const currentNamespace = (0, path_1.join)(namespace, config); const currentPath = (0, path_1.extname)(dirPath) ? dirPath : (0, path_1.join)(dirPath, config); const currentBaseDirPath = (0, path_1.dirname)(currentPath); if (!this.fileService.dirExistsSync(currentBaseDirPath)) { this.fileService.mkdirSync(currentBaseDirPath, true); } const templateKey = this.getTemplateKey(dirPath, currentPath, currentNamespace); await this.createFileFromTemplate(currentPath, templateKey, context); } async getTemplateConfig(namespace, context) { const templateKey = (0, path_1.join)(namespace, "config"); const templateContent = await this.templateFileService.getTemplateFileContent(templateKey); const content = await this.templateAdapter.renderTemplateString(templateContent, context); const templateConfig = JSON.parse(content); return templateConfig; } getTemplateKey(dirPath, filepath, namespace) { if ((0, path_1.extname)(namespace)) { return namespace; } const templateKey = (0, path_1.join)(namespace, filepath.replace(dirPath, "")); const templatePath = (0, path_1.join)(__dirname, "./../templates", templateKey + ".template"); if (!this.fileService.fileExistsSync(templatePath)) { throw new Error(`Template file "${templatePath}" does not exist`); } return templateKey; } async createFileFromTemplate(filePath, templateKey, context, encoding = "utf8") { const parentDir = (0, path_1.dirname)(filePath); if (!this.fileService.dirExistsSync(parentDir)) { throw new Error(`Unable to create file "${filePath}", directory "${parentDir}" does not exist`); } const fileContent = await this.templateAdapter.renderTemplateFile(templateKey, context); await this.fileFactory.fromString(fileContent, filePath, encoding).saveFile(); } }; TemplateService = __decorate([ (0, inversify_1.injectable)(), __param(0, (0, inversify_1.inject)(FileService_1.FileService)), __param(1, (0, inversify_1.inject)(FileFactory_1.FileFactory)), __param(2, (0, inversify_1.inject)(TemplateFileService_1.TemplateFileService)), __param(3, (0, inversify_1.inject)(container_1.AdapterKey)), __metadata("design:paramtypes", [FileService_1.FileService, FileFactory_1.FileFactory, TemplateFileService_1.TemplateFileService, Object]) ], TemplateService); exports.TemplateService = TemplateService; //# sourceMappingURL=TemplateService.js.map