-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (34 loc) · 1.02 KB
/
Copy pathindex.js
File metadata and controls
42 lines (34 loc) · 1.02 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
var es = require('event-stream'),
gutil = require('gulp-util');
module.exports = function (opt) {
function gulpInsertLines(file) {
if (file.isNull()) return this.emit('data', file);
if (file.isStream()) return this.emit('error', new Error("gulp-insert-lines: Streaming not supported"));
var str = file.contents.toString('utf8');
var line, i, lines;
var newLines = [];
lines = str.split(/\r\n|\r|\n/g);
for (i = 0; i < lines.length; i++) {
if (opt.after) {
if (lines[i].match(opt.after)) {
newLines.push(lines[i]);
newLines.push(opt.lineAfter);
} else {
newLines.push(lines[i]);
}
}
if (opt.before) {
if (lines[i].match(opt.before)) {
newLines.push(opt.lineBefore);
newLines.push(lines[i]);
} else {
newLines.push(lines[i]);
}
}
}
str = newLines.join('\n');
file.contents = new Buffer(str);
this.emit('data', file);
}
return es.through(gulpInsertLines);
};