finder-refferenct3/source/util/FileUtil.ts

65 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2024-10-31 02:37:08 +00:00
const Fs = require('fs');
const Path = require('path');
/**
*
*/
export default class FileUtil {
/**
* /
* @param {Fs.PathLike} srcPath
* @param {Fs.PathLike} destPath
*/
static copy(srcPath, destPath) {
if (!Fs.existsSync(srcPath)) return;
const stats = Fs.statSync(srcPath);
if (stats.isDirectory()) {
if (!Fs.existsSync(destPath)) Fs.mkdirSync(destPath);
const names = Fs.readdirSync(srcPath);
for (const name of names) {
this.copy(Path.join(srcPath, name), Path.join(destPath, name));
}
} else if (stats.isFile()) {
Fs.writeFileSync(destPath, Fs.readFileSync(srcPath));
}
}
/**
* /
* @param {Fs.PathLike} path
*/
static delete(path) {
if (!Fs.existsSync(path)) return;
const stats = Fs.statSync(path);
if (stats.isDirectory()) {
const names = Fs.readdirSync(path);
for (const name of names) {
this.delete(Path.join(path, name));
}
Fs.rmdirSync(path);
} else if (stats.isFile()) {
Fs.unlinkSync(path);
}
}
/**
* /
* @param {Fs.PathLike} path
* @param {(filePath: Fs.PathLike, stat: Fs.Stats) => void} handler
*/
static async map(path:string, handler: (string,any)=>Promise<void>) {
if (!Fs.existsSync(path)) return
const stats = Fs.statSync(path);
if (stats.isDirectory()) {
const names = Fs.readdirSync(path);
for (const name of names) {
await this.map(Path.join(path, name), handler);
}
} else if (stats.isFile()) {
await handler(path, stats);
}
}
}