-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph-generator.js
75 lines (67 loc) · 2.29 KB
/
graph-generator.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
const Project = require('@lerna/project');
const PackageGraph = require('@lerna/package-graph');
const plantuml = require('node-plantuml');
function generatePlantUml(graph) {
const chartText = [];
const packNames = [];
graph.forEach(pack => {
packNames.push(pack.name.replace(/-/g, '_'));
pack.localDependencies.forEach(dep => {
chartText.push(`${pack.name.replace(/-/g, '_')} --> ${dep.name.replace(/-/g, '_')}`);
})
});
return [
'@startuml',
'skinparam linetype ortho',
'skinparam monochrome true',
'skinparam shadowing false',
'skinparam nodeFontSize 15',
packNames.map(pack => `node ${pack}`).join('\n'),
chartText.join(`\n`),
'@enduml'
].join('\n').replace(/@[\w\-]+\//g, '');
}
async function getGraphFromLernaPath(lernaPath) {
const project = new Project(lernaPath);
const packages = await project.getPackages();
return new PackageGraph(packages);
}
function createPngBufferFromPlantUml(uml) {
plantuml.useNailgun();
return new Promise((resolve, reject) => {
plantuml.generate(uml, {}, function (err, buffer) {
if (!buffer.length) reject(new Error('Buffer length is zero. Verify java if java is installed.'))
else resolve(buffer);
});
});
}
function createTgfBufferFromGraph(graph) {
const nodes = [];
const edges = [];
graph.forEach(pack => {
nodes.push(pack.name);
});
graph.forEach(pack => {
pack.localDependencies.forEach(dep => {
edges.push(`${nodes.indexOf(pack.name)} ${nodes.indexOf(dep.name)}`);
});
});
const str = nodes
.map((node, index) => `${index} ${node}`)
.join('\n') + '\n#\n' + edges.join('\n');
return Buffer.from(str, 'utf-8');
}
const generateGraphBufferLernaPath = async (lernaPath, format = 'png') => {
const graph = await getGraphFromLernaPath(lernaPath);
if (format === 'png') {
const uml = generatePlantUml(graph);
return createPngBufferFromPlantUml(uml);
} else if (format === 'tgf') {
return createTgfBufferFromGraph(graph);
} else if (format === 'puml') {
return Buffer.from(generatePlantUml(graph), 'utf-8');
}
};
module.exports = {
generateGraphBufferLernaPath,
};