Compare commits

..
2 Commits
Author SHA1 Message Date
shmyga 188fcc8e4c refactor(log): refactor log and output
test / test (push) Failing after 5m51s
2026-10-26 22:22:16 +03:00
shmyga c8720102f9 fix: fix command options 2026-10-26 22:21:53 +03:00
7 changed files with 128 additions and 89 deletions
-42
View File
@@ -1,42 +0,0 @@
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();
+2 -2
View File
@@ -1,8 +1,8 @@
import async from "async";
import child_process from "child_process";
import { Log } from "./log";
import { Logger } from "./log";
const log = new Log("exec");
const log = new Logger("exec");
const queue = async.queue((task: any, done: any) => {
log.d("*%s*", task.command);
+53 -30
View File
@@ -1,63 +1,86 @@
import colors from "ansi-colors";
const Level = {
VERBOSE: -1,
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
};
export enum Level {
VERBOSE = -1,
DEBUG = 0,
INFO = 1,
WARNING = 2,
ERROR = 3,
}
const level = process.argv.indexOf("--verbose") > -1 ? Level.VERBOSE : Level.INFO;
export const DEFAULT_LEVEL: Level = process.argv.indexOf("--verbose") > -1 ? Level.VERBOSE : Level.INFO;
const formatTime = (date: Date): string => date.toTimeString().split(" ")[0];
export type FormatPattern = [string, colors.StyleFunction];
export class Log {
static level = level;
static colorPatterns: [string, (m: string) => string][] = [
export const DEFAULT_PATTERNS: FormatPattern[] = [
["_", colors.cyan],
["\\*", colors.magenta],
["!", colors.red],
];
static colorize(pattern: string) {
for (const item of this.colorPatterns) {
export class Formatter {
constructor(
private patterns: FormatPattern[] = DEFAULT_PATTERNS,
private time: boolean = true,
) {}
colorize(text: string): string {
for (const item of this.patterns) {
const r = new RegExp(item[0] + "(.*?)" + item[0], "g");
pattern = pattern.replace(r, item[1]("$1"));
text = text.replace(r, item[1]("$1"));
}
return pattern;
return text;
}
static write(level: number, tag: string, pattern: string, args: any[]) {
buildTime(): string {
return new Date().toTimeString().split(" ")[0];
}
format(message: string, tag: string, level: Level): string {
const values = [];
if (this.time) {
values.push(`[${colors.gray(this.buildTime())}]`);
}
if (tag) {
values.push(colors.green(tag));
}
values.push(this.colorize(message));
return values.join(" ");
}
}
export const DEFAULT_FORMATTER = new Formatter();
export class Logger {
constructor(
private tag: string,
private level: Level = DEFAULT_LEVEL,
private formatter: Formatter = DEFAULT_FORMATTER,
) {}
write(level: Level, message: string, args: any[]) {
if (level >= this.level) {
const message = `[${colors.gray(formatTime(new Date()))}] ${colors.green(tag)} ${this.colorize(pattern)}`;
console.log(message, ...args);
console.log(this.formatter.format(message, this.tag, level), ...args);
}
}
public tag: string;
constructor(tag: string) {
this.tag = tag;
}
v(message: string, ...args: any) {
Log.write(-1, this.tag, message, args);
this.write(Level.VERBOSE, message, args);
}
d(message: string, ...args: any) {
Log.write(0, this.tag, message, args);
this.write(Level.DEBUG, message, args);
}
i(message: string, ...args: any) {
Log.write(1, this.tag, message, args);
this.write(Level.INFO, message, args);
}
w(message: string, ...args: any) {
Log.write(2, this.tag, message, args);
this.write(Level.WARNING, message, args);
}
e(message: string, ...args: any) {
Log.write(3, this.tag, message, args);
this.write(Level.ERROR, message, args);
}
}
+55
View File
@@ -0,0 +1,55 @@
import colors from "ansi-colors";
import { Formatter, Level, Logger } from "./log";
export class OutputFormatter extends Formatter {
private colorMap: Record<Level, colors.StyleFunction> = {
[Level.VERBOSE]: colors.white,
[Level.DEBUG]: colors.white,
[Level.INFO]: colors.cyan,
[Level.ERROR]: colors.red,
[Level.WARNING]: colors.yellow,
};
format(message: string, tag: string, level: Level): string {
const color = this.colorMap[level];
if (message[0] === "\t" || message.startsWith("Called from ")) {
return color(message);
} else {
const result = message.split(" ");
return colors.gray(result.slice(0, 4).join(" ")) + " " + color(result.slice(4).join(" "));
}
}
}
export const DEFAULT_OUTPUT_FORMATTER = new OutputFormatter();
export class OutputLogger extends Logger {
private levelMap: Record<string, Level> = {
"[DEBUG]": Level.DEBUG,
"[INFO]": Level.INFO,
"[ERROR]": Level.ERROR,
"Called from ": Level.ERROR,
"[WARNING]": Level.WARNING,
};
constructor() {
super("", Level.VERBOSE, DEFAULT_OUTPUT_FORMATTER);
}
detectLevel(line: string): number {
for (const [tag, color] of Object.entries(this.levelMap)) {
if (line.indexOf(tag) > -1) {
return color;
}
}
return Level.DEBUG;
}
log(line: string) {
this.write(this.detectLevel(line), line, []);
}
}
export const OUTPUT_LOGGER = new OutputLogger();
export default (line: string) => OUTPUT_LOGGER.log(line);
+3 -3
View File
@@ -1,7 +1,7 @@
import os from "os";
import path from "path";
import { Downloader } from "../common/download";
import { Log } from "../common/log";
import { Logger } from "../common/log";
export class Sdk {
static _dir: string;
@@ -20,12 +20,12 @@ export class Sdk {
public name: string;
public version: string;
public log: Log;
public log: Logger;
public path: string;
constructor(name: string, version: string) {
this.name = name;
this.log = new Log(name);
this.log = new Logger(name);
this.version = version;
this.path = Sdk.path(name, version);
}
+2 -2
View File
@@ -1,6 +1,6 @@
import gulp from "gulp";
import rename from "gulp-rename";
import { DEBUG } from "../../common/debug";
import output from "../../common/output";
import { tail } from "../../common/tail";
import { Config } from "../../core/config";
import { Target } from "./target";
@@ -18,7 +18,7 @@ export class Runner extends Target {
}
log(stream: any) {
return stream.pipe(tail(DEBUG.log)).pipe(rename("out.log")).pipe(gulp.dest(this.targetPath));
return stream.pipe(tail(output)).pipe(rename("out.log")).pipe(gulp.dest(this.targetPath));
}
static register(platform: string, runner: RunnerFactory) {
+5 -2
View File
@@ -14,12 +14,15 @@ export class Command {
constructor(command: string, options?: any) {
this.command = command;
this.isWindows = process.platform.lastIndexOf("win") === 0;
this.options = Object.assign(options || {}, {
this.options = Object.assign(
{
cwd: process.cwd(),
env: process.env,
verbosity: options && options.silent ? 1 : 2,
usePowerShell: false,
});
},
options || {},
);
let previousPath;
previousPath = this.options.env.PATH;