Skip to content

Commit

Permalink
Changed to depend on the ts-graphviz package
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiazya committed Jan 17, 2023
1 parent c12ef70 commit 9b9ae87
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
83 changes: 41 additions & 42 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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)
}
};
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

/**
Expand All @@ -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));
Expand All @@ -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(() => {
Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit 9b9ae87

Please sign in to comment.