-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate-sequence-diagram.js
executable file
·82 lines (64 loc) · 2.09 KB
/
generate-sequence-diagram.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
#!/usr/bin/env node
const path = require('path');
var argv = require('yargs')
.usage('Generate a sequence diagram from a diagram DSL file and output it as a png image with a given name. \nUsage: $0')
.example('$0 -f diagram-file.txt -o output-file.png -t [snapHand|snapSimple]')
.demand('f')
.alias('f', 'input')
.describe('f', 'Input filename with the diagram DSL')
.demand('o')
.alias('o', 'output')
.describe('o', 'Output filename. Could be either jpeg, png or pdf. Defaults to "png".')
.default('o', 'out.png')
.alias('t', 'theme')
.describe('t', 'Diagram theme.')
.choices('t', ['snapHand', 'snapSimple'])
.default('t', 'snapHand')
.argv;
var fs = require('fs');
const puppeteer = require('puppeteer');
var Handlebars = require('handlebars');
var templateSrc = fs.readFileSync('./templates/sequence-diagram.handlebars', 'utf8');
var template = Handlebars.compile(templateSrc);
var sequenceDiagram;
var sequenceDiagramTheme = argv.t;
var tempFile = ".tmp.html";
const pdfRegex = /pdf$/g;
const imageRegex = /(png|jpg|jpeg)$/g;
sequenceDiagram = fs.readFileSync(argv.f, 'utf8');
var handlebarsContext = {
"sequenceDiagram": sequenceDiagram,
"theme": sequenceDiagramTheme
};
var sequenceDiagramOutput = template(handlebarsContext);
fs.writeFileSync(tempFile, sequenceDiagramOutput);
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`file://${path.resolve('.tmp.html')}`, {
waitUntil: 'networkidle2'
});
const executionContext = await page.mainFrame().executionContext()
const result = await executionContext.evaluate(
() => {
var width = Math.ceil($( "#diagram svg" ).width());
var height = Math.ceil($( "#diagram svg" ).height());
return {
width, height
}
}
)
page.setViewport(result);
if(pdfRegex.test(argv.o)){
await page.pdf({
path: argv.o
});
} else if (imageRegex.test(argv.o)) {
await page.screenshot({
path: argv.o
});
} else {
console.error(`File format for file ${argv.o} is not supported.`);
}
browser.close();
})();