Skip to content

Commit

Permalink
Merge pull request #171 from lleaff/master
Browse files Browse the repository at this point in the history
Add an .svg public method to the API
  • Loading branch information
pahen committed Jun 3, 2019
2 parents 4b4a37a + 13446e7 commit cf3d3d1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ madge('path/to/app.js')
});
```

#### .svg()

> Return a `Promise` resolved with the XML SVG representation of the dependency graph as a `Buffer`.
```javascript
const madge = require('madge');

madge('path/to/app.js')
.then((res) => res.svg())
.then((output) => {
console.log(output.toString());
});
```

# Configuration

Property | Type | Default | Description
Expand Down
9 changes: 9 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ class Madge {

return graph.image(this.obj(), this.circular(), imagePath, this.config);
}

/**
* Return Buffer with XML SVG representation of the dependency graph.
* @api public
* @return {Promise}
*/
svg() {
return graph.svg(this.obj(), this.circular(), this.config);
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ 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,
Expand Down Expand Up @@ -112,6 +115,24 @@ function createGraph(modules, circular, config, options) {
});
}

/**
* Return the module dependency graph XML SVG representation as a Buffer.
* @param {Object} modules
* @param {Array} circular
* @param {Object} config
* @return {Promise}
*/
module.exports.svg = function (modules, circular, config) {
const options = createGraphvizOptions(config);

options.type = 'svg';

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

/**
* Creates an image from the module dependency graph.
* @param {Object} modules
Expand Down
13 changes: 13 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ describe('API', () => {
});
});

describe('svg()', () => {
it('returns a promise resolved with XML SVG output in a Buffer', (done) => {
madge(__dirname + '/cjs/b.js')
.then((res) => res.svg())
.then((output) => {
output.should.instanceof(Buffer);
output.toString().should.match(/<svg.*/);
done();
})
.catch(done);
});
});

describe('image()', () => {
let imagePath;

Expand Down

0 comments on commit cf3d3d1

Please sign in to comment.