2024-12-30 14:47:00 +01:00
|
|
|
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs";
|
2024-12-25 12:22:28 +01:00
|
|
|
import { join } from "path";
|
|
|
|
import { readdirSync } from "fs";
|
|
|
|
|
|
|
|
export abstract class FileSystemHelper {
|
2024-12-30 14:47:00 +01:00
|
|
|
static createFolder(...args: string[]) {
|
|
|
|
const exportPath = this.formatPath(...args);
|
2024-12-25 12:22:28 +01:00
|
|
|
if (!existsSync(exportPath)) {
|
|
|
|
mkdirSync(exportPath, { recursive: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-30 14:47:00 +01:00
|
|
|
static readFile(...filePath: string[]) {
|
|
|
|
return readFileSync(this.formatPath(...filePath), "utf8");
|
2024-12-25 12:22:28 +01:00
|
|
|
}
|
|
|
|
|
2024-12-30 14:47:00 +01:00
|
|
|
static readFileasBase64(...filePath: string[]) {
|
|
|
|
return readFileSync(this.formatPath(...filePath), "base64");
|
|
|
|
}
|
|
|
|
|
|
|
|
static readTemplateFile(filePath: string) {
|
|
|
|
return readFileSync(process.cwd() + filePath, "utf8");
|
|
|
|
}
|
|
|
|
|
|
|
|
static writeFile(filePath: string, filename: string, file: any) {
|
|
|
|
this.createFolder(filePath);
|
|
|
|
let path = this.formatPath(filePath, filename);
|
|
|
|
writeFileSync(path, file);
|
2024-12-25 12:22:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static formatPath(...args: string[]) {
|
2025-01-04 09:34:45 +01:00
|
|
|
return join(process.cwd(), "files", ...args);
|
2024-12-30 14:47:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static normalizePath(...args: string[]) {
|
2024-12-25 12:22:28 +01:00
|
|
|
return join(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getFilesInDirectory(directoryPath: string, filetype?: string): string[] {
|
2024-12-30 14:47:00 +01:00
|
|
|
const fullPath = this.formatPath(directoryPath);
|
2024-12-28 18:03:33 +01:00
|
|
|
if (!existsSync(fullPath)) {
|
|
|
|
return [];
|
|
|
|
}
|
2024-12-25 12:22:28 +01:00
|
|
|
return readdirSync(fullPath, { withFileTypes: true })
|
|
|
|
.filter((dirent) => !dirent.isDirectory() && (!filetype || dirent.name.endsWith(filetype)))
|
|
|
|
.map((dirent) => dirent.name);
|
|
|
|
}
|
2024-12-30 14:47:00 +01:00
|
|
|
|
|
|
|
static clearDirectoryByFiletype(directoryPath: string, filetype: string) {
|
|
|
|
const fullPath = this.formatPath(directoryPath);
|
|
|
|
if (!existsSync(fullPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
readdirSync(fullPath, { withFileTypes: true })
|
|
|
|
.filter((dirent) => !dirent.isDirectory() && dirent.name.endsWith(filetype))
|
|
|
|
.forEach((dirent) => {
|
|
|
|
const filePath = join(fullPath, dirent.name);
|
|
|
|
unlinkSync(filePath);
|
|
|
|
});
|
|
|
|
}
|
2024-12-25 12:22:28 +01:00
|
|
|
}
|