diff --git a/README.md b/README.md index e906858d..25418f9d 100644 --- a/README.md +++ b/README.md @@ -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()); }); ``` @@ -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) diff --git a/bin/cli.js b/bin/cli.js index 4bf1a564..f6c0136f 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -21,6 +21,7 @@ program .option('-j, --json', 'output as JSON') .option('-i, --image ', 'write graph to file as an image') .option('-l, --layout ', '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 ', 'comma separated string of valid file extensions') .option('--require-config ', 'path to RequireJS config') @@ -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(); diff --git a/lib/api.js b/lib/api.js index 4ee7b83f..9556e9d8 100644 --- a/lib/api.js +++ b/lib/api.js @@ -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 diff --git a/test/api.js b/test/api.js index c240e5a0..001c4481 100644 --- a/test/api.js +++ b/test/api.js @@ -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;