-
Notifications
You must be signed in to change notification settings - Fork 1
/
scan_node_modules.js
75 lines (68 loc) · 1.55 KB
/
scan_node_modules.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var fs = require('fs');
var path = require('path');
function walk(filepath, cb)
{
var ls = fs.readdirSync(filepath);
for (i in ls) {
var f = ls[i];
var fstat = fs.statSync(filepath + "/" + f);
if (fstat.isFile()) {
cb(filepath + "/" + f);
}
else if (fstat.isDirectory()) {
walk(filepath + "/" + f, cb);
}
}
}
var stats = {};
var dependencies = {};
walk('.', function(filepath) {
if (path.basename(filepath) == "package.json") {
var packageJson = JSON.parse(fs.readFileSync(filepath));
var deps = packageJson.dependencies;
if (deps) {
for (dep in deps) {
dep = dep.replace(/['"]+/g, '');
var version = deps[dep];
if (!(dep in dependencies)) {
dependencies[dep] = {};
}
if (!(version in dependencies[dep])) {
dependencies[dep][version] = [];
}
dependencies[dep][version].push(filepath);
}
}
} else {
var loc = stats;
var parts = filepath.split(path.sep);
parts.pop();
for (i in parts) {
var _dir = parts[i];
if (!(_dir in loc)) {
loc[_dir] = { _count:1 }
} else {
loc[_dir]._count++;
}
loc = loc[_dir];
}
}
});
var max_depth = 2;
function print(o, depth)
{
if (depth > max_depth)
return;
var inset = '';
for (var i = 0; i < depth; i++)
inset += '.';
for (k in o) {
if (k != "_count") {
console.log(inset + k + " - " + o[k]._count);
print(o[k], depth + 1);
}
}
}
console.log("done");
print(stats, 0);
console.log(dependencies);