mirror of
https://github.com/LucasVbr/LucasVbr.git
synced 2026-12-05 19:00:50 +00:00
refactor: project structure and update configurations
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { dirname } from 'node:path';
|
||||
|
||||
/**
|
||||
* FileManager is a singleton class that provides methods to read and write files.
|
||||
* It ensures that only one instance of the FileManager exists throughout the application.
|
||||
*/
|
||||
export class FileManager {
|
||||
/**
|
||||
* Reads the content of a file at the specified path.
|
||||
* @param {string} filePath - The path to the file to read.
|
||||
* @returns {Promise<string>} A promise that resolves with the file content.
|
||||
*/
|
||||
public static async read(filePath: string): Promise<string> {
|
||||
return readFileSync(filePath, {
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes content to a file at the specified path.
|
||||
* If the file does not exist, it will be created.
|
||||
* @param {string} filePath - The path to the file to write.
|
||||
* @param {string} content - The content to write to the file.
|
||||
* @returns {Promise<void>} A promise that resolves when the write operation is complete.
|
||||
*/
|
||||
public static async write(filePath: string, content: string): Promise<void> {
|
||||
// Ensure the directory exists before writing the file
|
||||
const dir: string = dirname(filePath);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
return writeFileSync(filePath, content, {
|
||||
encoding: 'utf-8',
|
||||
flag: 'w',
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user