-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
export-diagrams.js
150 lines (120 loc) · 4.41 KB
/
export-diagrams.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const puppeteer = require('puppeteer');
const fs = require('fs');
const PNG_FORMAT = 'png';
const SVG_FORMAT = 'svg';
const IGNORE_HTTPS_ERRORS = true;
const HEADLESS = true;
const IMAGE_VIEW_TYPE = 'Image';
if (process.argv.length < 4) {
console.log("Usage: <structurizrUrl> <png|svg> [username] [password]")
process.exit(1);
}
const url = process.argv[2];
const format = process.argv[3];
if (format !== PNG_FORMAT && format !== SVG_FORMAT) {
console.log("The output format must be ' + PNG_FORMAT + ' or ' + SVG_FORMAT + '.");
process.exit(1);
}
var username;
var password;
if (process.argv.length > 3) {
username = process.argv[4];
password = process.argv[5];
}
var expectedNumberOfExports = 0;
var actualNumberOfExports = 0;
(async () => {
const browser = await puppeteer.launch({ignoreHTTPSErrors: IGNORE_HTTPS_ERRORS, headless: HEADLESS});
const page = await browser.newPage();
if (username !== undefined && password !== undefined) {
// sign in
const parts = url.split('://');
const signinUrl = parts[0] + '://' + parts[1].substring(0, parts[1].indexOf('/')) + '/dashboard';
console.log(' - Signing in via ' + signinUrl);
await page.goto(signinUrl, { waitUntil: 'networkidle2' });
await page.type('#username', username);
await page.type('#password', password);
await page.keyboard.press('Enter');
await page.waitForSelector('div#dashboard');
}
// visit the diagrams page
console.log(" - Opening " + url);
await page.goto(url, { waitUntil: 'domcontentloaded' });
await page.waitForFunction('structurizr.scripting && structurizr.scripting.isDiagramRendered() === true');
if (format === PNG_FORMAT) {
// add a function to the page to save the generated PNG images
await page.exposeFunction('savePNG', (content, filename) => {
console.log(" - " + filename);
content = content.replace(/^data:image\/png;base64,/, "");
fs.writeFile(filename, content, 'base64', function (err) {
if (err) throw err;
});
actualNumberOfExports++;
if (actualNumberOfExports === expectedNumberOfExports) {
console.log(" - Finished");
browser.close();
}
});
}
// get the array of views
const views = await page.evaluate(() => {
return structurizr.scripting.getViews();
});
views.forEach(function(view) {
if (view.type === IMAGE_VIEW_TYPE) {
expectedNumberOfExports++; // diagram only
} else {
expectedNumberOfExports++; // diagram
expectedNumberOfExports++; // key
}
});
console.log(" - Starting export");
for (var i = 0; i < views.length; i++) {
const view = views[i];
await page.evaluate((view) => {
structurizr.scripting.changeView(view.key);
}, view);
await page.waitForFunction('structurizr.scripting.isDiagramRendered() === true');
if (format === SVG_FORMAT) {
const diagramFilename = view.key + '.svg';
const diagramKeyFilename = view.key + '-key.svg'
var svgForDiagram = await page.evaluate(() => {
return structurizr.scripting.exportCurrentDiagramToSVG({ includeMetadata: true });
});
console.log(" - " + diagramFilename);
fs.writeFile(diagramFilename, svgForDiagram, function (err) {
if (err) throw err;
});
actualNumberOfExports++;
if (view.type !== IMAGE_VIEW_TYPE) {
var svgForKey = await page.evaluate(() => {
return structurizr.scripting.exportCurrentDiagramKeyToSVG();
});
console.log(" - " + diagramKeyFilename);
fs.writeFile(diagramKeyFilename, svgForKey, function (err) {
if (err) throw err;
});
actualNumberOfExports++;
}
if (actualNumberOfExports === expectedNumberOfExports) {
console.log(" - Finished");
browser.close();
}
} else {
const diagramFilename = view.key + '.png';
const diagramKeyFilename = view.key + '-key.png'
page.evaluate((diagramFilename) => {
structurizr.scripting.exportCurrentDiagramToPNG({ includeMetadata: true, crop: false }, function(png) {
window.savePNG(png, diagramFilename);
})
}, diagramFilename);
if (view.type !== IMAGE_VIEW_TYPE) {
page.evaluate((diagramKeyFilename) => {
structurizr.scripting.exportCurrentDiagramKeyToPNG(function(png) {
window.savePNG(png, diagramKeyFilename);
})
}, diagramKeyFilename);
}
}
}
})();