{"version":3,"sources":["../../src/utils/fs.ts"],"names":["readFileContent","filePath","cwd","absolutePath","path","fs","error","isEnoentError","writeFileContent","content","sanitizedPath","deleteFile","fileExists","safeRename","fromPath","toPath","renameFile","fromAbsolutePath","toAbsolutePath","isDirectoryEmpty","dirPath","removeEmptyParentDirectories","rootDir","getErrorMessage"],"mappings":"+LAIO,MAAMA,CAAAA,CAAkB,MAAOC,EAAkBC,CAAAA,CAAc,OAAA,CAAQ,GAAA,EAAI,GAA8B,CAC9G,GAAI,CACF,MAAMC,CAAAA,CAAeC,mBAAK,OAAA,CAAQF,CAAAA,CAAKD,CAAQ,CAAA,CAE/C,QADc,MAAMI,WAAAA,CAAG,KAAKF,CAAY,CAAA,EAC9B,aAAY,CACb,IAAA,CAEF,MAAME,WAAAA,CAAG,SAASF,CAAAA,CAAc,OAAO,CAChD,CAAA,MAASG,EAAO,CACd,GAAIC,oBAAAA,CAAcD,CAAK,EACrB,OAAO,IAAA,CAET,MAAMA,CACR,CACF,EAEaE,CAAAA,CAAmB,MAAOP,CAAAA,CAAkBQ,CAAAA,CAAiBP,EAAc,OAAA,CAAQ,GAAA,EAAI,GAAqB,CACvH,MAAMQ,CAAAA,CAAgBT,CAAAA,CAAS,OAAA,CAAQ,SAAA,CAAW,EAAE,CAAA,CACpD,GAAI,CAACS,CAAAA,CAAe,CAClB,QAAQ,IAAA,CAAK,CAAA,8CAAA,EAAiDT,CAAQ,CAAA,WAAA,CAAa,EACnF,MACF,CACA,MAAME,CAAAA,CAAeC,mBAAK,OAAA,CAAQF,CAAAA,CAAKQ,CAAa,CAAA,CACpD,MAAML,WAAAA,CAAG,KAAA,CAAMD,mBAAK,OAAA,CAAQD,CAAY,EAAG,CAAE,SAAA,CAAW,IAAK,CAAC,EAC9D,MAAME,WAAAA,CAAG,SAAA,CAAUF,CAAAA,CAAcM,EAAS,OAAO,EACnD,CAAA,CAEaE,CAAAA,CAAa,MAAOV,CAAAA,CAAkBC,CAAAA,CAAc,QAAQ,GAAA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,CAAc,CAAA,CAAG,CAAE,SAAA,CAAW,IAAK,CAAC,CAAA,CAChE,MAAML,CAAAA,CAAWI,CAAAA,CAAkBC,CAAc,EACnD,EAGaC,CAAAA,CAAmB,MAAOC,CAAAA,EAAsC,CAC3E,GAAI,CAEF,OAAA,CADc,MAAMf,WAAAA,CAAG,OAAA,CAAQe,CAAO,CAAA,EACzB,MAAA,GAAW,CAC1B,CAAA,KAAgB,CAEd,OAAO,MACT,CACF,CAAA,CAGaC,EAA+B,MAAOD,CAAAA,CAAiBE,CAAAA,GAAmC,CACrG,GAAI,EAAA,CAACF,CAAAA,CAAQ,WAAWE,CAAO,CAAA,EAAKF,IAAYE,CAAAA,CAAAA,CAIhD,GAAI,CACc,MAAMH,EAAiBC,CAAO,CAAA,GAE5C,MAAMf,WAAAA,CAAG,MAAMe,CAAO,CAAA,CAEtB,MAAMC,CAAAA,CAA6BjB,mBAAK,OAAA,CAAQgB,CAAO,EAAGE,CAAO,CAAA,EAErE,OAAShB,CAAAA,CAAgB,CAEjBA,CAAAA,YAAiB,KAAA,EAAS,SAAUA,CAAAA,GACpCA,CAAAA,CAAgC,IAAA,GAAS,QAAA,EAAaA,EAAgC,IAAA,GAAS,SAAA,CAAA,EACnG,OAAA,CAAQ,IAAA,CAAK,gCAAgCc,CAAO,CAAA,CAAA,CAAA,CAAKG,uBAAgBjB,CAAK,CAAC,EAEnF,CACF","file":"fs.cjs","sourcesContent":["import { promises as fs } from 'fs';\nimport path from 'path';\nimport { getErrorMessage, isEnoentError } from './logger';\n\nexport const readFileContent = async (filePath: string, cwd: string = process.cwd()): Promise<string | null> => {\n  try {\n    const absolutePath = path.resolve(cwd, filePath);\n    const stats = await fs.stat(absolutePath);\n    if (stats.isDirectory()) {\n      return null; // Path is a directory, not a file\n    }\n    return await fs.readFile(absolutePath, 'utf-8');\n  } catch (error) {\n    if (isEnoentError(error)) {\n      return null; // File doesn't exist\n    }\n    throw error;\n  }\n};\n\nexport const writeFileContent = async (filePath: string, content: string, cwd: string = process.cwd()): Promise<void> => {\n  const sanitizedPath = filePath.replace(/[/\\\\]+$/, '');\n  if (!sanitizedPath) {\n    console.warn(`Invalid file path (points to root directory): ${filePath}. Skipping.`);\n    return;\n  }\n  const absolutePath = path.resolve(cwd, sanitizedPath);\n  await fs.mkdir(path.dirname(absolutePath), { recursive: true });\n  await fs.writeFile(absolutePath, content, 'utf-8');\n};\n\nexport const deleteFile = async (filePath: string, cwd: string = process.cwd()): Promise<void> => {\n  try {\n    await fs.unlink(path.resolve(cwd, filePath));\n  } catch (error: unknown) {\n    if (error instanceof Error && 'code' in error && ((error as NodeJS.ErrnoException).code === 'ENOENT' || (error as NodeJS.ErrnoException).code === 'ENOTDIR')) {\n      // File already deleted or is a directory, which is fine for an unlink operation.\n      return;\n    }\n    throw error;\n  }\n};\n\nexport const fileExists = async (filePath: string, cwd: string = process.cwd()): Promise<boolean> => {\n  try {\n    await fs.access(path.resolve(cwd, filePath));\n    return true;\n  } catch {\n    return false;\n  }\n};\n\nexport const safeRename = async (fromPath: string, toPath:string): Promise<void> => {\n    try {\n        await fs.rename(fromPath, toPath);\n    } catch (error: unknown) {\n        if (error instanceof Error && 'code' in error && (error as NodeJS.ErrnoException).code === 'EXDEV') {\n            await fs.copyFile(fromPath, toPath);\n            await fs.unlink(fromPath);\n        } else {\n            throw error;\n        }\n    }\n};\n\nexport const renameFile = async (fromPath: string, toPath: string, cwd: string = process.cwd()): Promise<void> => {\n  const fromAbsolutePath = path.resolve(cwd, fromPath);\n  const toAbsolutePath = path.resolve(cwd, toPath);\n  await fs.mkdir(path.dirname(toAbsolutePath), { recursive: true });\n  await safeRename(fromAbsolutePath, toAbsolutePath);\n};\n\n// Helper to check if a directory is empty\nexport const isDirectoryEmpty = async (dirPath: string): Promise<boolean> => {\n  try {\n    const files = await fs.readdir(dirPath);\n    return files.length === 0;\n  } catch (error) {\n    // If directory doesn't exist or is not accessible, consider it \"not empty\"\n    return false;\n  }\n};\n\n// Recursively remove all empty parent directories up to a limit\nexport const removeEmptyParentDirectories = async (dirPath: string, rootDir: string): Promise<void> => {\n  if (!dirPath.startsWith(rootDir) || dirPath === rootDir) {\n    return;\n  }\n  \n  try {\n    const isEmpty = await isDirectoryEmpty(dirPath);\n    if (isEmpty) {\n      await fs.rmdir(dirPath);\n      // Recursively check parent directory\n      await removeEmptyParentDirectories(path.dirname(dirPath), rootDir);\n    }\n  } catch (error: unknown) {\n    // Ignore directory removal errors, but don't continue up the chain\n    if (!(error instanceof Error && 'code' in error &&\n        ((error as NodeJS.ErrnoException).code === 'ENOENT' || (error as NodeJS.ErrnoException).code === 'ENOTDIR'))) {\n      console.warn(`Failed to clean up directory ${dirPath}:`, getErrorMessage(error));\n    }\n  }\n};"]}