Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add —-orphans to show modules that no one is depending on #121

Merged
merged 1 commit into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,19 @@ madge('path/to/app.js').then((res) => {
const madge = require('madge');

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

#### .orphans()

> Return a `Array` of all modules that no one is depending on.

```javascript
const madge = require('madge');

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

Expand Down Expand Up @@ -226,13 +238,13 @@ $ madge --circular path/src/app.js
> Show modules that depends on a given module

```sh
$ madge --depends 'wheels' path/src/app.js
$ madge --depends wheels.js path/src/app.js
```

> Excluding modules

```sh
$ madge --exclude '^(foo|bar)$' path/src/app.js
$ madge --exclude '^(foo|bar)\.js$' path/src/app.js
```

> Save graph as a SVG image (graphviz required)
Expand Down
9 changes: 9 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ program
.option('-j, --json', 'output as JSON')
.option('-i, --image <file>', 'write graph to file as an image')
.option('-l, --layout <name>', 'layout engine to use for graph (dot/neato/fdp/sfdp/twopi/circo)')
.option('--orphans', 'show modules that no one is depending on')
.option('--dot', 'show graph using the DOT language')
.option('--extensions <list>', 'comma separated string of valid file extensions')
.option('--require-config <file>', 'path to RequireJS config')
Expand Down Expand Up @@ -164,6 +165,14 @@ new Promise((resolve, reject) => {
return res;
}

if (program.orphans) {
output.modules(res.orphans(), {
json: program.json
});

return res;
}

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

Expand Down
22 changes: 22 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ class Madge {
.filter((dep) => tree[dep].indexOf(id) >= 0);
}

/**
* Return a list of modules that no one is depending on.
* @api public
* @return {Array}
*/
orphans() {
const tree = this.obj();
const map = {};

Object
.keys(tree)
.forEach((dep) => {
tree[dep].forEach((id) => {
map[id] = true;
});
});

return Object
.keys(tree)
.filter((dep) => !map[dep]);
}

/**
* Return the module dependency graph as DOT output.
* @api public
Expand Down
9 changes: 9 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ describe('API', () => {
});
});

describe('orphans()', () => {
it('returns modules that no one is depending on', (done) => {
madge(__dirname + '/cjs/normal').then((res) => {
res.orphans().should.eql(['a.js']);
done();
}).catch(done);
});
});

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

Expand Down