forked from pmcalabrese/gulp-logwarn
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
76 lines (62 loc) · 1.85 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
var es = require('event-stream');
var colors = require('colors');
var PluginError = require('gulp-util').PluginError;
var through = require('through2')
var logWarn = function (subString, opt) {
var substrings = typeof subString === "undefined" ? [] : subString;
opt = opt || {};
function occurrences(file, cb) {
// file.contents = new Buffer(String(file.contents).toString());
if (file.isNull()) {
// Do nothing if no contents
}
if (file.isBuffer()) {
var string = file.contents.toString();
string = string.toString('utf8').split(/\r\n|[\n\r\u0085\u2028\u2029]/g);
var message = "";
var m = 0;
var n = 0, pos = 0;
var sc = 0;
while (sc < string.length) {
for (var i = substrings.length - 1; i >= 0; i--) {
var step = substrings[i].length;
pos = string[sc].indexOf(substrings[i], pos);
if (pos >= 0) {
n++;
message = message + " " + "[" + (sc + 1) + "] " + substrings[i] + " ,\n";
}
;
}
;
sc++;
}
m = m + n;
finalMessage = file.path + " (" + m + ")\n" + message;
if (m === 0) {
if (opt && opt.logLevel !== 'warn') {
console.log(finalMessage[opt.color || 'green']);
}
} else {
file.failures = finalMessage;
}
message = null;
cb(null, file);
}
}
return es.map(occurrences);
};
logWarn.report_failures = function () {
var hasError = false;
return through.obj(function (chunk, enc, cb) {
var failures = chunk.failures;
if (failures) {
hasError = true;
console.log(failures["red"]);
}
cb(null, chunk)
}).on('end', function () {
if (hasError)
return this.emit("error", new PluginError("gulp-logWarn", "you have devil keywords in you code "));
})
};
module.exports = logWarn;