-
Notifications
You must be signed in to change notification settings - Fork 319
/
graph.js
168 lines (147 loc) · 4.23 KB
/
graph.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
'use strict';
const path = require('path');
const {promisify} = require('util');
const gv = require('ts-graphviz');
const adapter = require('ts-graphviz/adapter');
const toArray = require('stream-to-array');
const exec = promisify(require('child_process').execFile);
const writeFile = promisify(require('fs').writeFile);
/**
* Set color on a node.
* @param {Object} node
* @param {String} color
*/
function setNodeColor(node, color) {
node.attributes.set('color', color);
node.attributes.set('fontcolor', color);
}
/**
* Check if Graphviz is installed on the system.
* @param {Object} config
* @return {Promise}
*/
async function checkGraphvizInstalled(config) {
const cmd = config.graphVizPath ? path.join(config.graphVizPath, 'gvpr') : 'gvpr';
try {
await exec(cmd, ['-V']);
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error(`Graphviz could not be found. Ensure that "gvpr" is in your $PATH. ${err}`);
} else {
throw new Error(`Unexpected error when calling Graphviz "${cmd}". ${err}`);
}
}
}
/**
* Return options to use with graphviz digraph.
* @param {Object} config
* @return {Object}
*/
function createGraphvizOptions(config) {
const graphVizOptions = config.graphVizOptions || {};
return {
dotCommand: config.graphVizPath ? config.graphVizPath : null,
attributes: {
// Graph
graph: Object.assign({
overlap: false,
pad: 0.3,
rankdir: config.rankdir,
layout: config.layout,
bgcolor: config.backgroundColor
}, graphVizOptions.G),
// Edge
edge: Object.assign({
color: config.edgeColor
}, graphVizOptions.E),
// Node
node: Object.assign({
fontname: config.fontName,
fontsize: config.fontSize,
color: config.nodeColor,
shape: config.nodeShape,
style: config.nodeStyle,
height: 0,
fontcolor: config.nodeColor
}, graphVizOptions.N)
}
};
}
/**
* Creates the graphviz graph.
* @param {Object} modules
* @param {Array} circular
* @param {Object} config
* @param {Object} options
* @return {Promise}
*/
function createGraph(modules, circular, config, options) {
const g = gv.digraph('G');
const nodes = {};
const cyclicModules = circular.reduce((a, b) => a.concat(b), []);
Object.keys(modules).forEach((id) => {
nodes[id] = nodes[id] || g.createNode(id);
if (!modules[id].length) {
setNodeColor(nodes[id], config.noDependencyColor);
} else if (cyclicModules.indexOf(id) >= 0) {
setNodeColor(nodes[id], config.cyclicNodeColor);
}
modules[id].forEach((depId) => {
nodes[depId] = nodes[depId] || g.createNode(depId);
if (!modules[depId]) {
setNodeColor(nodes[depId], config.noDependencyColor);
}
g.createEdge([nodes[id], nodes[depId]]);
});
});
const dot = gv.toDot(g);
return adapter
.toStream(dot, options)
.then(toArray)
.then(Buffer.concat);
}
/**
* Return the module dependency graph XML SVG representation as a Buffer.
* @param {Object} modules
* @param {Array} circular
* @param {Object} config
* @return {Promise}
*/
module.exports.svg = function (modules, circular, config) {
const options = createGraphvizOptions(config);
options.format = 'svg';
return checkGraphvizInstalled(config)
.then(() => createGraph(modules, circular, config, options));
};
/**
* Creates an image from the module dependency graph.
* @param {Object} modules
* @param {Array} circular
* @param {String} imagePath
* @param {Object} config
* @return {Promise}
*/
module.exports.image = function (modules, circular, imagePath, config) {
const options = createGraphvizOptions(config);
options.format = path.extname(imagePath).replace('.', '') || 'png';
return checkGraphvizInstalled(config)
.then(() => {
return createGraph(modules, circular, config, options)
.then((image) => writeFile(imagePath, image))
.then(() => path.resolve(imagePath));
});
};
/**
* Return the module dependency graph as DOT output.
* @param {Object} modules
* @param {Array} circular
* @param {Object} config
* @return {Promise}
*/
module.exports.dot = function (modules, circular, config) {
const options = createGraphvizOptions(config);
options.format = 'dot';
return checkGraphvizInstalled(config)
.then(() => createGraph(modules, circular, config, options))
.then((output) => output.toString('utf8'));
};