Added logging

This commit is contained in:
Kallum Jones
2022-08-03 16:52:24 +01:00
parent 765920f80c
commit 8a927d2f02
5 changed files with 311 additions and 5 deletions

View File

@ -1,13 +1,18 @@
#!/usr/bin/env node
import { Command } from "commander";
import {Command} from "commander";
import InitCommand from "./commands/init_command.js";
import InstallCommand from "./commands/install_command.js";
import Subcommand from "./commands/subcommand.js";
import Initialiser from "./util/initialiser.js";
import PrintUtils from "./util/print_utils.js";
//import PrettyError from "pretty-error";
import path from "path";
import {Logger, pino} from "pino"
export default class ModManager {
public static logger: Logger | null = null;
private static readonly LOG_FILE: string = path.join(Initialiser.getModManagerFolderPath(), "logs", `${new Date().valueOf()}.log.json`);
private static program: Command = new Command();
private static subcommands: Array<Subcommand> = [
@ -16,8 +21,10 @@ export default class ModManager {
];
static init() {
//const pe = new PrettyError();
//pe.start();
if (Initialiser.isInitialised()) {
this.logger = ModManager.createLogger();
}
this.program
.name('mod-manager')
@ -37,6 +44,16 @@ export default class ModManager {
PrintUtils.error("Mod Manager is not initialised");
}
}
static createLogger(): Logger {
let logger = pino({base: {pid: undefined, hostname: undefined}}, pino.destination({dest: this.LOG_FILE}));
process.on("uncaughtException", error => {
logger.error(error);
setTimeout(() => process.exit(1), 1)
})
return logger;
}
}
ModManager.init();

View File

@ -1,7 +1,8 @@
import { existsSync, mkdirSync, writeFileSync } from "fs";
import {existsSync, mkdirSync, writeFileSync} from "fs";
import path from "path";
import Mods from "../mods/mods.js";
import PrintUtils from "./print_utils.js";
import ModManager from "../mod-manager.js";
export default class Initialiser {
private static readonly MOD_MANAGER_FOLDER = ".mod-manager"
@ -13,6 +14,12 @@ export default class Initialiser {
if (success) {
PrintUtils.success("Sucessfully initialised Mod Manager!");
// Initialise a logger when Mod Manager is initialised
if (ModManager.logger == null) {
ModManager.logger = ModManager.createLogger();
}
} else {
PrintUtils.error("Unable to set up the Mod Manager folder structure");
}
@ -39,6 +46,7 @@ export default class Initialiser {
if (!existsSync(this.getModManagerFolderPath())) {
mkdirSync(this.getModManagerFolderPath());
writeFileSync(Mods.getModFilePath(), "[]");
mkdirSync(path.join(this.getModManagerFolderPath(), "logs"))
return true;
} else {
return false;

View File

@ -1,21 +1,38 @@
import chalk from "chalk";
import ora, { Ora } from "ora";
import ModManager from "../mod-manager.js";
export default class PrintUtils {
static info(print: string) {
if (ModManager.logger != null) {
ModManager.logger.info(print);
}
console.log(chalk.white(print));
}
static warn(print: string) {
if (ModManager.logger != null) {
ModManager.logger.warn(print)
}
console.log(chalk.yellowBright(print));
}
static success(print: string) {
if (ModManager.logger != null) {
ModManager.logger.info(print)
}
console.log(chalk.greenBright(print));
}
static error(print: string) {
if (ModManager.logger != null) {
ModManager.logger.error(print);
}
console.log(chalk.redBright(print));
}