-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (45 loc) · 1.19 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
'use strict';
var through = require('through2');
var PluginError = require('gulp-util').PluginError;
var PLUGIN_NAME = 'gulp-mask';
module.exports = function (open, close, defaultOpened) {
function tester(key) {
if (typeof key === 'string') {
return function (line) {
return line.trim() === key;
}
}
return function (line) {
return key.test(line);
}
}
if (!open) {
throw new PluginError(PLUGIN_NAME, 'Mask key required');
}
var shouldOpen = tester(open);
var shouldClose = close && tester(close);
return through.obj(function (file, enc, cb) {
var opened = defaultOpened;
if (file.isNull()) {
return cb();
}
if (file.isStream()) {
throw new PluginError(PLUGIN_NAME, 'Streaming not supported');
}
var lines = file.contents.toString('utf8').split("\n");
for (var i = 0; i < lines.length; i++) {
if (!opened) {
opened = shouldOpen(lines[i]);
lines[i] = "";
continue;
}
if (shouldClose && shouldClose(lines[i])) {
opened = false;
lines[i] = "";
}
}
file.contents = new Buffer(lines.join("\n"), 'utf8');
this.push(file);
cb();
});
};