-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (46 loc) · 1.25 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
var _ = require('lodash');
var through2 = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
var parse = function(result) {
return result.pop();
};
var transform = function(results) {
var obj = {};
_.forEach(results, function(key) {
obj[key] = null;
});
return JSON.stringify(obj, null, 2);
};
var options = {
parse: parse,
transform: transform,
duplicate: false // don't add twice the same key
};
module.exports = function(opts) {
var params = _.extend({}, options, opts);
var results = [];
if (_.isUndefined(params.regex)) {
throw new PluginError('gulp-regex-json', 'The `regex` option is mandatory');
}
return through2.obj(function(file, encoding, callback) {
var content = file.contents.toString(encoding);
var result;
while ((result = params.regex.exec(content)) !== null) {
var key = params.parse(result);
if (params.duplicate || !_.includes(results, key)) {
results.push(key);
}
}
return callback();
}, function(callback) {
var json = params.transform(results);
var file = new File({
path: params.file,
contents: new Buffer(json)
});
this.push(file);
return callback();
});
};