1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const pify = require('pify');
|
5 | const graphviz = require('graphviz');
|
6 |
|
7 | const exec = pify(require('child_process').exec);
|
8 | const writeFile = pify(require('fs').writeFile);
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function setNodeColor(node, color) {
|
16 | node.set('color', color);
|
17 | node.set('fontcolor', color);
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function checkGraphvizInstalled(config) {
|
26 | if (config.graphVizPath) {
|
27 | const cmd = path.join(config.graphVizPath, 'gvpr -V');
|
28 | return exec(cmd)
|
29 | .catch(() => {
|
30 | throw new Error('Could not execute ' + cmd);
|
31 | });
|
32 | }
|
33 |
|
34 | return exec('gvpr -V')
|
35 | .catch((error) => {
|
36 | throw new Error('Graphviz could not be found. Ensure that "gvpr" is in your $PATH.\n' + error);
|
37 | });
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function createGraphvizOptions(config) {
|
46 | const graphVizOptions = config.graphVizOptions || {};
|
47 |
|
48 | return {
|
49 |
|
50 | G: Object.assign({
|
51 | overlap: false,
|
52 | pad: 0.3,
|
53 | rankdir: config.rankdir,
|
54 | layout: config.layout,
|
55 | bgcolor: config.backgroundColor
|
56 | }, graphVizOptions.G),
|
57 |
|
58 | E: Object.assign({
|
59 | color: config.edgeColor
|
60 | }, graphVizOptions.E),
|
61 |
|
62 | N: Object.assign({
|
63 | fontname: config.fontName,
|
64 | fontsize: config.fontSize,
|
65 | color: config.nodeColor,
|
66 | shape: config.nodeShape,
|
67 | style: config.nodeStyle,
|
68 | height: 0,
|
69 | fontcolor: config.nodeColor
|
70 | }, graphVizOptions.N)
|
71 | };
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function createGraph(modules, circular, config, options) {
|
83 | const g = graphviz.digraph('G');
|
84 | const nodes = {};
|
85 | const cyclicModules = circular.reduce((a, b) => a.concat(b), []);
|
86 |
|
87 | if (config.graphVizPath) {
|
88 | g.setGraphVizPath(config.graphVizPath);
|
89 | }
|
90 |
|
91 | Object.keys(modules).forEach((id) => {
|
92 | nodes[id] = nodes[id] || g.addNode(id);
|
93 |
|
94 | if (!modules[id].length) {
|
95 | setNodeColor(nodes[id], config.noDependencyColor);
|
96 | } else if (cyclicModules.indexOf(id) >= 0) {
|
97 | setNodeColor(nodes[id], config.cyclicNodeColor);
|
98 | }
|
99 |
|
100 | modules[id].forEach((depId) => {
|
101 | nodes[depId] = nodes[depId] || g.addNode(depId);
|
102 |
|
103 | if (!modules[depId]) {
|
104 | setNodeColor(nodes[depId], config.noDependencyColor);
|
105 | }
|
106 |
|
107 | g.addEdge(nodes[id], nodes[depId]);
|
108 | });
|
109 | });
|
110 |
|
111 | return new Promise((resolve, reject) => {
|
112 | g.output(options, resolve, (code, out, err) => {
|
113 | reject(new Error(err));
|
114 | });
|
115 | });
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | module.exports.svg = function (modules, circular, config) {
|
126 | const options = createGraphvizOptions(config);
|
127 |
|
128 | options.type = 'svg';
|
129 |
|
130 | return checkGraphvizInstalled(config)
|
131 | .then(() => createGraph(modules, circular, config, options));
|
132 | };
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | module.exports.image = function (modules, circular, imagePath, config) {
|
143 | const options = createGraphvizOptions(config);
|
144 |
|
145 | options.type = path.extname(imagePath).replace('.', '') || 'png';
|
146 |
|
147 | return checkGraphvizInstalled(config)
|
148 | .then(() => {
|
149 | return createGraph(modules, circular, config, options)
|
150 | .then((image) => writeFile(imagePath, image))
|
151 | .then(() => path.resolve(imagePath));
|
152 | });
|
153 | };
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | module.exports.dot = function (modules, circular, config) {
|
163 | const options = createGraphvizOptions(config);
|
164 |
|
165 | options.type = 'dot';
|
166 |
|
167 | return checkGraphvizInstalled(config)
|
168 | .then(() => createGraph(modules, circular, config, options))
|
169 | .then((output) => output.toString('utf8'));
|
170 | };
|