UNPKG

@morodomi/ait3

Version:

AIT³ Development Platform - AI + Ticket + Test + Tool driven development methodology

75 lines (74 loc) 2.34 kB
import { writeFile, access, mkdir } from 'fs/promises'; import { dirname } from 'path'; /** * Check if a file exists * More robust than the previous inline implementation */ export async function fileExists(path) { try { await access(path); return true; } catch (error) { // Only return false for ENOENT (file not found) if (error instanceof Error && 'code' in error && error.code === 'ENOENT') { return false; } // Re-throw other errors (permissions, etc.) throw error; } } /** * Generate multiple files with automatic directory creation * Skips files that already exist to avoid overwriting */ export async function generateTemplateFiles(templates, options = {}) { const { overwrite = false } = options; const tasks = []; for (const template of templates) { if (!overwrite && await fileExists(template.path)) { continue; // Skip existing files } // Ensure directory exists const dir = dirname(template.path); if (dir !== '.') { await mkdir(dir, { recursive: true }); } tasks.push(writeFile(template.path, template.content, 'utf-8')); } await Promise.all(tasks); } /** * Generate files with error handling and reporting * Returns information about what was actually created */ export async function generateTemplateFilesWithReport(templates, options = {}) { const { overwrite = false } = options; const result = { created: [], skipped: [], errors: [] }; for (const template of templates) { try { if (!overwrite && await fileExists(template.path)) { result.skipped.push(template.path); continue; } // Ensure directory exists const dir = dirname(template.path); if (dir !== '.') { await mkdir(dir, { recursive: true }); } await writeFile(template.path, template.content, 'utf-8'); result.created.push(template.path); } catch (error) { result.errors.push({ path: template.path, error: error instanceof Error ? error.message : String(error) }); } } return result; }