/
index.js
122 lines (101 loc) · 3.18 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
'use strict';
var through = require('through2');
var gutil = require('gulp-util');
var defaults = {
warnList: {
search: [
' req.body.'
],
onFound: function (string, file) {
var error = 'Warning: Your file ' + file.history + ' contains "' + string + '", it should not.';
// eslint-disable-next-line no-console
console.log(error);
}
},
errList: {
search: [
'eval(',
'child_process.exec(',
'setTimeout(',
'setInterval('
],
onFound: function (string, file) {
var error = 'Warning: Your file ' + file.history + ' contains "' + string + '", it should not.';
// eslint-disable-next-line no-console
console.log(error);
}
}
};
/**
* @param {opts} an array of options
*/
function mraudit (opts) {
// Use default module settings, or apply opts which came from the user when
// instantiating this module
var settings;
// Sanity check the options passed to the gulp plugin
// Use plugin defaults, confirm it's an object, or otherwise throw an error
if (!opts) {
settings = defaults;
} else if (opts !== null && typeof opts === 'object') {
settings = opts;
} else {
return new gutil.PluginError('gulp-mraudit', 'expecting an object as parameter to function');
}
return through.obj(function (file, enc, cb) {
// Nothing to do with an empty file
if (file.isNull()) {
cb(null, file);
return;
}
// Not processing streams because we need to search the entire content
// of the file to find the relevant string
if (file.isStream()) {
cb(new gutil.PluginError('gulp-mraudit', 'Streaming not supported'));
return;
}
// Process the warnList
if (settings.hasOwnProperty('warnList') && settings.warnList.hasOwnProperty('search') &&
Object.prototype.toString.call(settings.warnList.search) === '[object Array]') {
findMatch(settings.warnList, file, enc);
}
// Process the errList
if (settings.hasOwnProperty('errList') && settings.errList.hasOwnProperty('search') &&
Object.prototype.toString.call(settings.errList.search) === '[object Array]') {
var match = findMatch(settings.errList, file, enc);
if (match) {
gulpError(match, file, cb);
}
}
cb(null, file);
});
}
function findMatch(list, file, enc) {
// Iterate through the array of strings to find a match,
// upon which we should call the handler callback for displaying
// a console msg
for (var i = 0; i <= list.search.length; i++) {
var match = findString(file.contents.toString(enc), list.search[i]);
if (match) {
if (list.hasOwnProperty('onFound')) {
list.onFound(match, file);
}
return match;
}
}
return false;
}
function findString(haystack, needle) {
if (typeof needle === 'string') {
return haystack.indexOf(needle) !== -1 ? needle : false;
}
if (needle instanceof RegExp) {
return haystack.match(needle) ? needle : false;
}
return false;
}
function gulpError(string, file, cb) {
var error = 'Your file contains "' + string + '", it should not.';
cb(new gutil.PluginError('gulp-mraudit', error));
}
module.exports = mraudit;