Files
UDN/utility.ts
T

50 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-07-05 12:48:42 +02:00
import Colors from "colors";
2024-07-05 16:04:12 +02:00
import Fs from "fs/promises";
import { WebSocketMessage } from "./websocket-handler";
2024-07-05 12:48:42 +02:00
2024-07-05 16:04:12 +02:00
// CONFIG
const indent = 25;
// METHODS
2024-07-05 00:16:22 +02:00
export function guardStringNotEmpty(
data: any,
cb: (data: string) => void
): void {
if (typeof data != "string" || data == "") return;
2024-07-05 00:15:00 +02:00
cb(data);
}
2024-07-05 12:48:42 +02:00
2024-07-05 16:04:12 +02:00
// writing
async function writeToLogFile(message: string): Promise<void> {
await Fs.appendFile("logs", `${message}\n`);
}
export function writeInfo(label: string, value: string): void {
console.log(`${label.padEnd(indent, " ")} ${Colors.bold.green(value)}`);
}
export function writeStat(label: string, value: number): void {
console.log(
`${label.padEnd(indent, " ")} ${Colors.bold.blue(value.toString())}`
);
}
export function writeError(message: string): void {
console.log(Colors.bold.red(message));
writeToLogFile(message);
}
export function writeSuccess(message: string): void {
console.log(Colors.bold.green(message));
writeToLogFile(message);
}
// messages
export function stringifyMessage(messageObject: WebSocketMessage): string {
return JSON.stringify(messageObject);
2024-07-05 13:17:19 +02:00
}
2024-07-05 16:04:12 +02:00
export function parseMessage(messageString: string): Object {
return JSON.parse(messageString);
2024-07-05 12:48:42 +02:00
}