diff --git a/lib/graph.js b/lib/graph.js index 29032ea4..4d5b459c 100644 --- a/lib/graph.js +++ b/lib/graph.js @@ -2,8 +2,9 @@ const path = require('path'); const {promisify} = require('util'); -const graphviz = require('graphviz'); - +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); @@ -13,8 +14,8 @@ const writeFile = promisify(require('fs').writeFile); * @param {String} color */ function setNodeColor(node, color) { - node.set('color', color); - node.set('fontcolor', color); + node.attributes.set('color', color); + node.attributes.set('fontcolor', color); } /** @@ -45,28 +46,31 @@ function createGraphvizOptions(config) { const graphVizOptions = config.graphVizOptions || {}; return { - // Graph - G: Object.assign({ - overlap: false, - pad: 0.3, - rankdir: config.rankdir, - layout: config.layout, - bgcolor: config.backgroundColor - }, graphVizOptions.G), - // Edge - E: Object.assign({ - color: config.edgeColor - }, graphVizOptions.E), - // Node - N: Object.assign({ - fontname: config.fontName, - fontsize: config.fontSize, - color: config.nodeColor, - shape: config.nodeShape, - style: config.nodeStyle, - height: 0, - fontcolor: config.nodeColor - }, graphVizOptions.N) + 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) + } }; } @@ -79,16 +83,12 @@ function createGraphvizOptions(config) { * @return {Promise} */ function createGraph(modules, circular, config, options) { - const g = graphviz.digraph('G'); + const g = gv.digraph('G'); const nodes = {}; const cyclicModules = circular.reduce((a, b) => a.concat(b), []); - if (config.graphVizPath) { - g.setGraphVizPath(config.graphVizPath); - } - Object.keys(modules).forEach((id) => { - nodes[id] = nodes[id] || g.addNode(id); + nodes[id] = nodes[id] || g.createNode(id); if (!modules[id].length) { setNodeColor(nodes[id], config.noDependencyColor); @@ -97,7 +97,7 @@ function createGraph(modules, circular, config, options) { } modules[id].forEach((depId) => { - nodes[depId] = nodes[depId] || g.addNode(depId); + nodes[depId] = nodes[depId] || g.createNode(depId); if (!modules[depId]) { setNodeColor(nodes[depId], config.noDependencyColor); @@ -106,12 +106,11 @@ function createGraph(modules, circular, config, options) { g.addEdge(nodes[id], nodes[depId]); }); }); - - return new Promise((resolve, reject) => { - g.output(options, resolve, (code, out, err) => { - reject(new Error(err)); - }); - }); + const dot = gv.toDot(g); + return adapter + .toStream(dot, options) + .then(toArray) + .then(Buffer.concat); } /** @@ -124,7 +123,7 @@ function createGraph(modules, circular, config, options) { module.exports.svg = function (modules, circular, config) { const options = createGraphvizOptions(config); - options.type = 'svg'; + options.format = 'svg'; return checkGraphvizInstalled(config) .then(() => createGraph(modules, circular, config, options)); @@ -141,7 +140,7 @@ module.exports.svg = function (modules, circular, config) { module.exports.image = function (modules, circular, imagePath, config) { const options = createGraphvizOptions(config); - options.type = path.extname(imagePath).replace('.', '') || 'png'; + options.format = path.extname(imagePath).replace('.', '') || 'png'; return checkGraphvizInstalled(config) .then(() => { @@ -161,7 +160,7 @@ module.exports.image = function (modules, circular, imagePath, config) { module.exports.dot = function (modules, circular, config) { const options = createGraphvizOptions(config); - options.type = 'dot'; + options.format = 'dot'; return checkGraphvizInstalled(config) .then(() => createGraph(modules, circular, config, options)) diff --git a/package.json b/package.json index 68297c0e..66d8ac6b 100644 --- a/package.json +++ b/package.json @@ -57,12 +57,13 @@ "detective-scss": "^2.0.1", "detective-stylus": "^1.0.0", "detective-typescript": "^7.0.0", - "graphviz": "0.0.9", "ora": "^5.4.1", "pluralize": "^8.0.0", "precinct": "^8.1.0", "pretty-ms": "^7.0.1", "rc": "^1.2.7", + "stream-to-array": "^2.3.0", + "ts-graphviz": "^1.5.0", "typescript": "^3.9.5", "walkdir": "^0.4.1" },