-
Notifications
You must be signed in to change notification settings - Fork 0
/
c4tool.js
370 lines (318 loc) · 10.3 KB
/
c4tool.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer-core');
const program = require('commander');
const yaml = require('js-yaml');
const watch = require('recursive-watch');
const sort = require('./ordering.js');
/**
* Return the absolute path of the Chrome/Chromium executable of the system
*/
const chromePath = () => {
const possiblePaths = {
'win32': [
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
'C:\\Program Files (x86)\\Google\\Application\\chrome.exe',
],
'darwin': [
'/Applications/Chromium.app/Contents/MacOS/Chromium',
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
],
'linux': [
'/usr/bin/chromium',
'/usr/bin/chromium-browser'
]
};
return possiblePaths[process.platform].find(path => fs.existsSync(path)) || null;
};
/**
* Sort a given c4 diagram using the ordering provided by the initial one
*
* @param {string} initial The initial diagram
* @param {string} actual The actual diagram
*
* @return {string} The properly ordered diagram
*/
const sortDiagram = (initialDiagram, actualDiagram) => {
const initial = yaml.load(initialDiagram);
const actual = yaml.load(actualDiagram);
sort( // sort elements
actual, '$', 'elements',
initial, () => '$..elements[*]',
['name']
);
sort( // sort relationships
actual, '$', 'relationships',
initial, () => '$..relationships[*]',
['source', 'destination']
);
sort( // sort element's containers
actual, '$..elements[?(@.containers)]', 'containers',
initial, (element) => `$..elements[?(@.containers && @.name=="${element.name}")].containers[*]`,
['name']
);
return yaml.dump(actual);
};
/**
* Space out objects in a map
*
* @param {string} diagram
*/
const formatDiagram = (diagram) => {
const regex = /([^:])\n(\s{1,})-/g;
return diagram.replace(regex, '$1\n\n$2-');
}
/**
* Puppeteer options used to lunch a new Chrome instance
*
* @param {boolean} headless
*/
const puppeteerOptions = (headless) => {
return {
defaultViewport: null,
ignoreHTTPSErrors: true,
executablePath: chromePath(),
headless: headless,
args: ['--disable-infobars', '--no-sandbox']
};
}
/**
* Lunch a new browser
*
* @param {boolean} headless
*/
const launchBrowser = async (headless) => {
try {
const options = puppeteerOptions(headless);
return await puppeteer.launch(options);
} catch (err) {
console.error(`could not launch browser: ${err}\n${err.stack}`);
process.exit(2);
}
}
const fileContent = (filename) => {
const resolved = path.resolve(filename);
if (!fs.existsSync(resolved)) {
console.error(`${filename} was not found`);
process.exit(2);
}
return fs.readFileSync(resolved, 'utf8');
}
/**
* Ensure that a given yaml string starts with a document separator.
* Otherwize, it won't load on Structurizr Express.
*
* @param {string} yamlString
*
* @return {string}
*
*/
const prepareYaml = (yamlString) => {
const sepLoc = yamlString.indexOf('---');
return sepLoc >= 0 ? yamlString.substring(sepLoc) : `---\n${yamlString}`;
}
const removeEmptyElements = (obj) => {
var isArray = obj instanceof Array;
for (var k in obj) {
if (obj[k] === null || obj[k] === '') isArray ? obj.splice(k, 1) : delete obj[k];
else if (typeof obj[k] == "object") removeEmptyElements(obj[k]);
if (isArray && obj.length == k) removeEmptyElements(obj);
}
return obj;
};
/**
* Remove empty properties from in a YAML string
*
* @param {string} yamlString
*
* @return {string} A YAML string without empty properties
*/
const cleanupYaml = (yamlString) => {
const cleanedObject = removeEmptyElements(yaml.load(yamlString));
return yaml.dump(cleanedObject);
};
/**
* Load Structurizr Express in a page
*
* @param {puppeteer.browser} browser
*
* @return {puppeteer.page}
*/
const structurizrExpressPage = async (browser) => {
const url = 'https://structurizr.com/express';
const page = await browser.newPage();
const disableExpressIntroduction = {
name: 'structurizr.hideExpressIntroduction',
value: 'true',
domain: 'structurizr.com'
};
await page.setCookie(disableExpressIntroduction);
await page.goto(url);
await page.waitForXPath("//*[name()='svg']");
return page;
};
/**
* Load a YAML string into Structurizr Express
*
* @param {puppeteer.page} page
* @param {string} yamlDiagram
*/
const renderDiagramInExpress = async (page, yamlDiagram) => {
return page.evaluate(
(yamlDiagram) => structurizr.scripting.renderExpressDefinition(yamlDiagram),
yamlDiagram
);
};
/**
* Update Structurizr Express
*
* @param {puppeteer.page} page
*/
const updateStructurizrExpress = async (page) => {
return page.evaluate(() => {
window.diagramToStructurizrExpress();
window.updateExpressControls(true);
});
};
const zoomFitContent = async (page) => {
await page.evaluate(() => {
Structurizr.diagram.zoomFitContent();
});
}
/**
* CLI action used to edit a givan diagram in Structurizr Express
*
* @param {string} filename A diagram file name
*/
const editDiagram = async (filename) => {
var initialDiagram = prepareYaml(fileContent(filename));
const browser = await launchBrowser(false);
const page = await structurizrExpressPage(browser);
await renderDiagramInExpress(page, initialDiagram);
await updateStructurizrExpress(page);
// Save edited diagram to the local file
await page.exposeFunction('sendDiagram', async (updatedDiagram) => {
const schemaPath = path.resolve(filename);
var updatedDiagram = cleanupYaml(updatedDiagram);
updatedDiagram = sortDiagram(initialDiagram, updatedDiagram);
updatedDiagram = formatDiagram(updatedDiagram);
fs.writeFileSync(schemaPath, updatedDiagram);
console.log(`saved diagram ${schemaPath} from Scructurizr Express`);
await exportDiagramToPNG(page, schemaPath);
})
// Create a save diagram button
await page.evaluate(async () => {
let saveToFileButton = document.createElement('button');
saveToFileButton.setAttribute('class', 'btn btn-default btn-primary');
saveToFileButton.setAttribute('id', 'saveToFile');
saveToFileButton.innerHTML = '<img src="/static/glyphicons/glyphicons-basic-199-save.svg" class="glyphicon-btn glyphicon-white"> Save to file';
const saveButton = document.querySelector('button#saveStructurizrExpressButton');
saveButton.parentElement.appendChild(saveToFileButton);
saveButton.parentElement.removeChild(saveButton);
saveToFileButton.addEventListener('click', async () => {
window.diagramToStructurizrExpress();
window.sendDiagram(window.toYamlString());
});
});
watch(filename, async (filename) => {
const date = (new Date).toString();
const yamlDiagram = prepareYaml(fileContent(filename));
await renderDiagramInExpress(page, yamlDiagram);
await updateStructurizrExpress(page);
await zoomFitContent(page);
initialDiagram = yamlDiagram;
console.log(`${date} re-rendering diagram in Structurizr Express`);
});
console.log('To exit, press Ctrl+C or close your browser');
};
/**
* Export the a given diagram in PNG
*
* @param {puppeteer.page} page
* @param {string} filename
*/
const exportDiagramToPNG = async (page, filename) => {
const pathInfo = path.parse(filename);
const renderedPath = path.join(pathInfo.dir, pathInfo.name + '.png');
const base64DataForDiagram = await page.evaluate(() => structurizr.scripting.exportCurrentDiagramToPNG());
fs.writeFile(renderedPath, base64DataForDiagram.replace(/^data:image\/png;base64,/, ""), 'base64', (err) => {
if (err) throw err;
console.log(`${filename} rendered.`);
});
}
/**
* CLI action used to render a diagram
*
* @param {string} filename A YAML diagram file name
*/
const renderDiagram = async (filename) => {
const browser = await launchBrowser(true);
const page = await structurizrExpressPage(browser);
const yamlDiagram = prepareYaml(fileContent(filename));
await renderDiagramInExpress(page, yamlDiagram);
await exportDiagramToPNG(page, path.resolve(filename));
browser.close();
};
/**
* Check is a given filename correspond to a YAML file
*
* @param {string} filename
*/
const isAYamlFile = (filename) => {
return /ya?ml$/.test(event.file);
};
/**
* Check is a given filename is a directory
*
* @param {string} directory
*/
const withDirectory = (directory) => {
const resolved = path.resolve(directory)
try {
const stat = fs.statSync(resolved);
} catch (err) {
console.error(`could not find directory: ${err}\n${err.stack}`);
process.exit(2);
}
return resolved;
}
/**
* CLI action used to watch a given directory which contains diagrams
*
* @param {string} directory
*/
const watchDiagrams = async (directory) => {
const browser = await launchBrowser(true);
const page = await structurizrExpressPage(browser);
const target = withDirectory(directory);
console.log(`watching directory ${target}`);
watch(target, async (filename) => {
if (/ya?ml$/.test(filename)) {
const yamlDiagram = prepareYaml(fileContent(filename));
await renderDiagramInExpress(page, yamlDiagram);
await exportDiagramToPNG(page, filename);
}
});
};
/**
* CLI configurations
*/
const run = () => {
program
.command('edit <filename>')
.description('edit a diagram inside structurizr express!')
.action(editDiagram);
program
.command('render <filename>')
.description('render a given diagram into PNG')
.action(renderDiagram);
program
.command('watch <directory>')
.description('watch diagrams within a directory and generate PNG when modified')
.action(watchDiagrams);
program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}
};
run();