UNPKG

mume-with-litvis

Version:

Fork of mume with added http://litvis.org/

124 lines 4.69 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.render = void 0; const child_process_1 = require("child_process"); const path = require("path"); const mume_1 = require("./mume"); const puml_server_1 = require("./puml-server"); const utility_1 = require("./utility"); const PlantUMLJarPath = path.resolve(utility_1.extensionDirectoryPath, "./dependencies/plantuml/plantuml.jar"); /** * key is fileDirectoryPath, value is PlantUMLTask */ const TASKS = {}; /** * key is fileDirectoryPath, value is String */ const CHUNKS = {}; /** * key is fileDirectoryPath, value is Array */ const CALLBACKS = {}; class PlantUMLTask { constructor(fileDirectoryPath) { this.fileDirectoryPath = fileDirectoryPath; this.chunks = CHUNKS[this.fileDirectoryPath] || ""; this.callbacks = CALLBACKS[this.fileDirectoryPath] || []; this.task = null; this.startTask(); } generateSVG(content) { return new Promise((resolve, reject) => { this.callbacks.push(resolve); this.task.stdin.write(content + "\n"); }); } startTask() { this.task = (0, child_process_1.spawn)("java", [ "-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Dplantuml.include.path=" + [this.fileDirectoryPath, (0, mume_1.getExtensionConfigPath)()].join(path.delimiter), "-jar", PlantUMLJarPath, // '-graphvizdot', 'exe' "-pipe", "-tsvg", "-charset", "UTF-8", ]); this.task.stdout.on("data", (chunk) => { let data = chunk.toString(); this.chunks += data; if (this.chunks.trimRight().endsWith("</svg>") && this.chunks.match(/<svg/g).length === this.chunks.match(/<\/svg>/g).length) { data = this.chunks; this.chunks = ""; // clear CHUNKS const diagrams = data.split("<?xml "); diagrams.forEach((diagram) => { if (diagram.length) { const callback = this.callbacks.shift(); if (callback) { callback(diagram.startsWith("<") ? diagram : "<?xml " + diagram); } } }); } }); this.task.on("error", () => this.closeSelf()); this.task.on("exit", () => this.closeSelf()); } /** * stop this.task and store this.chunks and this.callbacks */ closeSelf() { TASKS[this.fileDirectoryPath] = null; CHUNKS[this.fileDirectoryPath] = this.chunks; CALLBACKS[this.fileDirectoryPath] = this.callbacks; } } // async call function render(content, fileDirectoryPath = "", serverURL = "") { return __awaiter(this, void 0, void 0, function* () { content = content.trim(); // ' @mume_file_directory_path:/fileDirectoryPath // fileDirectoryPath const match = content.match(/^'\s@mume_file_directory_path:(.+)$/m); if (match) { fileDirectoryPath = match[1]; } const startMatch = content.match(/^\@start(.+?)\s+/m); if (startMatch) { if (!content.match(new RegExp(`^\\@end${startMatch[1]}`, "m"))) { content = "@startuml\n@enduml"; // error } } else { content = `@startuml ${content} @enduml`; } if (!TASKS[fileDirectoryPath]) { if (!!serverURL) { TASKS[fileDirectoryPath] = new puml_server_1.default(serverURL); } else { // init `plantuml.jar` task TASKS[fileDirectoryPath] = new PlantUMLTask(fileDirectoryPath); } } return yield TASKS[fileDirectoryPath].generateSVG(content); }); } exports.render = render; //# sourceMappingURL=puml.js.map