Skip to content

Commit

Permalink
Respect graphVizOptions in DOT output
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Jun 3, 2019
1 parent cf3d3d1 commit 4edf82a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 29 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"extends": "@aptoma/eslint-config",
"parserOptions": {
"ecmaVersion": 9
},
"env": {
"node": true,
"mocha": true,
"es6": true
}
}
}
2 changes: 1 addition & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Madge {
* @return {Promise}
*/
dot() {
return graph.dot(this.obj(), this.config);
return graph.dot(this.obj(), this.circular(), this.config);
}

/**
Expand Down
26 changes: 8 additions & 18 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ module.exports.svg = function (modules, circular, config) {
options.type = 'svg';

return checkGraphvizInstalled(config)
.then(() => {
return createGraph(modules, circular, config, options);
});
.then(() => createGraph(modules, circular, config, options));
};

/**
Expand All @@ -157,24 +155,16 @@ module.exports.image = function (modules, circular, imagePath, config) {
/**
* Return the module dependency graph as DOT output.
* @param {Object} modules
* @param {Array} circular
* @param {Object} config
* @return {Promise}
*/
module.exports.dot = function (modules, config) {
const nodes = {};
const g = graphviz.digraph('G');

return checkGraphvizInstalled(config)
.then(() => {
Object.keys(modules).forEach((id) => {
nodes[id] = nodes[id] || g.addNode(id);
module.exports.dot = function (modules, circular, config) {
const options = createGraphvizOptions(config);

modules[id].forEach((depId) => {
nodes[depId] = nodes[depId] || g.addNode(depId);
g.addEdge(nodes[id], nodes[depId]);
});
});
options.type = 'dot';

return g.to_dot();
});
return checkGraphvizInstalled(config)
.then(() => createGraph(modules, circular, config, options))
.then((output) => output.toString('utf8'));
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
"devDependencies": {
"@aptoma/eslint-config": "^7.0.1",
"eslint": "^5.12.0",
"mocha": "^5.1.1",
"expect": "^1.20.2",
"expect-mocha-snapshot": "^1.0.1",
"mocha": "^5.2.0",
"mz": "^2.7.0",
"should": "^13.2.3"
}
Expand Down
33 changes: 33 additions & 0 deletions test/__snapshots__/api.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`API dot() returns a promise resolved with graphviz DOT output 1`] = `
"digraph G {
graph [bb=\\"0,0,144,24.099\\",
bgcolor=\\"#111111\\",
layout=dot,
overlap=false,
pad=0.3,
rankdir=LR
];
node [color=\\"#c6c5fe\\",
fontcolor=\\"#c6c5fe\\",
fontname=Arial,
fontsize=\\"14px\\",
height=0,
label=\\"\\\\N\\",
shape=box,
style=rounded
];
edge [color=\\"#757575\\"];
\\"b.js\\" [height=0.3347,
pos=\\"27,12.049\\",
width=0.75];
\\"c.js\\" [color=\\"#cfffac\\",
fontcolor=\\"#cfffac\\",
height=0.3347,
pos=\\"117,12.049\\",
width=0.75];
\\"b.js\\" -> \\"c.js\\" [pos=\\"e,89.705,12.049 54.003,12.049 62.028,12.049 70.967,12.049 79.531,12.049\\"];
}
"
`;
14 changes: 6 additions & 8 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const os = require('os');
const path = require('path');
const fs = require('mz/fs');
const madge = require('../lib/api');
const expect = require('expect');
const toMatchSnapshot = require('expect-mocha-snapshot');
expect.extend({toMatchSnapshot});

require('should');

Expand Down Expand Up @@ -187,14 +190,9 @@ describe('API', () => {
});

describe('dot()', () => {
it('returns a promise resolved with graphviz DOT output', (done) => {
madge(__dirname + '/cjs/b.js')
.then((res) => res.dot())
.then((output) => {
output.should.eql('digraph G {\n "b.js";\n "c.js";\n "b.js" -> "c.js";\n}\n');
done();
})
.catch(done);
it('returns a promise resolved with graphviz DOT output', async function () {
const res = await madge(__dirname + '/cjs/b.js');
expect(await res.dot()).toMatchSnapshot(this);
});
});

Expand Down

0 comments on commit 4edf82a

Please sign in to comment.