]>
Commit | Line | Data |
---|---|---|
1 | 'use strict'; | |
2 | ||
3 | const DotParser = require('dotparser'); | |
4 | ||
5 | const internals = { | |
6 | ||
7 | // Extract vertices and dependencies | |
8 | ||
9 | parseDotObject(entries) { | |
10 | ||
11 | const vertices = new Set(); | |
12 | const dependencies = {}; | |
13 | ||
14 | entries.forEach((entry) => { | |
15 | ||
16 | if (entry.node_id) { | |
17 | vertices.add(entry.node_id.id); | |
18 | } | |
19 | ||
20 | if (entry.edge_list) { | |
21 | vertices.add(entry.edge_list[0].id); | |
22 | vertices.add(entry.edge_list[1].id); | |
23 | ||
24 | dependencies[entry.edge_list[1].id] = dependencies[entry.edge_list[1].id] || new Set(); | |
25 | dependencies[entry.edge_list[1].id].add(entry.edge_list[0].id); | |
26 | } | |
27 | ||
28 | }); | |
29 | ||
30 | return { | |
31 | vertices, | |
32 | dependencies | |
33 | }; | |
34 | }, | |
35 | ||
36 | // Generate grafn code from a list of vertices and dependencies | |
37 | ||
38 | generateCode(vertices, dependencies) { | |
39 | ||
40 | const code = ['const Grafn = require(\'grafn\')', 'const graph = new Grafn()']; | |
41 | ||
42 | for (const vertex of vertices) { | |
43 | const vertexDependencies = Array.from(dependencies[vertex] || []); | |
44 | ||
45 | code.push('graph.vertex({'); | |
46 | code.push(` name: '${vertex}',`); | |
47 | code.push(` dependencies: ${JSON.stringify(vertexDependencies).replace(/"/g, '\'')},`); | |
48 | code.push(' action: async () => {}'); | |
49 | code.push('});'); | |
50 | } | |
51 | ||
52 | return code.join('\n'); | |
53 | } | |
54 | }; | |
55 | ||
56 | // Take some dot source code... make it grafn source code. | |
57 | ||
58 | module.exports = function graphvizToGrafn(dotSource) { | |
59 | ||
60 | const dotObject = DotParser(dotSource); | |
61 | const { vertices, dependencies } = internals.parseDotObject(dotObject[0].children); | |
62 | ||
63 | return internals.generateCode(vertices, dependencies); | |
64 | }; |