-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (37 loc) · 1000 Bytes
/
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
var Transform = require('stream').Transform,
util = require('util'),
retrieve = require('./utils/retrieve'),
toJsString = require('./utils/toJsString');
module.exports = function (filename, opts) {
Object.assign({}, opts);
return new Haribotify(filename, opts);
};
util.inherits(Haribotify, Transform);
function Haribotify (filename, opts) {
Transform.call(this);
this._filename = filename;
this._data = '';
this._opts = opts;
}
Haribotify.prototype._transform = function (buf, enc, callback) {
this._data += buf;
callback();
};
Haribotify.prototype._flush = function (callback) {
if (this._data.indexOf('<') !== 0) {
this.push(this._data);
callback();
return;
}
try {
var text = retrieve(this._filename, this._opts.format);
var jsText = toJsString(text);
var result = 'module.exports = ' + jsText + ';'
this.emit('haribotify', result);
this.push(result);
} catch (err) {
this.emit('error', err);
return;
}
callback();
};