-
Notifications
You must be signed in to change notification settings - Fork 19
/
cli.js
executable file
·90 lines (79 loc) · 2.17 KB
/
cli.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { convertToDot, embedDotComment, embedSvgComment, trailingPngComment, parseEmbeddedComment } from "./layout.js";
import { load } from "js-yaml";
import { promises as fs } from "node:fs";
import { Buffer } from "node:buffer";
import { Graphviz } from "@hpcc-js/wasm";
import svg2img from "svg2img";
const args = process.argv.slice(2);
if (args.length != 2) {
console.log("usage: deciduous input.yaml output.svg\n or: deciduous input.yaml output.dot");
process.exit(1);
}
const graphviz = Graphviz.load();
function fileExtension(path) {
const result = path.match(/\.\w+$/)[0];
switch (result) {
case ".svg":
case ".dot":
case ".png":
case ".yaml":
return result;
default:
throw new Error(`invalid file path: ${path}; only support .svg, .dot, .png, and .yaml`);
}
}
const [inputFile, outputFile] = args;
let newInput;
switch (fileExtension(inputFile)) {
case ".yaml":
newInput = await fs.readFile(inputFile, "utf8");
break;
case ".dot":
case ".svg":
case ".png": {
const fileContents = await fs.readFile(inputFile, "latin1");
newInput = parseEmbeddedComment(fileContents);
if (newInput === undefined) {
console.error(`${inputFile} is not a compatible deciduous image.`)
process.exit(1);
}
break;
}
}
const parsed = load(newInput);
const { dot } = convertToDot(parsed);
function svgtoimgAsync(args) {
return new Promise((resolve, reject) => {
svg2img(args, function(err, buffer) {
if (err != null) {
reject(err);
} else {
resolve(buffer);
}
});
});
}
switch (fileExtension(outputFile)) {
case ".yaml": {
await fs.writeFile(outputFile, newInput);
break;
}
case ".dot": {
const branded = embedDotComment(dot, newInput);
await fs.writeFile(outputFile, branded, "utf-8");
break;
}
case ".svg": {
const svg = (await graphviz).layout(dot, "svg", "dot");
const branded = embedSvgComment(svg, newInput);
await fs.writeFile(outputFile, branded, "utf-8");
break;
}
case ".png": {
const svg = (await graphviz).layout(dot, "svg", "dot");
const png = await svgtoimgAsync(svg);
const branded = Buffer.concat([png, Buffer.from(trailingPngComment(newInput))]);
await fs.writeFile(outputFile, branded);
break;
}
}