diff --git a/src/utils/file-writer.ts b/src/utils/file-writer.ts new file mode 100644 index 0000000..f7a2b7e --- /dev/null +++ b/src/utils/file-writer.ts @@ -0,0 +1,51 @@ +import * as fs from 'fs'; +import * as path from 'path'; + +export interface WriteOptions { + outputDir: string; + fileName: string; + content: string; + overwrite?: boolean; +} + +export interface WriteResult { + success: boolean; + filePath?: string; + error?: string; +} + +export function writeFile(options: WriteOptions): WriteResult { + const { outputDir, fileName, content, overwrite = false } = options; + + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, { recursive: true }); + } + + const filePath = path.join(outputDir, fileName); + + if (fs.existsSync(filePath) && !overwrite) { + return { + success: false, + error: `File already exists: ${filePath}`, + }; + } + + try { + fs.writeFileSync(filePath, content, 'utf-8'); + return { + success: true, + filePath, + }; + } catch (error) { + return { + success: false, + error: `Failed to write file: ${error instanceof Error ? error.message : 'Unknown error'}`, + }; + } +} + +export function ensureDirectory(dirPath: string): void { + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath, { recursive: true }); + } +}