From 2a0d61e60ecb049cf0c040fdd4d511e972295c4d Mon Sep 17 00:00:00 2001 From: Patrik Henningsson Date: Thu, 6 Oct 2016 18:16:06 +0200 Subject: [PATCH] =?UTF-8?q?Show=20skipped=20files=20as=20warnings=20(disab?= =?UTF-8?q?le=20with=20=E2=80=94-no-warning)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++++ bin/cli.js | 22 ++++++++++++++++++---- lib/api.js | 16 ++++++++++++++-- lib/output.js | 17 +++++++++++++++++ lib/tree.js | 11 +++++++---- test/api.js | 15 +++++++++++++++ test/cjs/missing.js | 2 ++ 7 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 test/cjs/missing.js diff --git a/README.md b/README.md index 0be32ea1..036f02e9 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,18 @@ madge('path/to/app.js').then((res) => { }); ``` +#### .warnings() + +> Returns an `Object` of warnings. + +```javascript +const madge = require('madge'); + +madge('path/to/app.js').then((res) => { + console.log(res.warnings()); +}); +``` + #### .circular() > Returns an `Array` with all modules that has circular dependencies. diff --git a/bin/cli.js b/bin/cli.js index 060ca6f9..c217b96d 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -23,6 +23,7 @@ program .option('--require-config ', 'path to RequireJS config') .option('--webpack-config ', 'path to webpack config') .option('--no-color', 'disable color in output and image', false) + .option('--no-warning', 'disable warnings', false) .option('--stdin', 'read predefined tree from STDIN', false) .option('--debug', 'turn on debugĀ output', false) .parse(process.argv); @@ -103,15 +104,19 @@ new Promise((resolve, reject) => { }) .then((res) => { if (program.summary) { - return output.summary(res.obj(), { + output.summary(res.obj(), { json: program.json }); + + return res; } if (program.depends) { - return output.depends(res.depends(program.depends), { + output.depends(res.depends(program.depends), { json: program.json }); + + return res; } if (program.circular) { @@ -125,24 +130,33 @@ new Promise((resolve, reject) => { process.exit(1); } - return; + return res; } if (program.image) { return res.image(program.image).then((imagePath) => { console.log('Image created at %s', imagePath); + return res; }); } if (program.dot) { return res.dot().then((output) => { process.stdout.write(output); + return res; }); } - return output.list(res.obj(), { + output.list(res.obj(), { json: program.json }); + + return res; +}) +.then((res) => { + if (program.warning && !program.json) { + output.warnings(res); + } }) .catch((err) => { output.error(err); diff --git a/lib/api.js b/lib/api.js index 93f2708b..4c060bc3 100644 --- a/lib/api.js +++ b/lib/api.js @@ -51,8 +51,9 @@ class Madge { path = [path]; } - return tree(path, this.config).then((tree) => { - this.tree = tree; + return tree(path, this.config).then((res) => { + this.tree = res.tree; + this.skipped = res.skipped; return this; }); } @@ -66,6 +67,17 @@ class Madge { return this.tree; } + /** + * Return produced warnings. + * @api public + * @return {Array} + */ + warnings() { + return { + skipped: this.skipped + }; + } + /** * Return the modules that has circular dependencies. * @api public diff --git a/lib/output.js b/lib/output.js index 81d2a514..b0583ecb 100644 --- a/lib/output.js +++ b/lib/output.js @@ -105,6 +105,23 @@ module.exports.depends = function (depends, opts) { }); }; +/** + * Print warnings to the console. + * @param {Object} res + * @return {undefined} + */ +module.exports.warnings = function (res) { + const skipped = res.warnings().skipped; + + if (skipped.length) { + console.log(red.bold('\nSkipped files due to errors:\n')); + + skipped.forEach((file) => { + console.log(red(file)); + }); + } +}; + /** * Print error to the console. * @param {Object} err diff --git a/lib/tree.js b/lib/tree.js index 92d70c31..22a19560 100644 --- a/lib/tree.js +++ b/lib/tree.js @@ -104,6 +104,7 @@ class Tree { generateTree(files) { const depTree = {}; const visited = {}; + const nonExistent = []; files.forEach((file) => { if (visited[file]) { @@ -117,7 +118,8 @@ class Tree { webpackConfig: this.config.webpackConfig, visited: visited, filter: this.filterPath.bind(this), - detective: this.config.detectiveOptions + detective: this.config.detectiveOptions, + nonExistent: nonExistent })); }); @@ -127,9 +129,10 @@ class Tree { tree = this.exclude(tree, this.config.excludeRegExp); } - tree = this.sort(tree); - - return tree; + return { + tree: this.sort(tree), + skipped: nonExistent + }; } /** diff --git a/test/api.js b/test/api.js index 2172e4e2..442b208e 100644 --- a/test/api.js +++ b/test/api.js @@ -113,6 +113,21 @@ describe('API', () => { }); }); + describe('warnings()', () => { + it('returns an array of skipped files', (done) => { + madge(__dirname + '/cjs/missing.js').then((res) => { + res.obj().should.eql({ + missing: ['c'], + c: [] + }); + res.warnings().should.eql({ + skipped: ['./path/non/existing/file'] + }); + done(); + }).catch(done); + }); + }); + describe('dot()', () => { it('returns a promise resolved with graphviz DOT output', (done) => { madge(__dirname + '/cjs/b.js') diff --git a/test/cjs/missing.js b/test/cjs/missing.js new file mode 100644 index 00000000..19d74d8d --- /dev/null +++ b/test/cjs/missing.js @@ -0,0 +1,2 @@ +require('./c'); +require('./path/non/existing/file'); \ No newline at end of file