forked from naddison36/tx2uml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplantuml.js
66 lines (66 loc) · 2.39 KB
/
plantuml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genPumlJavaOptions = exports.streamPlantUml = exports.outputFormats = void 0;
const child_process_1 = require("child_process");
const path_1 = __importDefault(require("path"));
exports.outputFormats = ["png", "svg", "eps"];
const streamPlantUml = async (pumlStream, outputStream, options = {}) => {
const pumlJavaOptions = (0, exports.genPumlJavaOptions)(options);
return pipePuml(pumlStream, outputStream, pumlJavaOptions);
};
exports.streamPlantUml = streamPlantUml;
const genPumlJavaOptions = (options = {}) => {
const plantUmlOptions = [
"-jar",
path_1.default.join(__dirname, "./plantuml.jar"),
"-Djava.awt.headless=true",
];
if (options?.format) {
if (!exports.outputFormats.includes(options.format)) {
throw new Error(`Invalid format ${options.format}. Must be either: ${JSON.stringify(exports.outputFormats)}`);
}
plantUmlOptions.push("-t" + options.format);
}
if (options.limitSize) {
plantUmlOptions.push("-DPLANTUML_LIMIT_SIZE=" + options.limitSize);
}
if (options.config) {
plantUmlOptions.push("-config", options.config);
}
if (options.pipemap) {
plantUmlOptions.push("-pipemap");
}
else {
plantUmlOptions.push("-pipe");
}
return plantUmlOptions;
};
exports.genPumlJavaOptions = genPumlJavaOptions;
const pipePuml = (pumlStream, outputStream, plantUmlOptions) => {
return new Promise(async (resolve, reject) => {
const child = (0, child_process_1.spawn)("java", plantUmlOptions);
pumlStream.pipe(child.stdin);
child.stdout.pipe(outputStream);
let error = "";
for await (const chunk of child.stderr) {
error += chunk;
}
child.once("exit", code => {
if (code === 0) {
resolve(code);
}
else {
reject(new Error(`PlantUML process existed with status code ${code}. ${error}`));
}
});
child.once("error", err => {
reject(new Error(`PlantUML process failed with error: ${error}`, {
cause: err,
}));
});
});
};
//# sourceMappingURL=plantuml.js.map