This repository was archived by the owner on Jan 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (45 loc) · 1.24 KB
/
index.js
File metadata and controls
57 lines (45 loc) · 1.24 KB
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
'use strict';
var gutil = require('gulp-util'),
through = require('through2');
module.exports = function (options) {
var term;
if (!options.term) {
throw new gutil.PluginError('gulp-scan', '`term` required');
}
if (!(options.term instanceof RegExp) &&
!(typeof options.term === 'string' || options.term instanceof RegExp)) {
throw new gutil.PluginError('gulp-scan', '`term` must be a string or RegExp');
}
if (!options.fn) {
throw new gutil.PluginError('gulp-scan', '`fn` required');
}
if (!(options.fn instanceof Function)) {
throw new gutil.PluginError('gulp-scan', '`fn` must be a Function.');
}
term = options.term instanceof RegExp ? options.term : new RegExp(options.term, 'g');
return through.obj(function (file, enc, cb) {
var content,
matches;
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-scan', 'Streaming not supported'));
return;
}
try {
content = file.contents.toString();
matches = content.match(term);
if(matches !== null) {
matches.forEach(function(match) {
options.fn(match, file.clone());
});
}
}
catch (err) {
this.emit('error', new gutil.PluginError('gulp-scan', err));
}
cb(null, file);
});
};