@tianyio/quality-helper
Version:
A comprehensive quality helper tool for project scaffolding and management
119 lines (118 loc) • 2.69 kB
JavaScript
import { promises } from "fs";
import { join } from "path";
class FileOperations {
// 读取文件
static async readFile(filePath) {
return await promises.readFile(filePath, "utf-8");
}
// 写入文件
static async writeFile(filePath, content, options = "utf-8") {
await promises.writeFile(filePath, content, options);
}
// 文件重命名
static async rename(oldPath, newPath) {
await promises.rename(oldPath, newPath);
}
/**
* 递归复制目录
*/
static async copyDirectory(source, target) {
await FileOperations.ensureDir(target);
const entries = await FileOperations.readDirWithFileTypes(source);
for (const entry of entries) {
const sourcePath = join(source, entry.name);
const targetPath = join(target, entry.name);
if (entry.isDirectory()) {
await FileOperations.copyDirectory(sourcePath, targetPath);
} else {
await promises.copyFile(sourcePath, targetPath);
}
}
}
/**
* 检查文件或目录是否存在
*/
static async exists(path) {
try {
await promises.access(path);
return true;
} catch {
return false;
}
}
/**
* 确保目录存在,如果不存在则创建
*/
static async ensureDir(dirPath) {
await promises.mkdir(dirPath, { recursive: true });
}
/**
* 复制单个文件
*/
static async copyFile(source, target) {
await promises.copyFile(source, target);
}
// 设置执行权限(在Unix系统上)
static async setExecutable(filePath, mode) {
try {
await promises.chmod(filePath, mode);
} catch {
}
}
/**
* 读取目录内容
*/
static async readDir(dirPath) {
try {
return await promises.readdir(dirPath);
} catch {
return [];
}
}
/**
* 读取目录内容(包含文件类型信息)
*/
static async readDirWithFileTypes(dirPath) {
try {
return await promises.readdir(dirPath, { withFileTypes: true });
} catch {
return [];
}
}
/**
* 获取文件统计信息
*/
static async stat(path) {
try {
return await promises.stat(path);
} catch {
return null;
}
}
/**
* 检查路径是否为目录
*/
static async isDirectory(path) {
try {
const stat = await promises.stat(path);
return stat.isDirectory();
} catch {
return false;
}
}
/**
* 检查路径是否为文件
*/
static async isFile(path) {
try {
const stat = await promises.stat(path);
return stat.isFile();
} catch {
return false;
}
}
}
export {
FileOperations as F
};
//# sourceMappingURL=core-file-ops-d43o99eg.js.map