Files
twenty/packages/twenty-server/src/command/command-logger.ts
Weiko fa4670b14d chore: extend root eslint config in twenty-server (#5101)
Reopening @thaisguigon work from
https://github.com/twentyhq/twenty/pull/4781

---------

Co-authored-by: Thaïs Guigon <guigon.thais@gmail.com>
2024-04-22 17:34:24 +02:00

49 lines
1.1 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { existsSync } from 'fs';
import fs from 'fs/promises';
import { kebabCase } from 'src/utils/kebab-case';
@Injectable()
export class CommandLogger {
constructor(private readonly className: string) {}
async createSubDirectory(subDirectory: string): Promise<void> {
const path = `./logs/${kebabCase(this.className)}/${subDirectory}`;
if (existsSync(path) === false) {
await fs.mkdir(path, { recursive: true });
}
return;
}
async writeLog(
fileName: string,
data: unknown,
append = false,
): Promise<void> {
const path = `./logs/${kebabCase(this.className)}`;
if (existsSync(path) === false) {
await fs.mkdir(path, { recursive: true });
}
try {
await fs.writeFile(
`${path}/${fileName}.json`,
JSON.stringify(data, null, 2),
{
flag: append ? 'a' : 'w',
},
);
} catch (err) {
console.error(
`Error writing to file ${path}/${fileName}.json: ${err?.message}`,
);
throw err;
}
}
}