-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole-inject.js
More file actions
38 lines (29 loc) · 1.21 KB
/
console-inject.js
File metadata and controls
38 lines (29 loc) · 1.21 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
var eventstream = require("event-stream");
var through = require("through2");
var path = require("path");
var cons = require("./client")
module.exports = function (params) {
var options = params || {};
options.port = options.port || 45600;
var snippet = "\n<script>(" + cons.toString() + ")(window, " + options.port + ", {});</script>\n";
var stream = through.obj(function (file, enc, callback) {
var insertSnippetIfMatching = function (line) {
return new Buffer(line.toString(enc).replace(/<\/body>/, function(match) {
return snippet + match;
}));
};
if (file.isNull() || path.extname(file.path) !== ".html") {
// If it is not a file or not HTML - do nothing
} else if (file.contents instanceof Buffer) {
file.contents = insertSnippetIfMatching(file.contents);
} else {
file.contents = file.contents.pipe(eventstream.split("\n")).pipe(through(function (line, enc, callback) {
this.push(insertSnippetIfMatching(line));
callback();
})).pipe(eventstream.join("\n"));
}
this.push(file);
return callback();
});
return stream;
};