Skip to content

Commit

Permalink
Merge pull request #121 from pahen/orphans
Browse files Browse the repository at this point in the history
Add —-orphans to show modules that no one is depending on
  • Loading branch information
pahen authored Jul 15, 2017
2 parents 3b16e61 + 8007f33 commit 75d543a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
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

0 comments on commit 75d543a

Please sign in to comment.