big update
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
const through = require('through2');
|
||||
const colors = require('ansi-colors');
|
||||
const log = require('fancy-log');
|
||||
|
||||
const TAG = colors.green('[tail]');
|
||||
|
||||
|
||||
const { Writable } = require('stream');
|
||||
const { StringDecoder } = require('string_decoder');
|
||||
const {Tail} = require('tail');
|
||||
const {Writable} = require('stream');
|
||||
const {StringDecoder} = require('string_decoder');
|
||||
|
||||
|
||||
class StringWritable extends Writable {
|
||||
|
||||
constructor(handler, options) {
|
||||
super(options);
|
||||
this.handler = handler;
|
||||
@@ -17,6 +13,7 @@ class StringWritable extends Writable {
|
||||
this._decoder = new StringDecoder(state.defaultEncoding);
|
||||
this.data = '';
|
||||
}
|
||||
|
||||
_write(chunk, encoding, callback) {
|
||||
if (encoding === 'buffer') {
|
||||
chunk = this._decoder.write(chunk);
|
||||
@@ -27,6 +24,7 @@ class StringWritable extends Writable {
|
||||
this.data += chunk;
|
||||
callback();
|
||||
}
|
||||
|
||||
_final(callback) {
|
||||
this.data += this._decoder.end();
|
||||
callback();
|
||||
@@ -36,7 +34,13 @@ class StringWritable extends Writable {
|
||||
|
||||
module.exports = (handler) => {
|
||||
return through.obj(function (file, enc, callback) {
|
||||
file.contents.pipe(new StringWritable(handler));
|
||||
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.push(file);
|
||||
callback();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user