This repository has been archived by the owner on Oct 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
176 lines (155 loc) · 4.3 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var fs = require("fs");
var path = require("path");
var detective = require('detective');
var q = require('q');
var walkdir = require("walkdir");
var _ = require('lodash');
var minimatch = require('minimatch');
var util = require('util');
var merge = require('merge');
function getModulesRequiredFromFilename(filename) {
var content = fs.readFileSync(filename, "utf-8");
try {
return detective(content, {
word: '',
isRequire: function(node) {
var callee = node.callee;
return callee &&
(
(node.type === 'CallExpression' && callee.type === 'Identifier'
&& callee.name === 'require')
||
(callee.property && callee.property.name === 'loadNpmTasks')
);
}
});
} catch (err) {
return err;
}
}
var missing = {};
function checkDirectory(dir, ignoreDirs, deps, ignorePackages) {
var deferred = q.defer();
var directoryPromises = [];
var finder = walkdir(dir, { "no_recurse": true });
var invalidFiles = {};
finder.on("directory", function (subdir) {
if (_.contains(ignoreDirs, path.basename(subdir))
|| (_.isEmpty(deps))) {
return;
}
directoryPromises.push(checkDirectory(subdir, ignoreDirs, deps, ignorePackages));
});
finder.on("file", function (filename) {
if (path.extname(filename) === ".js") {
var modulesRequired = getModulesRequiredFromFilename(filename);
if (util.isError(modulesRequired)) {
invalidFiles[filename] = modulesRequired;
} else {
modulesRequired = modulesRequired.map(function (module) {
return module.replace ? module.replace(/\/.*$/, '') : module;
});
var toCompare = _.difference(modulesRequired, ignorePackages);
var diff = _.difference(toCompare, deps);
diff.forEach(function(item){
missing[item] = filename;
});
//missing = _.uniq(missing.concat(diff));
//console.log(missing);
//deps = _.difference(modulesRequired, deps);
//devDeps = _.difference(modulesRequired, devDeps);
}
}
});
finder.on("end", function () {
deferred.resolve(q.allSettled(directoryPromises).then(function(directoryResults) {
_.each(directoryResults, function(result) {
if (result.state === 'fulfilled') {
invalidFiles = _.merge(invalidFiles, result.value.invalidFiles, {});
deps = _.intersection(deps, result.value.dependencies);
}
});
return {
missing: missing,
dependencies: deps,
devDependencies: [],
invalidFiles: invalidFiles
};
}));
});
return deferred.promise;
}
function depCheck(rootDir, options, cb) {
var pkg = require(path.join(rootDir, 'package.json'));
var rcPath = path.join(rootDir, '.missingdepsrc');
var rc = (fs.existsSync(rcPath)) ? JSON.parse(fs.readFileSync(rcPath, 'utf-8')) : {};
var deps = Object.keys(merge(pkg.dependencies, pkg.devDependencies));
var ignoreDirs = _([
'.git',
'.svn',
'.hg',
'.idea',
'node_modules',
'bower_components'
])
.concat(options.ignoreDirs)
.concat(rc.ignore)
.flatten()
.unique()
.valueOf();
var ignorePackages = _((rc.ignoreModules) ? rc.ignoreModules : [])
.concat(options.ignorePackages)
.concat([
'.',
'..',
"child_process",
"cluster",
"crypto",
"dns",
"domain",
"events",
"fs",
"http",
"https",
"net",
"os",
"path",
"punycode",
"querystring",
"readline",
"smalloc",
"stream",
"string_decoder",
"tls",
"dgram",
"url",
"util",
"vm",
"zlib"
])
.flatten()
.unique()
.valueOf();
function isIgnored(dependency) {
return _.any(options.ignoreMatches, function(match) {
return minimatch(dependency, match);
});
}
function hasBin(dependency) {
try {
var depPkg = require(path.join(rootDir, "node_modules", dependency, "package.json"));
return _.has(depPkg, 'bin');
} catch (e) {}
}
function filterDependencies(dependencies) {
return _(dependencies)
.keys()
.reject(hasBin)
.reject(isIgnored)
.valueOf();
}
return checkDirectory(rootDir, ignoreDirs, deps, ignorePackages)
.then(cb)
.done();
}
module.exports = depCheck;