2024-03-20 09:26:14 +01:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "node:path";
|
2024-03-20 19:35:24 +01:00
|
|
|
import {mkdirSync} from "fs";
|
2024-03-21 08:43:28 +01:00
|
|
|
import {lstatSync} from "node:fs";
|
2024-03-20 09:26:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
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 [];
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:35:24 +01:00
|
|
|
|
|
|
|
public static prepareFileForWriting(file: string) {
|
|
|
|
const parentDirectory = path.dirname(file)
|
|
|
|
|
|
|
|
if(!fs.existsSync(parentDirectory)) {
|
|
|
|
mkdirSync(parentDirectory, {recursive: true})
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 08:43:28 +01:00
|
|
|
|
|
|
|
public static removeFiles(files: string[]) {
|
|
|
|
files.forEach(file => {
|
|
|
|
if(fs.lstatSync(file).isDirectory()) {
|
|
|
|
fs.rmdirSync(file)
|
|
|
|
} else {
|
|
|
|
fs.unlinkSync(file);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-03-20 09:26:14 +01:00
|
|
|
}
|