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

Show skipped files as warnings (disable with —-no-warning) #108

Merged
merged 1 commit into from
Oct 6, 2016
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 18 additions & 4 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ program
.option('--require-config <file>', 'path to RequireJS config')
.option('--webpack-config <file>', '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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
16 changes: 14 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Tree {
generateTree(files) {
const depTree = {};
const visited = {};
const nonExistent = [];

files.forEach((file) => {
if (visited[file]) {
Expand All @@ -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
}));
});

Expand All @@ -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
};
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 2 additions & 0 deletions test/cjs/missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./c');
require('./path/non/existing/file');