ConceptCreator/app/storage/FileUtils.ts
Sebastian Böckelmann 0884709603
All checks were successful
E2E Testing / test (push) Successful in 1m41s
Persist Characters
2024-03-22 10:01:47 +01:00

40 lines
975 B
TypeScript

import * as fs from "fs";
import * as path from "node:path";
import {mkdirSync} from "fs";
import {lstatSync} from "node:fs";
export class FileUtils {
public static listFilesInDirectory(directory: string) {
if(fs.lstatSync(directory).isDirectory()) {
return fs.readdirSync(directory).map(fileName => path.join(directory, fileName))
} else {
return [];
}
}
public static prepareFileForWriting(file: string) {
const parentDirectory = path.dirname(file)
if(!fs.existsSync(parentDirectory)) {
mkdirSync(parentDirectory, {recursive: true})
}
}
public static prepareDirectoryFroWriting(directoryFile: string) {
if(!fs.existsSync(directoryFile)) {
mkdirSync(directoryFile, {recursive: true})
}
}
public static removeFiles(files: string[]) {
files.forEach(file => {
if(fs.lstatSync(file).isDirectory()) {
fs.rmdirSync(file)
} else {
fs.unlinkSync(file);
}
})
}
}