diff --git a/project/src/utils/VFS.ts b/project/src/utils/VFS.ts index 37ea722e..d2ed724e 100644 --- a/project/src/utils/VFS.ts +++ b/project/src/utils/VFS.ts @@ -10,7 +10,7 @@ import lockfile from "proper-lockfile"; import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue"; @injectable() -export class VFS +export class VFS { accessFilePromisify: (path: fs.PathLike, mode?: number) => Promise; copyFilePromisify: (src: fs.PathLike, dst: fs.PathLike, flags?: number) => Promise; @@ -39,14 +39,14 @@ export class VFS this.renamePromisify = promisify(fs.renameSync); } - public exists(filepath: fs.PathLike): boolean + public exists(filepath: fs.PathLike): boolean { return fs.existsSync(filepath); } - public async existsAsync(filepath: fs.PathLike): Promise + public async existsAsync(filepath: fs.PathLike): Promise { - try + try { // Create the command to add to the queue const command = { @@ -59,19 +59,19 @@ export class VFS // If no Exception, the file exists return true; } - catch + catch { - // If Exception, the file does not exis + // If Exception, the file does not exist return false; } } - public copyFile(filepath: fs.PathLike, target: fs.PathLike): void + public copyFile(filepath: fs.PathLike, target: fs.PathLike): void { fs.copyFileSync(filepath, target); } - public async copyAsync(filepath: fs.PathLike, target: fs.PathLike): Promise + public async copyAsync(filepath: fs.PathLike, target: fs.PathLike): Promise { const command = { uuid: crypto.randomUUID(), @@ -80,12 +80,12 @@ export class VFS await this.asyncQueue.waitFor(command); } - public createDir(filepath: string): void + public createDir(filepath: string): void { fs.mkdirSync(filepath.substr(0, filepath.lastIndexOf("/")), { "recursive": true }); } - public async createDirAsync(filepath: string): Promise + public async createDirAsync(filepath: string): Promise { const command = { uuid: crypto.randomUUID(), @@ -94,65 +94,65 @@ export class VFS await this.asyncQueue.waitFor(command); } - public copyDir(filepath: string, target: string, fileExtensions: string | string[] = undefined): void + public copyDir(filepath: string, target: string, fileExtensions: string | string[] = undefined): void { const files = this.getFiles(filepath); const dirs = this.getDirs(filepath); - if (!this.exists(target)) + if (!this.exists(target)) { this.createDir(`${target}/`); } - for (const dir of dirs) + for (const dir of dirs) { this.copyDir(path.join(filepath, dir), path.join(target, dir), fileExtensions); } - for (const file of files) + for (const file of files) { // copy all if fileExtension is not set, copy only those with fileExtension if set - if (!fileExtensions || fileExtensions.includes(file.split(".").pop())) + if (!fileExtensions || fileExtensions.includes(file.split(".").pop())) { this.copyFile(path.join(filepath, file), path.join(target, file)); } } } - public async copyDirAsync(filepath: string, target: string, fileExtensions: string | string[]): Promise + public async copyDirAsync(filepath: string, target: string, fileExtensions: string | string[]): Promise { const files = this.getFiles(filepath); const dirs = this.getDirs(filepath); - if (!await this.existsAsync(target)) + if (!await this.existsAsync(target)) { await this.createDirAsync(`${target}/`); } - for (const dir of dirs) + for (const dir of dirs) { await this.copyDirAsync(path.join(filepath, dir), path.join(target, dir), fileExtensions); } - for (const file of files) + for (const file of files) { // copy all if fileExtension is not set, copy only those with fileExtension if set - if (!fileExtensions || fileExtensions.includes(file.split(".").pop())) + if (!fileExtensions || fileExtensions.includes(file.split(".").pop())) { await this.copyAsync(path.join(filepath, file), path.join(target, file)); } } } - public readFile(...args: Parameters): string + public readFile(...args: Parameters): string { const read = fs.readFileSync(...args); if (this.isBuffer(read)) return read.toString(); return read; } - - public async readFileAsync(path: fs.PathLike): Promise + + public async readFileAsync(path: fs.PathLike): Promise { const read = await this.readFilePromisify(path); if (this.isBuffer(read)) @@ -165,11 +165,11 @@ export class VFS return value?.write && value.toString && value.toJSON && value.equals; } - public writeFile(filepath: any, data = "", append = false, atomic = true): void + public writeFile(filepath: any, data = "", append = false, atomic = true): void { const options = (append) ? { "flag": "a" } : { "flag": "w" }; - if (!this.exists(filepath)) + if (!this.exists(filepath)) { this.createDir(filepath); fs.writeFileSync(filepath, ""); @@ -177,53 +177,53 @@ export class VFS this.lockFileSync(filepath); - if (!append && atomic) + if (!append && atomic) { writeFileSync(filepath, data); } - else + else { fs.writeFileSync(filepath, data, options); } - if (this.checkFileSync(filepath)) + if (this.checkFileSync(filepath)) { this.unlockFileSync(filepath); } } - public async writeFileAsync(filepath: any, data = "", append = false, atomic = true): Promise + public async writeFileAsync(filepath: any, data = "", append = false, atomic = true): Promise { const options = (append) ? { "flag": "a" } : { "flag": "w" }; - if (!await this.exists(filepath)) + if (!await this.exists(filepath)) { await this.createDir(filepath); await this.writeFilePromisify(filepath, ""); } - if (!append && atomic) + if (!append && atomic) { await this.writeFilePromisify(filepath, data); } - else + else { await this.writeFilePromisify(filepath, data, options); } } - public getFiles(filepath: string): string[] + public getFiles(filepath: string): string[] { - return fs.readdirSync(filepath).filter((item) => + return fs.readdirSync(filepath).filter((item) => { return fs.statSync(path.join(filepath, item)).isFile(); }); } - public async getFilesAsync(filepath: string): Promise + public async getFilesAsync(filepath: string): Promise { const addr = await this.readdirPromisify(filepath); - return addr.filter(async (item) => + return addr.filter(async (item) => { const stat = await this.statPromisify(path.join(filepath, item)); return stat.isFile(); @@ -231,45 +231,45 @@ export class VFS } - public getDirs(filepath: string): string[] + public getDirs(filepath: string): string[] { - return fs.readdirSync(filepath).filter((item) => + return fs.readdirSync(filepath).filter((item) => { return fs.statSync(path.join(filepath, item)).isDirectory(); }); } - public async getDirsAsync(filepath: string): Promise + public async getDirsAsync(filepath: string): Promise { const addr = await this.readdirPromisify(filepath); - return addr.filter(async (item) => + return addr.filter(async (item) => { const stat = await this.statPromisify(path.join(filepath, item)); return stat.isDirectory(); }); } - public removeFile(filepath: string): void + public removeFile(filepath: string): void { fs.unlinkSync(filepath); } - public async removeFileAsync(filepath: string): Promise + public async removeFileAsync(filepath: string): Promise { await this.unlinkPromisify(filepath); } - public removeDir(filepath: string): void + public removeDir(filepath: string): void { const files = this.getFiles(filepath); const dirs = this.getDirs(filepath); - for (const dir of dirs) + for (const dir of dirs) { this.removeDir(path.join(filepath, dir)); } - for (const file of files) + for (const file of files) { this.removeFile(path.join(filepath, file)); } @@ -277,19 +277,19 @@ export class VFS fs.rmdirSync(filepath); } - public async removeDirAsync(filepath: string): Promise + public async removeDirAsync(filepath: string): Promise { const files = this.getFiles(filepath); const dirs = this.getDirs(filepath); const promises = []; - for (const dir of dirs) + for (const dir of dirs) { promises.push(this.removeDirAsync(path.join(filepath, dir))); } - for (const file of files) + for (const file of files) { promises.push(this.removeFile(path.join(filepath, file))); } @@ -298,45 +298,45 @@ export class VFS await this.rmdirPromisify(filepath); } - public rename(oldPath: string, newPath: string): void + public rename(oldPath: string, newPath: string): void { fs.renameSync(oldPath, newPath); } - public async renameAsync(oldPath: string, newPath: string): Promise + public async renameAsync(oldPath: string, newPath: string): Promise { await this.renamePromisify(oldPath, newPath); } - protected lockFileSync(filepath: any): void + protected lockFileSync(filepath: any): void { lockfile.lockSync(filepath); } - protected checkFileSync(filepath: any): any + protected checkFileSync(filepath: any): any { return lockfile.checkSync(filepath); } - protected unlockFileSync(filepath: any): void + protected unlockFileSync(filepath: any): void { lockfile.unlockSync(filepath); } - public getFileExtension(filepath: string): string + public getFileExtension(filepath: string): string { return filepath.split(".").pop(); } - public stripExtension(filepath: string): string + public stripExtension(filepath: string): string { return filepath.split(".").slice(0, -1).join("."); } - public async minifyAllJsonInDirRecursive(filepath: string): Promise + public async minifyAllJsonInDirRecursive(filepath: string): Promise { const files = this.getFiles(filepath).filter((item) => this.getFileExtension(item) === "json"); - for (const file of files) + for (const file of files) { const filePathAndName = path.join(filepath, file); const minified = JSON.stringify(JSON.parse(this.readFile(filePathAndName))); @@ -344,16 +344,16 @@ export class VFS } const dirs = this.getDirs(filepath); - for (const dir of dirs) + for (const dir of dirs) { this.minifyAllJsonInDirRecursive(path.join(filepath, dir)); } } - public async minifyAllJsonInDirRecursiveAsync(filepath: string): Promise + public async minifyAllJsonInDirRecursiveAsync(filepath: string): Promise { const files = this.getFiles(filepath).filter((item) => this.getFileExtension(item) === "json"); - for (const file of files) + for (const file of files) { const filePathAndName = path.join(filepath, file); const minified = JSON.stringify(JSON.parse(await this.readFile(filePathAndName))); @@ -362,7 +362,7 @@ export class VFS const dirs = this.getDirs(filepath); const promises: Promise[] = []; - for (const dir of dirs) + for (const dir of dirs) { promises.push(this.minifyAllJsonInDirRecursive(path.join(filepath, dir))); } diff --git a/project/src/utils/logging/AbstractWinstonLogger.ts b/project/src/utils/logging/AbstractWinstonLogger.ts index 5a3c923c..8eeec508 100644 --- a/project/src/utils/logging/AbstractWinstonLogger.ts +++ b/project/src/utils/logging/AbstractWinstonLogger.ts @@ -12,7 +12,7 @@ import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue"; import { ICommand } from "@spt-aki/models/spt/utils/ICommand"; import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; -export abstract class AbstractWinstonLogger implements ILogger +export abstract class AbstractWinstonLogger implements ILogger { protected showDebugInConsole = false; protected filePath: string; @@ -55,7 +55,7 @@ export abstract class AbstractWinstonLogger implements ILogger this.filePath = `${this.getFilePath()}${this.getFileName()}`; this.writeFilePromisify = promisify(fs.writeFile); this.showDebugInConsole = globalThis.G_DEBUG_CONFIGURATION; - if (!fs.existsSync(this.getFilePath())) + if (!fs.existsSync(this.getFilePath())) { fs.mkdirSync(this.getFilePath(), { recursive: true }); } @@ -69,7 +69,7 @@ export abstract class AbstractWinstonLogger implements ILogger level: this.showDebugInConsole ? "debug" : "custom", format: format.combine( format.colorize({ all: true, colors: this.logLevels.colors }), - format.printf(({ message }) => + format.printf(({ message }) => { return `${message}`; }) @@ -91,7 +91,7 @@ export abstract class AbstractWinstonLogger implements ILogger format.timestamp(), format.align(), format.json(), - format.printf(({ timestamp, level, message }) => + format.printf(({ timestamp, level, message }) => { return `[${timestamp}] ${level}: ${message}`; }) @@ -108,7 +108,7 @@ export abstract class AbstractWinstonLogger implements ILogger if (this.isLogExceptions()) { - process.on("uncaughtException", (error) => + process.on("uncaughtException", (error) => { this.error(`${error.name}: ${error.message}`); this.error(error.stack); @@ -123,9 +123,9 @@ export abstract class AbstractWinstonLogger implements ILogger protected abstract isLogExceptions(): boolean; protected abstract getFilePath(): string; - + protected abstract getFileName(): string; - + protected getLogMaxSize(): string { return "5m"; @@ -136,7 +136,7 @@ export abstract class AbstractWinstonLogger implements ILogger return "14d"; } - public async writeToLogFile(data: string | Daum): Promise + public async writeToLogFile(data: string | Daum): Promise { const command: ICommand = { uuid: crypto.randomUUID(), @@ -145,7 +145,7 @@ export abstract class AbstractWinstonLogger implements ILogger await this.asyncQueue.waitFor(command); } - public async log(data: string | Error | Record, color: string, backgroundColor = "" ): Promise + public async log(data: string | Error | Record, color: string, backgroundColor = "" ): Promise { const textColor = `${color} ${backgroundColor}`.trimEnd(); const tmpLogger = createLogger({ @@ -161,14 +161,14 @@ export abstract class AbstractWinstonLogger implements ILogger let command: ICommand; - if (typeof (data) === "string") + if (typeof (data) === "string") { command = { uuid: crypto.randomUUID(), cmd: async () => await tmpLogger.log("custom", data) }; } - else + else { command = { uuid: crypto.randomUUID(), @@ -179,7 +179,7 @@ export abstract class AbstractWinstonLogger implements ILogger await this.asyncQueue.waitFor(command); } - public async error(data: string | Record): Promise + public async error(data: string | Record): Promise { const command: ICommand = { uuid: crypto.randomUUID(), @@ -188,7 +188,7 @@ export abstract class AbstractWinstonLogger implements ILogger await this.asyncQueue.waitFor(command); } - public async warning(data: string | Record): Promise + public async warning(data: string | Record): Promise { const command: ICommand = { uuid: crypto.randomUUID(), @@ -197,7 +197,7 @@ export abstract class AbstractWinstonLogger implements ILogger await this.asyncQueue.waitFor(command); } - public async success(data: string | Record): Promise + public async success(data: string | Record): Promise { const command: ICommand = { uuid: crypto.randomUUID(), @@ -206,7 +206,7 @@ export abstract class AbstractWinstonLogger implements ILogger await this.asyncQueue.waitFor(command); } - public async info(data: string | Record): Promise + public async info(data: string | Record): Promise { const command: ICommand = { uuid: crypto.randomUUID(), @@ -231,18 +231,18 @@ export abstract class AbstractWinstonLogger implements ILogger await this.asyncQueue.waitFor(command); } - public async debug(data: string | Record, onlyShowInConsole = false): Promise + public async debug(data: string | Record, onlyShowInConsole = false): Promise { let command: ICommand; - if (onlyShowInConsole) + if (onlyShowInConsole) { command = { uuid: crypto.randomUUID(), cmd: async () => await this.log(data, this.logLevels.colors.debug) }; } - else + else { command = { uuid: crypto.randomUUID(), diff --git a/project/src/utils/logging/WinstonMainLogger.ts b/project/src/utils/logging/WinstonMainLogger.ts index 8dd08fa9..145d1059 100644 --- a/project/src/utils/logging/WinstonMainLogger.ts +++ b/project/src/utils/logging/WinstonMainLogger.ts @@ -4,9 +4,8 @@ import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue"; import { AbstractWinstonLogger } from "@spt-aki/utils/logging/AbstractWinstonLogger"; @injectable() -export class WinstonMainLogger extends AbstractWinstonLogger +export class WinstonMainLogger extends AbstractWinstonLogger { - constructor( @inject("AsyncQueue") protected asyncQueue: IAsyncQueue ) @@ -14,27 +13,27 @@ export class WinstonMainLogger extends AbstractWinstonLogger super(asyncQueue); } - protected isLogExceptions(): boolean + protected isLogExceptions(): boolean { return true; } - protected isLogToFile(): boolean - { - return true; - } - - protected isLogToConsole(): boolean + protected isLogToFile(): boolean { return true; } - protected getFilePath(): string + protected isLogToConsole(): boolean + { + return true; + } + + protected getFilePath(): string { return "./user/logs/"; } - protected getFileName(): string + protected getFileName(): string { return "server-%DATE%.log"; } diff --git a/project/src/utils/logging/WinstonRequestLogger.ts b/project/src/utils/logging/WinstonRequestLogger.ts index 85edc0a3..4883f310 100644 --- a/project/src/utils/logging/WinstonRequestLogger.ts +++ b/project/src/utils/logging/WinstonRequestLogger.ts @@ -4,7 +4,7 @@ import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue"; import { AbstractWinstonLogger } from "@spt-aki/utils/logging/AbstractWinstonLogger"; @injectable() -export class WinstonRequestLogger extends AbstractWinstonLogger +export class WinstonRequestLogger extends AbstractWinstonLogger { constructor( @inject("AsyncQueue") protected asyncQueue: IAsyncQueue @@ -13,27 +13,27 @@ export class WinstonRequestLogger extends AbstractWinstonLogger super(asyncQueue); } - protected isLogExceptions(): boolean + protected isLogExceptions(): boolean { return false; } - protected isLogToFile(): boolean + protected isLogToFile(): boolean { return true; } - protected isLogToConsole(): boolean + protected isLogToConsole(): boolean { return false; } - protected getFilePath(): string + protected getFilePath(): string { return "./user/logs/requests/"; } - protected getFileName(): string + protected getFileName(): string { return "requests-%DATE%.log"; }