32 lines
990 B
TypeScript
32 lines
990 B
TypeScript
|
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);
|
||
|
return readdirSync(fullPath, { withFileTypes: true })
|
||
|
.filter((dirent) => !dirent.isDirectory() && (!filetype || dirent.name.endsWith(filetype)))
|
||
|
.map((dirent) => dirent.name);
|
||
|
}
|
||
|
}
|