2
2
3
3
const path = require ( 'path' ) ;
4
4
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' ) ;
7
8
const exec = promisify ( require ( 'child_process' ) . execFile ) ;
8
9
const writeFile = promisify ( require ( 'fs' ) . writeFile ) ;
9
10
@@ -13,8 +14,8 @@ const writeFile = promisify(require('fs').writeFile);
13
14
* @param {String } color
14
15
*/
15
16
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 ) ;
18
19
}
19
20
20
21
/**
@@ -45,28 +46,31 @@ function createGraphvizOptions(config) {
45
46
const graphVizOptions = config . graphVizOptions || { } ;
46
47
47
48
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
+ }
70
74
} ;
71
75
}
72
76
@@ -79,16 +83,12 @@ function createGraphvizOptions(config) {
79
83
* @return {Promise }
80
84
*/
81
85
function createGraph ( modules , circular , config , options ) {
82
- const g = graphviz . digraph ( 'G' ) ;
86
+ const g = gv . digraph ( 'G' ) ;
83
87
const nodes = { } ;
84
88
const cyclicModules = circular . reduce ( ( a , b ) => a . concat ( b ) , [ ] ) ;
85
89
86
- if ( config . graphVizPath ) {
87
- g . setGraphVizPath ( config . graphVizPath ) ;
88
- }
89
-
90
90
Object . keys ( modules ) . forEach ( ( id ) => {
91
- nodes [ id ] = nodes [ id ] || g . addNode ( id ) ;
91
+ nodes [ id ] = nodes [ id ] || g . createNode ( id ) ;
92
92
93
93
if ( ! modules [ id ] . length ) {
94
94
setNodeColor ( nodes [ id ] , config . noDependencyColor ) ;
@@ -97,7 +97,7 @@ function createGraph(modules, circular, config, options) {
97
97
}
98
98
99
99
modules [ id ] . forEach ( ( depId ) => {
100
- nodes [ depId ] = nodes [ depId ] || g . addNode ( depId ) ;
100
+ nodes [ depId ] = nodes [ depId ] || g . createNode ( depId ) ;
101
101
102
102
if ( ! modules [ depId ] ) {
103
103
setNodeColor ( nodes [ depId ] , config . noDependencyColor ) ;
@@ -106,12 +106,11 @@ function createGraph(modules, circular, config, options) {
106
106
g . addEdge ( nodes [ id ] , nodes [ depId ] ) ;
107
107
} ) ;
108
108
} ) ;
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 ) ;
115
114
}
116
115
117
116
/**
@@ -124,7 +123,7 @@ function createGraph(modules, circular, config, options) {
124
123
module . exports . svg = function ( modules , circular , config ) {
125
124
const options = createGraphvizOptions ( config ) ;
126
125
127
- options . type = 'svg' ;
126
+ options . format = 'svg' ;
128
127
129
128
return checkGraphvizInstalled ( config )
130
129
. then ( ( ) => createGraph ( modules , circular , config , options ) ) ;
@@ -141,7 +140,7 @@ module.exports.svg = function (modules, circular, config) {
141
140
module . exports . image = function ( modules , circular , imagePath , config ) {
142
141
const options = createGraphvizOptions ( config ) ;
143
142
144
- options . type = path . extname ( imagePath ) . replace ( '.' , '' ) || 'png' ;
143
+ options . format = path . extname ( imagePath ) . replace ( '.' , '' ) || 'png' ;
145
144
146
145
return checkGraphvizInstalled ( config )
147
146
. then ( ( ) => {
@@ -161,7 +160,7 @@ module.exports.image = function (modules, circular, imagePath, config) {
161
160
module . exports . dot = function ( modules , circular , config ) {
162
161
const options = createGraphvizOptions ( config ) ;
163
162
164
- options . type = 'dot' ;
163
+ options . format = 'dot' ;
165
164
166
165
return checkGraphvizInstalled ( config )
167
166
. then ( ( ) => createGraph ( modules , circular , config , options ) )
0 commit comments