@ryanuo/utils
Version:
提供多种实用工具函数,涵盖算法、浏览器操作、网络请求等多个领域
21 lines (20 loc) • 550 B
JavaScript
import { access, mkdir, readdir, rm, stat } from "node:fs/promises";
import { dirname, join } from "node:path";
export async function mkdirp(dirPath) {
try {
await access(dirPath);
} catch {
await mkdirp(dirname(dirPath));
await mkdir(dirPath);
}
}
export async function rmrf(path) {
const stats = await stat(path);
if (stats.isDirectory()) {
const files = await readdir(path);
await Promise.all(files.map((file) => rmrf(join(path, file))));
await rm(path, { recursive: true });
} else {
await rm(path);
}
}