/
dependency-check.js
44 lines (36 loc) · 1.23 KB
/
dependency-check.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
/*jslint node: true */
'use strict';
module.exports = function (grunt) {
var check = require('dependency-check');
grunt.registerMultiTask('dependency-check', 'Matches used modules to listed dependencies', function () {
var done = this.async();
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
missing: true,
unused: true,
excludeMissingDev: false,
package: '.',
});
check({ path: options.package, entries: this.filesSrc }, function(err, data) {
if (err) {
return grunt.fail.fatal(err);
}
var pkg = data.package;
var deps = data.used;
var results;
if (options.unused) {
results = check.extra(pkg, deps, { excludeDev: options.excludeMissingDev });
if (results.length !== 0) {
grunt.log.error('Modules in package.json not used in code: ' + grunt.log.wordlist(results, { color: 'red' }));
}
}
if (options.missing) {
results = check.missing(pkg, deps);
if (results.length !== 0) {
grunt.fail.fatal('Dependencies not listed in package.json: ' + grunt.log.wordlist(results, { color: 'red' }));
}
}
done();
});
});
};