refactor: refork tail module

This commit is contained in:
2026-10-22 12:04:40 +03:00
parent a421d56ec1
commit 84d905dbb3
12 changed files with 1047 additions and 2036 deletions
+733 -1696
View File
File diff suppressed because it is too large Load Diff
+13 -8
View File
@@ -38,39 +38,44 @@
"got": "^16.1.0",
"gulp": "^5.0.1",
"gulp-clean": "^0.4.0",
"gulp-connect": "^5.7.0",
"gulp-debian": "^0.1.9",
"gulp-gzip": "^1.4.2",
"gulp-rename": "^1.2.2",
"gulp-rename": "^2.1.0",
"gulp-spawn": "^0.4.0",
"gulp-tar": "^3.0.0",
"gulp-webserver": "^0.9.1",
"gulp-zip": "^5.0.0",
"lodash.defaults": "^4.2.0",
"lodash.template": "^4.4.0",
"plugin-error": "^1.0.1",
"promise-streams": "^2.1.1",
"tail": "^1.2.3",
"tail": "^2.2.6",
"tar": "^7.5.22",
"through2": "^2.0.3",
"through2": "^5.0.12",
"tmp-file": "^2.0.1",
"unzip-stream": "^0.3.4",
"vinyl": "^2.1.0",
"vinyl-fs": "^3.0.2"
"vinyl": "^3.0.1",
"vinyl-fs": "^4.0.2"
},
"devDependencies": {
"@types/async": "^3.2.26",
"@types/cli-progress": "^3.11.6",
"@types/fs-extra": "^11.0.4",
"@types/gulp": "^4.0.18",
"@types/gulp-connect": "^5.0.10",
"@types/gulp-rename": "^2.0.7",
"@types/lodash.template": "^4.5.3",
"@types/node": "^26.6.5",
"@types/tail": "^2.2.3",
"@types/tar": "^7.0.87",
"@types/through2": "^5.0.0",
"@types/unzip-stream": "^0.3.4",
"@types/vinyl-fs": "^3.0.8",
"eslint": "^10.12.0",
"eslint": "^10.13.0",
"prettier": "^3.9.10",
"tsdown": "0.22.14",
"typescript": "^7.0.2"
},
"inlinedDependencies": {
"lodash": "4.18.1"
}
}
+28 -45
View File
@@ -1,48 +1,25 @@
import { Transform, Writable } from "stream";
import { Transform, Writable, type TransformCallback, type WritableOptions } from "stream";
import { StringDecoder } from "string_decoder";
import { Tail } from "tail";
import through from "through2";
import Vinyl from "vinyl";
export class TailVinyl extends Vinyl {
constructor(params: any) {
params.contents = new Transform();
super(params);
if (params.path) {
this.tail = new Tail(params.path);
(this.tail.on("line", (line) => {
if (params.handler) {
line = params.handler(line);
}
if (line) {
this.contents.write(line + "\n");
}
}),
this.tail.on("error", (error: any) => this.contents.write("error: " + error)));
}
}
export type TailHandler = (line: string) => void;
dispose() {
if (this.tail) {
this.tail.unwatch();
}
this.contents.destroy();
}
}
export class HandlerWriter extends Writable {
private handler: any;
private decoder: StringDecoder;
private data: string;
export class StringWritable extends Writable {
constructor(handler: any, options?: any) {
constructor(handler: TailHandler, options?: WritableOptions) {
if (!options) options = {};
options.objectMode = true;
super(options);
this.handler = handler;
const state = this._writableState;
this._decoder = new StringDecoder(state.defaultEncoding);
this.decoder = new StringDecoder(options?.defaultEncoding);
this.data = "";
}
_write(chunk, encoding, callback) {
if (encoding === "buffer") {
chunk = this._decoder.write(chunk);
for (const line of chunk.split("\n"))
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
chunk = this.decoder.write(chunk);
for (const line of chunk.split("\n")) {
if (line.length) {
this.handler(line);
}
@@ -51,18 +28,24 @@ export class StringWritable extends Writable {
callback();
}
_final(callback) {
this.data += this._decoder.end();
_final(callback: (error?: Error | null) => void): void {
this.data += this.decoder.end();
callback();
}
}
export const tail = (handler: any) => {
return through.obj(function (file: any, enc: string, callback: any) {
if (file.contents && file.contents.pipe) {
file.contents.pipe(new StringWritable(handler));
export class TailTransform extends Transform {
constructor(private handler: TailHandler) {
super({ objectMode: true });
}
this.push(file);
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void {
if (chunk.contents && chunk.contents.pipe) {
chunk.contents.pipe(new HandlerWriter(this.handler));
}
this.push(chunk);
callback();
});
};
}
}
export const tail = (handler: TailHandler) => new TailTransform(handler);
+65
View File
@@ -0,0 +1,65 @@
import { Transform, type TransformCallback } from "stream";
import { Tail } from "tail";
import Vinyl from "vinyl";
export type WatchHandler = (line: string) => string | undefined;
export interface WatchParams {
source: string;
handler?: WatchHandler;
}
class BufferingTransform extends Transform {
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void {
this.push(chunk);
callback();
}
}
export class TailVinyl extends Vinyl {
private tail?: Tail;
constructor(params: WatchParams) {
super({ path: "out.log", contents: new BufferingTransform() });
if (params.source) {
this.tail = new Tail(params.source);
this.tail.on("line", (line) => {
if (params.handler) {
line = params.handler(line);
}
if (line) {
this.contents.write(line + "\n");
}
});
this.tail.on("error", (error) => this.contents.write("error: " + error));
}
}
clone() {
return new Vinyl({
cwd: this.cwd,
base: this.base,
history: this.history.slice(),
contents: this.contents,
});
}
dispose() {
this.tail?.unwatch();
this.contents.destroy();
}
}
export class WatchVinylTransform extends Transform {
constructor(private params: WatchParams) {
super({ objectMode: true });
}
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void {
const log = new TailVinyl(this.params);
this.on("end", () => log.dispose());
this.push(log);
callback();
}
}
export const watch = (source: string, handler?: WatchHandler) => new WatchVinylTransform({ source, handler });
+2 -2
View File
@@ -5,7 +5,7 @@ import through from "through2";
import { Env } from "../common/env";
import exec from "../common/exec";
import { System } from "../common/system";
import { StringWritable } from "../common/tail";
import { HandlerWriter } from "../common/tail";
import { Downloader } from "../core/download";
import { Sdk } from "../core/sdk";
import { Command } from "../run/command";
@@ -223,7 +223,7 @@ export class Android extends Sdk {
const command = new Command(cmd).exec();
this.push(command);
command.contents.pipe(
new StringWritable((line) => {
new HandlerWriter((line: string) => {
if (line.includes("SDL") && (line.includes("onDestroy()") || line.includes("onStop()"))) {
callback();
throw "Exit"; // ToDo:
+2 -14
View File
@@ -2,10 +2,9 @@ import fs from "fs";
import fse from "fs-extra";
import os from "os";
import path from "path";
import through from "through2";
import { Env } from "../common/env";
import { System } from "../common/system";
import { TailVinyl } from "../common/tail";
import { watch } from "../common/watch";
import { Sdk } from "../core/sdk";
import run from "../run";
@@ -120,17 +119,6 @@ export class FlashPlayer extends Sdk {
this.log.i("_run_ *%s*", filename);
FlashPlayer.trust(filename);
FlashPlayer.enableLog();
return run(`${this.flashPlayerBin} ${filename}`, params)
.exec()
.pipe(
through.obj(function (file: any, enc: string, callback: any) {
const log = new TailVinyl({
path: FlashPlayer.log,
});
this.on("end", () => log.dispose());
this.push(log);
callback();
}),
);
return run(`${this.flashPlayerBin} ${filename}`, params).exec().pipe(watch(FlashPlayer.log));
}
}
+6 -3
View File
@@ -77,7 +77,10 @@ export class Haxe extends Sdk {
prepare() {
return Promise.all([this.neko.prepare(), super.prepare()]).then(() => {
fs.copyFileSync(path.resolve(import.meta.dirname, "..", "template/haxe/activate"), path.resolve(this.path, "activate"));
fs.copyFileSync(
path.resolve(import.meta.dirname, "..", "template/haxe/activate"),
path.resolve(this.path, "activate"),
);
});
}
@@ -128,7 +131,7 @@ export class Haxe extends Sdk {
flash: "*.swf",
}[platform] || "**/*";
return this.haxelib(args).then(() => {
let r = vfs.src(`${target}/${result}`);
let r = vfs.src(`${target}/${result}`, { encoding: false });
if (platform === "android") {
r = r.pipe(rename(`${config.meta.filename}_${config.meta.version}.apk`));
}
@@ -175,7 +178,7 @@ export class Haxe extends Sdk {
cpp: `${config.meta.filename}/${config.main.split(".").pop()}${debug ? "-debug" : ""}`,
}[platform] || "**/*";
return this.haxe(args).then(() => {
let r = vfs.src(`${target}/${result}`);
let r = vfs.src(`${target}/${result}`, { encoding: false });
if (platform === "cpp") {
r = r.pipe(rename(config.meta.filename));
}
+2 -11
View File
@@ -1,9 +1,8 @@
import os from "os";
import path from "path";
import through from "through2";
import { Env } from "../common/env";
import { System } from "../common/system";
import { TailVinyl } from "../common/tail";
import { watch } from "../common/watch";
import run from "../run";
import { FlashPlayer } from "./flashplayer";
@@ -55,18 +54,10 @@ export class RufflePlayer extends FlashPlayer {
return run(`${this.flashPlayerBin} ${filename}`, params)
.exec()
.pipe(
through.obj(function (file: any, enc: string, callback: any) {
const log = new TailVinyl({
path: RufflePlayer.log,
handler: (line: string) => {
watch(RufflePlayer.log, (line: string) => {
if (line.includes("avm_trace")) {
return line.split("avm_trace: ").pop();
}
},
});
this.on("end", () => log.dispose());
this.push(log);
callback();
}),
);
}
+4 -8
View File
@@ -1,5 +1,4 @@
import gulp from "gulp";
import webserver from "gulp-webserver";
import connect from "gulp-connect";
import { Config } from "../../../core/config";
import { Platform } from "../../../core/type";
import { Runner } from "../../core/runner";
@@ -10,15 +9,12 @@ export class Html5Runner extends Runner {
}
call() {
return gulp.src(this.targetPath).pipe(
webserver({
// TODO: config?
return connect.server({
root: this.targetPath,
host: "0.0.0.0",
port: 3000,
open: true,
fallback: "index.html",
}),
);
});
}
}
+53 -77
View File
@@ -1,83 +1,61 @@
var cp = require('child_process');
var path = require('path');
var stream = require('stream');
var util = require('util');
var defaults = require('lodash.defaults');
var applyTemplate = require('lodash.template');
var Vinyl = require('vinyl');
var colors = require('ansi-colors');
import colors from "ansi-colors";
import cp from "child_process";
import { defaults, template } from "lodash";
import path from "path";
import stream from "stream";
import util from "util";
import Vinyl from "vinyl";
/**
* Creates a new `gulp-run` command.
*
* @param {string} command
* @param {object} options
*/
export function Command(command, options?) {
var previousPath;
export class Command {
private command: string;
private isWindows: boolean;
private options: any;
constructor(command: string, options?: any) {
this.command = command;
// We're on Windows if `process.platform` starts with "win", i.e. "win32".
this.isWindows = (process.platform.lastIndexOf('win') === 0);
// the cwd and environment of the command are the same as the main node
// process by default.
this.isWindows = process.platform.lastIndexOf("win") === 0;
this.options = defaults(options || {}, {
cwd: process.cwd(),
env: process.env,
verbosity: (options && options.silent) ? 1 : 2,
usePowerShell: false
verbosity: options && options.silent ? 1 : 2,
usePowerShell: false,
});
// include node_modules/.bin on the path when we execute the command.
let previousPath;
previousPath = this.options.env.PATH;
this.options.env.PATH = path.join(this.options.cwd, 'node_modules', '.bin');
this.options.env.PATH = path.join(this.options.cwd, "node_modules", ".bin");
this.options.env.PATH += path.delimiter;
this.options.env.PATH += previousPath;
}
}
/**
* Execute the command, invoking the callback when the command exits.
* Returns a Vinyl file wrapping the command's stdout.
*
* @param {string} stdin
* @param {function} callback
* @return {Stream}
*/
Command.prototype.exec = function exec(stdin, callback) {
var self = this;
var command;
var fileName;
var directory;
var subShell;
var log;
var err;
var stdout;
exec(stdin?: any, callback?: any) {
let fileName;
let directory;
let subShell;
// parse the arguments, both are optional.
// after parsing, stdin is a vinyl file to use as standard input to
// the command (possibly empty), and callback is a function.
if (typeof stdin === 'function') {
if (typeof stdin === "function") {
callback = stdin;
stdin = undefined;
} else if (typeof callback !== 'function') {
} else if (typeof callback !== "function") {
callback = function noop() {};
}
if (!(stdin instanceof Vinyl)) {
fileName = this.command.split(' ')[0];
fileName = this.command.split(" ")[0];
directory = path.join(this.options.cwd, fileName);
if (typeof stdin === 'string') {
if (typeof stdin === "string") {
stdin = new Vinyl({
path: directory,
contents: new Buffer(stdin)
contents: new Buffer(stdin),
});
} else if (stdin instanceof Buffer || stdin instanceof stream.Readable) {
stdin = new Vinyl({
path: directory,
contents: stdin
contents: stdin,
});
} else {
stdin = new Vinyl(stdin);
@@ -91,27 +69,27 @@ Command.prototype.exec = function exec(stdin, callback) {
// execute the command.
// we spawn the command in a subshell, so things like i/o redirection
// just work. e.g. `echo hello world >> ./hello.txt` works as expected.
command = applyTemplate(this.command)({
file: stdin
const command = template(this.command)({
file: stdin,
});
if (this.isWindows && this.options.usePowerShell) {
// windows powershell
subShell = cp.spawn('powershell.exe', ['-NonInteractive', '-NoLogo', '-Command', command], {
subShell = cp.spawn("powershell.exe", ["-NonInteractive", "-NoLogo", "-Command", command], {
env: this.options.env,
cwd: this.options.cwd
cwd: this.options.cwd,
});
} else if (this.isWindows) {
// windows cmd.exe
subShell = cp.spawn('cmd.exe', ['/c', command], {
subShell = cp.spawn("cmd.exe", ["/c", command], {
env: this.options.env,
cwd: this.options.cwd
cwd: this.options.cwd,
});
} else {
// POSIX shell
subShell = cp.spawn('sh', ['-c', command], {
subShell = cp.spawn("sh", ["-c", command], {
env: this.options.env,
cwd: this.options.cwd
cwd: this.options.cwd,
});
}
@@ -123,17 +101,17 @@ Command.prototype.exec = function exec(stdin, callback) {
// prevents overlaping output of multiple commands)
// - if verbosity equals to 1, the command's stderr is buffered as in 2, but
// the stdout is silenced.
log = new stream.PassThrough();
const log = new stream.PassThrough();
function sendLog(context) {
const sendLog = (context: any) => {
var title = util.format(
'$ %s%s',
"$ %s%s",
colors.blue(command),
(self.options.verbosity < 2) ? colors.grey(' # Silenced\n') : '\n'
this.options.verbosity < 2 ? colors.grey(" # Silenced\n") : "\n",
);
context.write(title);
}
};
switch (this.options.verbosity) {
case 3:
@@ -151,19 +129,21 @@ Command.prototype.exec = function exec(stdin, callback) {
}
// setup the cleanup proceedure for when the command finishes.
subShell.once('close', function handleSubShellClose() {
subShell.once("close", function handleSubShellClose() {
// write the buffered output to stdout
var content = log.read();
const content = log.read();
if (content !== null) {
process.stdout.write(content);
}
});
subShell.once('exit', function handleSubShellExit(code) {
subShell.once("exit", function handleSubShellExit(code) {
// report an error if the command exited with a non-zero exit code.
if (code !== 0) {
err = new Error(util.format('Command `%s` exited with code %s', command, code));
const err: Error & { status?: number | null } = new Error(
util.format("Command `%s` exited with code %s", command, code),
);
err.status = code;
return callback(err);
@@ -174,20 +154,16 @@ Command.prototype.exec = function exec(stdin, callback) {
// the file wrapping stdout is as the one wrapping stdin (same metadata)
// with different contents.
stdout = stdin.clone();
const stdout = stdin.clone({ path: "stdout" });
stdout.contents = subShell.stdout.pipe(new stream.PassThrough());
// finally, write the input to the process's stdin.
//stdin.pipe(subShell.stdin); //ToDo:
//stdin.pipe(subShell.stdin); //TODO:
return stdout;
};
}
/**
* Returns the command template.
*
* @return {string}
*/
Command.prototype.toString = function toString() {
toString() {
return this.command;
};
}
}
+14 -61
View File
@@ -1,71 +1,24 @@
/**
* Pipe shell commands in gulp.
* @module gulp-run
*/
import { Transform, type TransformCallback } from "stream";
import { Command } from "./command";
var inherit = require("util").inherits;
var Transform = require("stream").Transform;
export class RunTransform extends Transform {
private command: Command;
/**
* Creates a GulpRunner.
*
* A GulpRunner is a Vinyl transform stream that spawns a child process to
* transform the file. A separate process is spawned to handle each file
* passing through the stream.
*
* @param {string} template
* @param {object} options
*/
function GulpRunner(template, options) {
if (!(this instanceof GulpRunner)) {
return new GulpRunner(template, options);
constructor(template: string, options?: any) {
super({ objectMode: true });
this.command = new Command(template, options || {});
}
this.command = new Command(template, options || {});
Transform.call(this, { objectMode: true });
}
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void {
const stdout = this.command.exec(chunk, callback);
this.push(stdout);
}
/**
* @extends {Stream.Transform}
*/
inherit(GulpRunner, Transform);
/**
* @param {string} file
* @param {string} encoding
* @param {function} callback
* @return {void}
*/
GulpRunner.prototype._transform = function _transform(file, encoding, callback) {
var newfile = this.command.exec(file, callback);
this.push(newfile);
};
/**
* Writes `stdin` to itself and returns itself.
*
* Whenever an object is written into the GulpRunner, a new process is
* spawned taking that data as standard input, and a Vinyl file wrapping the
* process's standard output is pushed downstream.
*
* `stdin` may be a String, Buffer, Readable stream, or Vinyl file.
*
* @param {mixed} stdin
* @param {function} callback
* @return {GulpRunner}
*/
GulpRunner.prototype.exec = function exec(stdin, callback) {
exec(stdin?: any, callback?: any) {
this.write(stdin, callback);
this.end();
return this;
};
}
}
/**
* @static
* @type {Command}
*/
GulpRunner.Command = Command;
export default GulpRunner;
export default (template: string, options?: any) => new RunTransform(template, options);
+14
View File
@@ -0,0 +1,14 @@
declare module "vinyl" {
export class File {
path: string;
cwd: string;
base: string;
contents: any;
stat: any;
history: any[];
constructor(params: any);
clone(options?: any);
}
export default File;
}