-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
45 lines (34 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env node
var mdeps = require('module-deps'),
path = require('path'),
graphlib = require('graphlib');
var core = ['events', 'path', 'util', 'vm', 'dns', 'dgram', 'http', 'https', 'net', 'fs'];
var externalModuleRegexp = process.platform === 'win32' ?
/^(\.|\w:)/ :
/^[\/.]/;
var transform = [];
try {
transform = require(path.resolve('package.json')).browserify.transform;
} catch(e) { }
var g = new graphlib.Graph({ directed: true });
var md = mdeps({
transform: transform,
filter: function(id) {
return core.indexOf(id) === -1 && externalModuleRegexp.test(id);
}
});
md.on('data', function(entry) {
Object.keys(entry.deps).forEach(function(dep) {
g.setEdge(entry.id, entry.deps[dep]);
});
});
md.on('end', function() {
var cycles = graphlib.alg.findCycles(g);
console.log('found %s cycles', cycles.length);
cycles.forEach(function(cycle) {
console.log(cycle);
});
if (cycles.length)
process.exit(1);
});
md.end({ file: process.argv[2] });