-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
78 lines (65 loc) · 1.65 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
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var chalk = require('chalk');
var hookStdio = require('hook-stdio');
var extend = require('util-extend');
var Detector = require('buddy.js/lib/detector');
var reporters = require('buddy.js/lib/reporters');
function buddySucceeded(output, reporter) {
switch(reporter) {
case 'simple':
return (output.indexOf('No magic numbers found') > -1);
case 'detailed':
return (output.indexOf('No magic numbers found') > -1);
case 'json':
return output === '[]';
default:
return true;
}
}
module.exports = function(options) {
var paths = [];
options = extend({
ignore: [0, 1],
disableIgnore: false,
detectObjects: false,
reporter: 'simple',
enforceConst: false,
noColor: false
}, options);
options.ignore = options.disableIgnore ? [] : options.ignore;
if (options.noColor) {
chalk.enabled = false;
}
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
paths.push(file.path);
cb(null, file);
}, function (cb) {
if (paths.length === 0) {
cb();
return;
}
var detector = new Detector(paths, options);
var reporter = new reporters[options.reporter](detector);
var output = '';
var unhook = hookStdio.stdout(function(string) {
output += string;
}, true);
detector.run().then(function() {
unhook();
if (!buddySucceeded(output, options.reporter)) {
this.emit('error', new gutil.PluginError('gulp-buddy.js', 'buddy.js failed', {
showStack: false
}));
}
cb();
}.bind(this), function(e) {
throw new gutil.PluginError('gulp-buddy.js', e);
});
});
};