refactor: rewrite on typescript

This commit is contained in:
2026-10-13 21:11:57 +03:00
parent be54ce5d25
commit f735be8bf9
40 changed files with 2333 additions and 2175 deletions
+42
View File
@@ -0,0 +1,42 @@
import colors from "ansi-colors";
const _colors = {
"[DEBUG]": colors.white,
"[INFO]": colors.cyan,
"[ERROR]": colors.red,
"Called from ": colors.red,
"[WARNING]": colors.yellow,
};
export type Color = (v: string) => string;
export const getColor = (line: string): Color | null => {
for (const [tag, color] of Object.entries(_colors)) {
if (line.indexOf(tag) > -1) {
return color;
}
}
return null; // colors.reset;
};
export class Debug {
public color: Color;
constructor() {
this.color = colors.white;
this.log = this.log.bind(this);
}
log(line: string) {
const newColor = getColor(line);
if (newColor) this.color = newColor;
if (line[0] === "\t" || line.startsWith("Called from ")) {
console.log(this.color(line));
} else {
const result = line.split(" ");
console.log(colors.gray(result.slice(0, 4).join(" ")) + " " + this.color(result.slice(4).join(" ")));
}
}
}
export const DEBUG = new Debug();
+19
View File
@@ -0,0 +1,19 @@
import path from "path";
export class Env {
static set(key: string, value: string) {
process.env[key] = value;
}
static get(key: string) {
return process.env[key];
}
static addPath(value: string, key = "PATH") {
if (!process.env[key]) {
process.env[key] = value;
} else if (process.env[key].split(path.delimiter).indexOf(value) === -1) {
process.env[key] = [process.env[key], value].join(path.delimiter);
}
}
}
+31
View File
@@ -0,0 +1,31 @@
import async from "async";
import child_process from "child_process";
import { Log } from "./log";
const log = new Log("exec");
const queue = async.queue((task: any, done: any) => {
log.d("*%s*", task.command);
//process.chdir(task.dir);
child_process.exec(task.command, { cwd: task.dir, maxBuffer: 1024 * 10000 }, (err, stdout, stderr) => {
if (err) {
log.v("!%s!", err);
task.failure("\n" + (stderr || stdout || err));
} else {
log.v("%s", stdout);
task.success({ stdout: stdout, stderr: stderr });
}
done();
});
});
export default (dir: string, command: string) => {
return new Promise((success, failure) => {
queue.push({
dir: dir,
command: command,
success: success,
failure: failure,
});
});
};
+61
View File
@@ -0,0 +1,61 @@
import colors from "ansi-colors";
import timestamp from "time-stamp";
const Level = {
VERBOSE: -1,
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
};
const level = process.argv.indexOf("--verbose") > -1 ? Level.VERBOSE : Level.INFO;
export class Log {
static level = level;
static colorize(pattern: string) {
for (const item of [
["_", colors.cyan],
["\\*", colors.magenta],
["!", colors.red],
]) {
const r = new RegExp(item[0] + "(.*?)" + item[0], "g");
pattern = pattern.replace(r, item[1]("$1"));
}
return pattern;
}
static write(level: number, tag: string, pattern: string, ...args: any) {
if (level >= this.level) {
const message = `[${colors.gray(timestamp("HH:mm:ss"))}] ${colors.green(tag)} ${this.colorize(pattern)}`;
console.log(message, ...args);
}
}
public tag: string;
constructor(tag: string) {
this.tag = tag;
}
v(...args: any) {
Log.write(-1, this.tag, ...args);
}
d(...args: any) {
Log.write(0, this.tag, ...args);
}
i(...args: any) {
Log.write(1, this.tag, ...args);
}
w(...args: any) {
Log.write(2, this.tag, ...args);
}
e(...args: any) {
Log.write(3, this.tag, ...args);
}
}
+28
View File
@@ -0,0 +1,28 @@
import os from "os";
export class System {
static get isWindows() {
return os.type() === "Windows_NT";
}
static get isLinux() {
return os.type() === "Linux";
}
static get os() {
return this.isWindows ? "windows" : this.isLinux ? "linux" : undefined;
}
static get archInt() {
if (os.arch() === "ia32") return 32;
if (os.arch() === "x64") return 64;
}
static get isArch32() {
return this.archInt === 32;
}
static get isArch64() {
return this.archInt === 64;
}
}
+68
View File
@@ -0,0 +1,68 @@
import { Transform, Writable } 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)));
}
}
dispose() {
if (this.tail) {
this.tail.unwatch();
}
this.contents.destroy();
}
}
export class StringWritable extends Writable {
constructor(handler: any, options?: any) {
super(options);
this.handler = handler;
const state = this._writableState;
this._decoder = new StringDecoder(state.defaultEncoding);
this.data = "";
}
_write(chunk, encoding, callback) {
if (encoding === "buffer") {
chunk = this._decoder.write(chunk);
for (const line of chunk.split("\n"))
if (line.length) {
this.handler(line);
}
}
this.data += chunk;
callback();
}
_final(callback) {
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));
}
this.push(file);
callback();
});
};