Skip to content

Commit 9b9ae87

Browse files
committed
Changed to depend on the ts-graphviz package
1 parent c12ef70 commit 9b9ae87

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

lib/graph.js

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
const path = require('path');
44
const {promisify} = require('util');
5-
const graphviz = require('graphviz');
6-
5+
const gv = require('ts-graphviz');
6+
const adapter = require('ts-graphviz/adapter');
7+
const toArray = require('stream-to-array');
78
const exec = promisify(require('child_process').execFile);
89
const writeFile = promisify(require('fs').writeFile);
910

@@ -13,8 +14,8 @@ const writeFile = promisify(require('fs').writeFile);
1314
* @param {String} color
1415
*/
1516
function setNodeColor(node, color) {
16-
node.set('color', color);
17-
node.set('fontcolor', color);
17+
node.attributes.set('color', color);
18+
node.attributes.set('fontcolor', color);
1819
}
1920

2021
/**
@@ -45,28 +46,31 @@ function createGraphvizOptions(config) {
4546
const graphVizOptions = config.graphVizOptions || {};
4647

4748
return {
48-
// Graph
49-
G: Object.assign({
50-
overlap: false,
51-
pad: 0.3,
52-
rankdir: config.rankdir,
53-
layout: config.layout,
54-
bgcolor: config.backgroundColor
55-
}, graphVizOptions.G),
56-
// Edge
57-
E: Object.assign({
58-
color: config.edgeColor
59-
}, graphVizOptions.E),
60-
// Node
61-
N: Object.assign({
62-
fontname: config.fontName,
63-
fontsize: config.fontSize,
64-
color: config.nodeColor,
65-
shape: config.nodeShape,
66-
style: config.nodeStyle,
67-
height: 0,
68-
fontcolor: config.nodeColor
69-
}, graphVizOptions.N)
49+
dotCommand: config.graphVizPath ? config.graphVizPath : null,
50+
attributes: {
51+
// Graph
52+
graph: Object.assign({
53+
overlap: false,
54+
pad: 0.3,
55+
rankdir: config.rankdir,
56+
layout: config.layout,
57+
bgcolor: config.backgroundColor
58+
}, graphVizOptions.G),
59+
// Edge
60+
edge: Object.assign({
61+
color: config.edgeColor
62+
}, graphVizOptions.E),
63+
// Node
64+
node: Object.assign({
65+
fontname: config.fontName,
66+
fontsize: config.fontSize,
67+
color: config.nodeColor,
68+
shape: config.nodeShape,
69+
style: config.nodeStyle,
70+
height: 0,
71+
fontcolor: config.nodeColor
72+
}, graphVizOptions.N)
73+
}
7074
};
7175
}
7276

@@ -79,16 +83,12 @@ function createGraphvizOptions(config) {
7983
* @return {Promise}
8084
*/
8185
function createGraph(modules, circular, config, options) {
82-
const g = graphviz.digraph('G');
86+
const g = gv.digraph('G');
8387
const nodes = {};
8488
const cyclicModules = circular.reduce((a, b) => a.concat(b), []);
8589

86-
if (config.graphVizPath) {
87-
g.setGraphVizPath(config.graphVizPath);
88-
}
89-
9090
Object.keys(modules).forEach((id) => {
91-
nodes[id] = nodes[id] || g.addNode(id);
91+
nodes[id] = nodes[id] || g.createNode(id);
9292

9393
if (!modules[id].length) {
9494
setNodeColor(nodes[id], config.noDependencyColor);
@@ -97,7 +97,7 @@ function createGraph(modules, circular, config, options) {
9797
}
9898

9999
modules[id].forEach((depId) => {
100-
nodes[depId] = nodes[depId] || g.addNode(depId);
100+
nodes[depId] = nodes[depId] || g.createNode(depId);
101101

102102
if (!modules[depId]) {
103103
setNodeColor(nodes[depId], config.noDependencyColor);
@@ -106,12 +106,11 @@ function createGraph(modules, circular, config, options) {
106106
g.addEdge(nodes[id], nodes[depId]);
107107
});
108108
});
109-
110-
return new Promise((resolve, reject) => {
111-
g.output(options, resolve, (code, out, err) => {
112-
reject(new Error(err));
113-
});
114-
});
109+
const dot = gv.toDot(g);
110+
return adapter
111+
.toStream(dot, options)
112+
.then(toArray)
113+
.then(Buffer.concat);
115114
}
116115

117116
/**
@@ -124,7 +123,7 @@ function createGraph(modules, circular, config, options) {
124123
module.exports.svg = function (modules, circular, config) {
125124
const options = createGraphvizOptions(config);
126125

127-
options.type = 'svg';
126+
options.format = 'svg';
128127

129128
return checkGraphvizInstalled(config)
130129
.then(() => createGraph(modules, circular, config, options));
@@ -141,7 +140,7 @@ module.exports.svg = function (modules, circular, config) {
141140
module.exports.image = function (modules, circular, imagePath, config) {
142141
const options = createGraphvizOptions(config);
143142

144-
options.type = path.extname(imagePath).replace('.', '') || 'png';
143+
options.format = path.extname(imagePath).replace('.', '') || 'png';
145144

146145
return checkGraphvizInstalled(config)
147146
.then(() => {
@@ -161,7 +160,7 @@ module.exports.image = function (modules, circular, imagePath, config) {
161160
module.exports.dot = function (modules, circular, config) {
162161
const options = createGraphvizOptions(config);
163162

164-
options.type = 'dot';
163+
options.format = 'dot';
165164

166165
return checkGraphvizInstalled(config)
167166
.then(() => createGraph(modules, circular, config, options))

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@
5757
"detective-scss": "^2.0.1",
5858
"detective-stylus": "^1.0.0",
5959
"detective-typescript": "^7.0.0",
60-
"graphviz": "0.0.9",
6160
"ora": "^5.4.1",
6261
"pluralize": "^8.0.0",
6362
"precinct": "^8.1.0",
6463
"pretty-ms": "^7.0.1",
6564
"rc": "^1.2.7",
65+
"stream-to-array": "^2.3.0",
66+
"ts-graphviz": "^1.5.0",
6667
"typescript": "^3.9.5",
6768
"walkdir": "^0.4.1"
6869
},

0 commit comments

Comments
 (0)