-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter.js
56 lines (43 loc) · 1.08 KB
/
exporter.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
var util = require('util');
var events = require('events');
var csv = require('csv');
var fs = require('fs');
var stream = require('stream');
function Exporter(path) {
this.path = path;
this.emit('finished', false);
}
util.inherits(Exporter, events.EventEmitter);
Exporter.prototype.toStream = function(data) {
var s = new stream.Readable();
s._data = data.toString();
s._read = function(n) {
var chunk;
n = (n == null || n === -1) ? undefined : n;
chunk = this._data.slice(0, n);
this._data = this._data.slice(n);
if (chunk == "") {
return this.emit('end');
} else {
this.push(chunk);
this.emit('data', chunk);
return chunk;
}
};
return s;
}
Exporter.prototype.doWrite = function(data) {
var $self = this;
fs.writeFile(this.path, data, function(err) {
if (err) { return $self.emit('error', err); }
$self.emit('finished', true);
})
};
Exporter.prototype.writeToFile = function(data) {
var $self = this;
csv.stringify(data, function(err, output) {
if (err) { return $self.emit('error', err); }
$self.doWrite(output);
});
}
module.exports = Exporter;