UNPKG

ngrx-uml

Version:
110 lines (106 loc) 4.51 kB
"use strict"; 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.PlantUmlOutputService = void 0; const chalk_1 = __importDefault(require("chalk")); const fs_1 = require("fs"); const http_1 = __importDefault(require("http")); const loglevel_1 = __importDefault(require("loglevel")); const path_1 = __importDefault(require("path")); const plantuml_encoder_1 = require("plantuml-encoder"); const utils_1 = require("../../utils"); const defaultOptions = { plantUmlServerUrl: 'www.plantuml.com', remotePathPrefix: '/plantuml/' }; class PlantUmlOutputService { constructor(options) { this.options = Object.assign(Object.assign({}, defaultOptions), options); } transform(inputs) { return __awaiter(this, void 0, void 0, function* () { for (const input of inputs) { yield this.transformFromString(input.name, input.result); } }); } transformFromString(name, input) { return __awaiter(this, void 0, void 0, function* () { const diagram = this.createDiagram(name, input); const fileBaseName = utils_1.removeiIlegalCharacters(name, this.options.clickableLinks); this.writeWsdToFile(diagram, this.options.outDir, fileBaseName); if (this.options.ext !== 'off') { return this.renderToImageFile(diagram, this.options.outDir, fileBaseName, this.options.ext); } }); } // #region IMAGE renderToImageFile(input, outDir, fileName, ext) { if (!fs_1.existsSync(outDir)) { fs_1.mkdirSync(outDir, { recursive: true }); } const writeStream = this.createWriteStream(outDir, fileName, ext); return this.renderImage(ext, input, writeStream); } renderImage(extension, plantuml, resultStream) { const encodedPlantuml = plantuml_encoder_1.encode(plantuml); const remotePath = `${this.options.remotePathPrefix}${extension}/${encodedPlantuml}`; return new Promise((resolve, reject) => { http_1.default.get({ host: this.options.plantUmlServerUrl, path: remotePath }, (res) => { res.pipe(resultStream); resultStream.on('close', () => { resolve(); }); res.on('error', (err) => { loglevel_1.default.warn('mapToPlantUml', `problem with request ${err.message}`); reject(err); }); }); }); } createWriteStream(outDir, fileName, extension) { const filePath = path_1.default.format({ dir: outDir, name: fileName, ext: '.' + extension }); const fileStream = fs_1.createWriteStream(filePath); fileStream.once('close', () => { loglevel_1.default.info(`Diagram image saved: ${chalk_1.default.cyan(filePath)} `); }); return fileStream; } // #endregion // #region WSD createDiagram(name, diagramContent) { return `@startuml ${name} set namespaceSeparator :: skinparam class { BackgroundColor<<listen>> HoneyDew BackgroundColor<<action>> Wheat BackgroundColor<<dispatch>> Technology } ${diagramContent} @enduml`; } writeWsdToFile(diagram, outDir, name) { if (this.options.saveWsd) { const fileName = name + '.wsd'; const filePath = utils_1.writeToFile(diagram, path_1.default.join(outDir, 'wsd'), fileName); loglevel_1.default.info(`Wsd saved to: ${chalk_1.default.gray(filePath)}`); } } } exports.PlantUmlOutputService = PlantUmlOutputService;