Skip to content

Commit

Permalink
Add .svg() in API to export the svg as a Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
lleaff committed Aug 3, 2018
1 parent 005fb2e commit d01f6f3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,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 @@ -150,6 +150,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
12 changes: 12 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ 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);
done();
})
.catch(done);
});
});

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

Expand Down

0 comments on commit d01f6f3

Please sign in to comment.