Skip to content

Commit

Permalink
Add support for dependencyFilter function
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Feb 5, 2017
1 parent 7f3bbd3 commit eac8591
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Property | Type | Default | Description
`graphVizOptions` | Object | false | Custom GraphViz [options](http://www.graphviz.org/content/attrs)
`graphVizPath` | String | null | Custom GraphViz path
`detectiveOptions` | Object | false | Custom `detective` options for [dependency-tree](https://github.com/dependents/node-dependency-tree)
`dependencyFilter` | Function | false | Function called with a dependency filepath (exclude substree by returning false)

> Note that when running the CLI it's possible to use a runtime configuration file. The config should placed in `.madgerc` in your project or home folder. Look [here](https://github.com/dominictarr/rc#standards) for alternative locations for the file. Here's an example:
Expand Down
3 changes: 2 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const defaultConfig = {
cyclicNodeColor: '#ff6c60',
edgeColor: '#757575',
graphVizOptions: false,
graphVizPath: false
graphVizPath: false,
dependencyFilter: false
};

class Madge {
Expand Down
19 changes: 14 additions & 5 deletions lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Tree {
}

walk.sync(srcPath, (filePath, stat) => {
if (!this.filterPath(filePath) || !stat.isFile()) {
if (this.isNpmPath(filePath) || !stat.isFile()) {
return;
}

Expand Down Expand Up @@ -117,7 +117,16 @@ class Tree {
requireConfig: this.config.requireConfig,
webpackConfig: this.config.webpackConfig,
visited: visited,
filter: this.filterPath.bind(this),
filter: (dependencyFilePath, traversedFilePath) => {
let dependencyFilterRes = true;
const isNpmPath = this.isNpmPath(dependencyFilePath);

if (this.config.dependencyFilter) {
dependencyFilterRes = this.config.dependencyFilter(dependencyFilePath, traversedFilePath, this.baseDir);
}

return !isNpmPath && (dependencyFilterRes || dependencyFilterRes === undefined);
},
detective: this.config.detectiveOptions,
nonExistent: nonExistent
}));
Expand Down Expand Up @@ -184,12 +193,12 @@ class Tree {
}

/**
* Filter out some paths from found files
* Check if path is from NPM folder
* @param {String} path
* @return {Boolean}
*/
filterPath(path) {
return this.config.includeNpm || path.indexOf('node_modules') < 0;
isNpmPath(path) {
return path.indexOf('node_modules') >= 0;
}

/**
Expand Down
58 changes: 58 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,64 @@ describe('API', () => {
}).catch(done);
});

describe('dependencyFilter', () => {
it('will stop traversing when returning false', (done) => {
madge(__dirname + '/cjs/a.js', {
dependencyFilter: () => {
return false;
}
}).then((res) => {
res.obj().should.eql({
a: []
});
done();
}).catch(done);
});

it('will not stop traversing when not returning anything', (done) => {
madge(__dirname + '/cjs/a.js', {
dependencyFilter: () => {}
}).then((res) => {
res.obj().should.eql({
a: ['b', 'c'],
b: ['c'],
c: []
});
done();
}).catch(done);
});

it('will pass arguments to the function', (done) => {
let counter = 0;

madge(__dirname + '/cjs/a.js', {
dependencyFilter: (dependencyFilePath, traversedFilePath, baseDir) => {
if (counter === 0) {
dependencyFilePath.should.match(/test\/cjs\/b\.js$/);
traversedFilePath.should.match(/test\/cjs\/a\.js$/);
baseDir.should.match(/test\/cjs$/);
}

if (counter === 1) {
dependencyFilePath.should.match(/test\/cjs\/c\.js$/);
traversedFilePath.should.match(/test\/cjs\/a\.js$/);
baseDir.should.match(/test\/cjs$/);
}

if (counter === 2) {
dependencyFilePath.should.match(/test\/cjs\/c\.js$/);
traversedFilePath.should.match(/test\/cjs\/b\.js$/);
baseDir.should.match(/test\/cjs$/);
}

counter++;
}
}).then(() => {
done();
}).catch(done);
});
});

describe('obj()', () => {
it('returns dependency object', (done) => {
madge(__dirname + '/cjs/a.js').then((res) => {
Expand Down

0 comments on commit eac8591

Please sign in to comment.