forked from structurizr/puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-diagrams-lite.js
124 lines (96 loc) · 3.63 KB
/
export-diagrams-lite.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const puppeteer = require('puppeteer');
const fs = require('fs');
if (process.argv.length < 4) {
console.log("Please specify a Structurizr URL, output format (PNG or SVG), and optional diagram key.");
console.log("Usage: <structurizrUrl> <png|svg> [diagram key]")
process.exit(1);
}
const structurizrUrl = process.argv[2];
const format = process.argv[3];
if (format !== 'png' && format !== 'svg') {
console.log("The output format must be png or svg.");
process.exit(1);
}
var browser;
var diagramKeys = [];
var expectedNumberOfExports = 0;
var actualNumberOfExports = 0;
const url = structurizrUrl + '/workspace/diagrams';
const filenameSuffix = 'structurizr-';
(async () => {
browser = await puppeteer.launch({ignoreHTTPSErrors: true, headless: true});
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'domcontentloaded' });
await page.exposeFunction('savePNG', (content, filename) => {
console.log("Writing " + filename);
content = content.replace(/^data:image\/png;base64,/, "");
fs.writeFile(filename, content, 'base64', function (err) {
if (err) throw err;
});
actualNumberOfExports++;
if (actualNumberOfExports === expectedNumberOfExports) {
browser.close();
}
});
await page.waitForFunction('structurizr.scripting && structurizr.scripting.isDiagramRendered() === true');
// figure out which views should be exported
if (process.argv[4] !== undefined) {
console.log(process.argv[4]);
diagramKeys.push(process.argv[4]);
expectedNumberOfExports++;
} else {
const views = await page.evaluate(() => {
return structurizr.scripting.getViews();
});
views.forEach(function(view) {
diagramKeys.push(view.key);
expectedNumberOfExports++;
});
}
// every diagram has a key/legend
expectedNumberOfExports = (expectedNumberOfExports * 2);
for (var i = 0; i < diagramKeys.length; i++) {
var diagramKey = diagramKeys[i];
await page.evaluate((diagramKey) => {
structurizr.scripting.changeView(diagramKey);
}, diagramKey);
await page.waitForFunction('structurizr.scripting.isDiagramRendered() === true');
if (format === "svg") {
const diagramFilename = filenameSuffix + diagramKey + '.svg';
const diagramKeyFilename = filenameSuffix + diagramKey + '-key.svg'
var svgForDiagram = await page.evaluate(() => {
return structurizr.scripting.exportCurrentDiagramToSVG();
});
console.log("Writing " + diagramFilename);
fs.writeFile(diagramFilename, svgForDiagram, function (err) {
if (err) throw err;
});
actualNumberOfExports++;
var svgForKey = await page.evaluate(() => {
return structurizr.scripting.exportCurrentDiagramKeyToSVG();
});
console.log("Writing " + diagramKeyFilename);
fs.writeFile(diagramKeyFilename, svgForKey, function (err) {
if (err) throw err;
});
actualNumberOfExports++;
} else {
const diagramFilename = filenameSuffix + diagramKey + '.png';
const diagramKeyFilename = filenameSuffix + diagramKey + '-key.png'
page.evaluate((diagramFilename) => {
structurizr.scripting.exportCurrentDiagramToPNG({ crop: false }, function(png) {
window.savePNG(png, diagramFilename);
})
}, diagramFilename);
page.evaluate((diagramKeyFilename) => {
structurizr.scripting.exportCurrentDiagramKeyToPNG(function(png) {
window.savePNG(png, diagramKeyFilename);
})
}, diagramKeyFilename);
}
}
if (actualNumberOfExports === expectedNumberOfExports) {
console.log('bye');
browser.close();
}
})();