2024-12-25 12:22:28 +01:00
|
|
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
|
|
import { join } from "path";
|
|
|
|
import { readdirSync } from "fs";
|
|
|
|
|
|
|
|
export abstract class FileSystemHelper {
|
|
|
|
static createFolder(newFolder: string) {
|
|
|
|
const exportPath = join(process.cwd(), "export", newFolder);
|
|
|
|
if (!existsSync(exportPath)) {
|
|
|
|
mkdirSync(exportPath, { recursive: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static readFile(filePath: string) {
|
|
|
|
return readFileSync(join(process.cwd(), filePath), "utf8");
|
|
|
|
}
|
|
|
|
|
|
|
|
static writeFile(filePath: string, file: any) {
|
|
|
|
writeFileSync(filePath, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static formatPath(...args: string[]) {
|
|
|
|
return join(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getFilesInDirectory(directoryPath: string, filetype?: string): string[] {
|
|
|
|
const fullPath = join(process.cwd(), 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);
|
|
|
|
}
|
|
|
|
}
|