import fs from 'fs-extra';
import path from 'path';
import { logger } from './logger.js';
/**
 * 文件操作工具
 */
export class FileUtils {
    /**
     * 确保目录存在
     */
    static async ensureDir(dirPath) {
        try {
            await fs.ensureDir(dirPath);
        }
        catch (error) {
            logger.error(`Failed to create directory: ${dirPath}`);
            throw error;
        }
    }
    /**
     * 复制文件或目录
     */
    static async copy(src, dest, options) {
        try {
            const { overwrite = true } = options || {};
            if (overwrite && await fs.pathExists(dest)) {
                await fs.remove(dest);
            }
            await fs.copy(src, dest);
        }
        catch (error) {
            logger.error(`Failed to copy ${src} to ${dest}`);
            throw error;
        }
    }
    /**
     * 读取 JSON 文件
     */
    static async readJson(filePath) {
        try {
            return await fs.readJson(filePath);
        }
        catch (error) {
            logger.error(`Failed to read JSON file: ${filePath}`);
            throw error;
        }
    }
    /**
     * 写入 JSON 文件
     */
    static async writeJson(filePath, data, options) {
        try {
            await fs.ensureDir(path.dirname(filePath));
            await fs.writeJson(filePath, data, { spaces: options?.spaces || 2 });
        }
        catch (error) {
            logger.error(`Failed to write JSON file: ${filePath}`);
            throw error;
        }
    }
    /**
     * 检查文件或目录是否存在
     */
    static async exists(filePath) {
        return await fs.pathExists(filePath);
    }
    /**
     * 删除文件或目录
     */
    static async remove(filePath) {
        try {
            await fs.remove(filePath);
        }
        catch (error) {
            logger.error(`Failed to remove: ${filePath}`);
            throw error;
        }
    }
    /**
     * 获取文件扩展名
     */
    static getExtname(filePath) {
        return path.extname(filePath);
    }
    /**
     * 获取文件名（不含扩展名）
     */
    static getBasename(filePath) {
        return path.basename(filePath, path.extname(filePath));
    }
    /**
     * 获取目录名
     */
    static getDirname(filePath) {
        return path.dirname(filePath);
    }
    /**
     * 解析路径
     */
    static resolve(...paths) {
        return path.resolve(...paths);
    }
    /**
     * 连接路径
     */
    static join(...paths) {
        return path.join(...paths);
    }
}
//# sourceMappingURL=file.js.map