forked from structurizr/puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit-and-export-private-diagrams.js
150 lines (115 loc) · 4.53 KB
/
edit-and-export-private-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');
if (process.argv.length < 6) {
console.log("Please specify a username, password, output format (PNG or SVG), workspace ID, and optional diagram key.");
console.log("Usage: <username> <password> <png|svg> <workspaceId> [diagramKey]")
process.exit(1);
}
const username = process.argv[2];
const password = process.argv[3];
const format = process.argv[4];
if (format !== 'png' && format !== 'svg') {
console.log("The output format must be png or svg.");
process.exit(1);
}
const workspaceId = process.argv[5];
if (!new RegExp('^[0-9]+$').test(workspaceId)) {
console.log("The workspace ID must be a non-negative integer.");
process.exit(1);
} else {
console.log("Opening diagram in workspace " + workspaceId + "...");
}
var diagramKey;
if (process.argv[6] !== '*') {
diagramKey = process.argv[6];
}
const structurizrUrl = 'https://structurizr.com';
const url = structurizrUrl + '/workspace/' + workspaceId + '/diagram-editor' + (diagramKey ? '#' + diagramKey : '');
const filenameSuffix = 'structurizr-' + workspaceId + '-';
(async () => {
const browser = await puppeteer.launch({ignoreHTTPSErrors: false, headless: false});
const page = await browser.newPage();
await page.setCookie({
"name": "structurizr.hideDiagramEditorIntroduction",
"value": "true",
"url": structurizrUrl
})
await page.goto(structurizrUrl + '/dashboard');
await page.type('#username', username);
await page.type('#password', password);
await page.click('button[type="submit"]');
await page.waitForSelector('p.dashboardMetaData');
await page.goto(url);
await page.waitForXPath("//*[name()='svg']");
var workspaceSaved = async function(){
console.log("Workspace saved; creating PNG/SVG files for all views in the workspace...");
var diagramKey = await page.evaluate(() => {
return structurizr.scripting.getViewKey();
});
const views = await page.evaluate(() => {
return structurizr.scripting.getViews();
});
for (var i = 0; i < views.length; i++) {
var view = views[i];
await page.evaluate((view) => {
structurizr.scripting.changeView(view.key);
}, view);
await page.waitForXPath("//*[name()='svg']");
if (format === 'png') {
await exportDiagramAndKeyToPNG(page, view.key);
} else {
await exportDiagramAndKeyToSVG(page, view.key);
}
}
// and navigate back to previous view
await page.evaluate((diagramKey) => {
structurizr.scripting.changeView(diagramKey);
}, diagramKey);
}
await page.exposeFunction('workspaceSaved', workspaceSaved);
await page.evaluate(() => {
return structurizr.scripting.onWorkspaceSaved(workspaceSaved);
});
})();
async function exportDiagramAndKeyToPNG(page) {
var diagramKey = await page.evaluate(() => {
return structurizr.scripting.getViewKey();
});
const diagramFilename = filenameSuffix + diagramKey + '.png';
const diagramKeyFilename = filenameSuffix + diagramKey + '-key.png'
console.log(' - ' + diagramKey + ' (diagram) -> ' + diagramFilename);
var base64DataForDiagram = await page.evaluate(() => {
return structurizr.scripting.exportCurrentDiagramToPNG();
});
fs.writeFile(diagramFilename, base64DataForDiagram.replace(/^data:image\/png;base64,/, ""), 'base64', function (err) {
if (err) throw err;
});
console.log(' - ' + diagramKey + ' (key) -> ' + diagramKeyFilename);
var base64DataForKey = await page.evaluate(() => {
return structurizr.scripting.exportCurrentDiagramKeyToPNG(false);
});
fs.writeFile(diagramKeyFilename, base64DataForKey.replace(/^data:image\/png;base64,/, ""), 'base64', function (err) {
if (err) throw err;
});
}
async function exportDiagramAndKeyToSVG(page) {
var diagramKey = await page.evaluate(() => {
return structurizr.scripting.getViewKey();
});
const diagramFilename = filenameSuffix + diagramKey + '.html';
const diagramKeyFilename = filenameSuffix + diagramKey + '-key.html'
console.log(' - ' + diagramKey + ' (diagram) -> ' + diagramFilename);
var svgForDiagram = await page.evaluate(() => {
return structurizr.scripting.exportCurrentDiagramToSVG();
});
fs.writeFile(diagramFilename, svgForDiagram, function (err) {
if (err) throw err;
});
console.log(' - ' + diagramKey + ' (key) -> ' + diagramKeyFilename);
var svgForKey = await page.evaluate(() => {
return structurizr.scripting.exportCurrentDiagramKeyToSVG();
});
fs.writeFile(diagramKeyFilename, svgForKey, function (err) {
if (err) throw err;
});
}