Skip to content

Commit

Permalink
Add support for combining --image and --circular
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Sep 14, 2020
1 parent 8a44776 commit 7a4bd3b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ madge('path/to/app.js').then((res) => {
});
```

#### .circularGraph()

> Returns an `Object` with only circular dependencies.
```javascript
const madge = require('madge');

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

#### .depends()

> Returns an `Array` of all modules that depend on a given module.
Expand Down Expand Up @@ -177,9 +189,9 @@ madge('path/to/app.js')
});
```

#### .image(imagePath: string)
#### .image(imagePath: string, [circularOnly: boolean])

> Write the graph as an image to the given image path. The [image format](http://www.graphviz.org/content/output-formats) to use is determined from the file extension. Returns a `Promise` resolved with a full path to the written image.
> Write the graph as an image to the given image path. Set `circularOnly` to only include circular dependencies. The [image format](http://www.graphviz.org/content/output-formats) to use is determined from the file extension. Returns a `Promise` resolved with a full path to the written image.
```javascript
const madge = require('madge');
Expand Down Expand Up @@ -334,6 +346,12 @@ madge --exclude '^(foo|bar)\.js$' path/src/app.js
madge --image graph.svg path/src/app.js
```

> Save graph with only circular dependencies
```sh
madge --circular --image graph.svg path/src/app.js
```

> Save graph as a [DOT](http://en.wikipedia.org/wiki/DOT_language) file for further processing (requires [Graphviz](#graphviz-optional))
```sh
Expand Down
14 changes: 7 additions & 7 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ function createOutputFromOptions(program, res) {
return res;
}

if (program.image) {
return res.image(program.image, program.circular).then((imagePath) => {
spinner.succeed(`${chalk.bold('Image created at')} ${chalk.cyan.bold(imagePath)}`);
return res;
});
}

if (program.circular) {
const circular = res.circular();

Expand All @@ -255,13 +262,6 @@ function createOutputFromOptions(program, res) {
return res;
}

if (program.image) {
return res.image(program.image).then((imagePath) => {
spinner.succeed(`${chalk.bold('Image created at')} ${chalk.cyan.bold(imagePath)}`);
return res;
});
}

if (program.dot) {
return res.dot().then((output) => {
process.stdout.write(output);
Expand Down
28 changes: 25 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,28 @@ class Madge {
/**
* Return the modules that has circular dependencies.
* @api public
* @return {Object}
* @return {Array}
*/
circular() {
return cyclic(this.tree);
}

/**
* Return circular dependency graph.
* @api public
* @return {Object}
*/
circularGraph() {
const circularDeps = this.circular();

return Object.entries(this.obj())
.filter(([k]) => circularDeps.some((x) => x.includes(k)))
.reduce((acc, [k, v]) => {
acc[k] = v.filter((x) => circularDeps.some((y) => y.includes(x)));
return acc;
}, {});
}

/**
* Return a list of modules that depends on the given module.
* @api public
Expand Down Expand Up @@ -152,14 +168,20 @@ class Madge {
* Write dependency graph to image.
* @api public
* @param {String} imagePath
* @param {Boolean} circularOnly
* @return {Promise}
*/
image(imagePath) {
image(imagePath, circularOnly) {
if (!imagePath) {
return Promise.reject(new Error('imagePath not provided'));
}

return graph.image(this.obj(), this.circular(), imagePath, this.config);
return graph.image(
circularOnly ? this.circularGraph() : this.obj(),
this.circular(),
imagePath,
this.config
);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ describe('API', () => {
});
});

describe('circular()', () => {
it('returns list of circular dependencies', (done) => {
madge(__dirname + '/cjs/circular/a.js').then((res) => {
res.circular().should.eql([
['a.js', 'd.js']
]);
done();
}).catch(done);
});
});

describe('circularGraph()', () => {
it('returns graph with only circular dependencies', (done) => {
madge(__dirname + '/cjs/circular/a.js').then((res) => {
res.circularGraph().should.eql({
'a.js': ['d.js'],
'd.js': ['a.js']
});
done();
}).catch(done);
});
});

describe('warnings()', () => {
it('returns an array of skipped files', (done) => {
madge(__dirname + '/cjs/missing.js').then((res) => {
Expand Down

0 comments on commit 7a4bd3b

Please sign in to comment.