From d3f8f4940c48f3ebf9817d0fbe3268dd538f3890 Mon Sep 17 00:00:00 2001 From: shmyga Date: Fri, 4 May 2018 16:32:49 +0300 Subject: [PATCH] [tail] added TailVinyl class --- haxetool/flashplayer.js | 7 ++++++- haxetool/tail.js | 28 ++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/haxetool/flashplayer.js b/haxetool/flashplayer.js index d7d484e..66eba4a 100755 --- a/haxetool/flashplayer.js +++ b/haxetool/flashplayer.js @@ -6,6 +6,7 @@ const through = require('through2'); const Sdk = require('./sdk'); const Vinyl = require('vinyl'); const run = require('../run/index'); +const {TailVinyl} = require('./tail'); class FlashPlayer extends Sdk { @@ -99,7 +100,11 @@ class FlashPlayer extends Sdk { FlashPlayer.enableLog(); return run(`${this.flashPlayerBin} ${filename}`, params).exec() .pipe(through.obj(function (file, enc, callback) { - this.push(new Vinyl({path: FlashPlayer.log})); + const log = new TailVinyl({ + path: FlashPlayer.log, + }); + this.on('end', () => log.dispose()); + this.push(log); callback(); })); } diff --git a/haxetool/tail.js b/haxetool/tail.js index 2eb15d0..eee9d07 100644 --- a/haxetool/tail.js +++ b/haxetool/tail.js @@ -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;