[tail] added TailVinyl class

This commit is contained in:
2018-05-04 16:32:49 +03:00
parent bf426a0d2e
commit d3f8f4940c
2 changed files with 28 additions and 7 deletions

View File

@@ -1,7 +1,25 @@
const through = require('through2');
const {Tail} = require('tail');
const {Writable} = require('stream');
const {StringDecoder} = require('string_decoder');
const Vinyl = require('vinyl');
const {Tail} = require('tail');
const {Transform} = require('stream');
class TailVinyl extends Vinyl {
constructor(params) {
params.contents = new Transform();
super(params);
this.tail = new Tail(params.path);
this.tail.on('line', data => this.contents.write(data + '\n'));
this.tail.on('error', error => this.contents.write('error: ' + error));
}
dispose() {
this.tail.unwatch();
}
}
class StringWritable extends Writable {
@@ -32,17 +50,15 @@ class StringWritable extends Writable {
}
module.exports = (handler) => {
return through.obj(function (file, enc, callback) {
if (file.contents && file.contents.pipe) {
file.contents.pipe(new StringWritable(handler));
} else {
const tail = new Tail(file.path);
tail.on('line', data => handler(data));
tail.on('error', error => handler('[ERROR]: ', error));
this.on('end', () => tail.unwatch());
}
this.push(file);
callback();
});
};
module.exports.TailVinyl = TailVinyl;