]>
git.r.bdr.sh - rbdr/graphviz-to-grafn/blob - lib/graphviz_to_grafn.js
e001e12cbed83ab519c41024bbbcd7fb7968e2ed
3 const DotParser
= require('dotparser');
7 // Extract vertices and dependencies
9 parseDotObject(entries
) {
11 const vertices
= new Set();
12 const dependencies
= {};
14 entries
.forEach((entry
) => {
17 vertices
.add(entry
.node_id
.id
);
20 if (entry
.edge_list
) {
21 vertices
.add(entry
.edge_list
[0].id
);
22 vertices
.add(entry
.edge_list
[1].id
);
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
);
36 // Generate grafn code from a list of vertices and dependencies
38 generateCode(vertices
, dependencies
) {
40 const code
= ['const Grafn = require(\'grafn\')', 'const graph = new Grafn()'];
42 for (const vertex
of vertices
) {
43 const vertexDependencies
= Array
.from(dependencies
[vertex
] || []);
45 code
.push('graph.vertex({');
46 code
.push(` name: '${vertex}',`);
47 code
.push(` dependencies: ${JSON.stringify(vertexDependencies).replace(/"/g, '\'')},`);
48 code
.push(' action: async () => {}');
52 return code
.join('\n');
56 // Take some dot source code... make it grafn source code.
58 module
.exports
= function graphvizToGrafn(dotSource
) {
60 const dotObject
= DotParser(dotSource
);
61 const { vertices
, dependencies
} = internals
.parseDotObject(dotObject
[0].children
);
63 return internals
.generateCode(vertices
, dependencies
);