40 lines
975 B
TypeScript
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);
|
|
}
|
|
})
|
|
}
|
|
}
|