import fs from "fs/promises";

//忽略系统文件
const isSystemFile = (fileName: string) => {
  return fileName.includes(".DS_Store");
};

//读取目录下的文件
export const onReadDirectory = async (path: string): Promise<string[]> => {
  try {
    const files = await fs.readdir(path);
    return files.filter((res) => !isSystemFile(res));
  } catch (error) {
    console.error(`Error reading directory at ${path}:`, error);
    throw error; // 抛出错误
  }
};

//判断是否是一个文件夹
export const isDirectory = async (path: string): Promise<boolean> => {
  try {
    const stats = await fs.stat(path);
    return stats.isDirectory();
  } catch (error) {
    console.error(`Error reading path: ${path}`);
    throw error;
  }
};

//判断是否是一个文件夹
export const onWriteFile = async (path: string, content: string) => {
  try {
    return await fs.writeFile(path, content, "utf-8");
  } catch (error) {
    console.error(`Error writeFile path: ${path}`);
    throw error;
  }
};

//删除文件
export const onDeleteFile = async (path: string) => {
  try {
    await fs.unlink(path);
  } catch (error) {
    console.error(`Error deleting file at ${path}:`, error);
    throw error;
  }
};
