34 lines
806 B
TypeScript
34 lines
806 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 removeFiles(files: string[]) {
|
|
files.forEach(file => {
|
|
if(fs.lstatSync(file).isDirectory()) {
|
|
fs.rmdirSync(file)
|
|
} else {
|
|
fs.unlinkSync(file);
|
|
}
|
|
})
|
|
}
|
|
}
|