From 2ac00a9d8f298a3ebc676636fe72905d6851b1b1 Mon Sep 17 00:00:00 2001 From: AndyMik90 Date: Mon, 15 Dec 2025 22:06:46 +0100 Subject: [PATCH] feat: Add functionality to manage .gitignore entries during project initialization - Implemented ensureGitignoreEntries function to check and add necessary entries to the project's .gitignore file. - Automatically creates .gitignore if it doesn't exist and appends entries for the .auto-claude data directory. - Enhanced project initialization process to ensure proper exclusion of auto-generated files. --- .../src/main/project-initializer.ts | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/auto-claude-ui/src/main/project-initializer.ts b/auto-claude-ui/src/main/project-initializer.ts index df54009e..4b121511 100644 --- a/auto-claude-ui/src/main/project-initializer.ts +++ b/auto-claude-ui/src/main/project-initializer.ts @@ -1,4 +1,4 @@ -import { existsSync, mkdirSync, writeFileSync } from 'fs'; +import { existsSync, mkdirSync, writeFileSync, readFileSync, appendFileSync } from 'fs'; import path from 'path'; /** @@ -16,6 +16,67 @@ function debug(message: string, data?: Record): void { } } +/** + * Entries to add to .gitignore when initializing a project + */ +const GITIGNORE_ENTRIES = ['.auto-claude/']; + +/** + * Ensure entries exist in the project's .gitignore file. + * Creates .gitignore if it doesn't exist. + */ +function ensureGitignoreEntries(projectPath: string, entries: string[]): void { + const gitignorePath = path.join(projectPath, '.gitignore'); + + let content = ''; + let existingLines: string[] = []; + + if (existsSync(gitignorePath)) { + content = readFileSync(gitignorePath, 'utf-8'); + existingLines = content.split('\n').map(line => line.trim()); + } + + // Find entries that need to be added + const entriesToAdd: string[] = []; + for (const entry of entries) { + const entryNormalized = entry.replace(/\/$/, ''); // Remove trailing slash for comparison + const alreadyExists = existingLines.some(line => { + const lineNormalized = line.replace(/\/$/, ''); + return lineNormalized === entry || lineNormalized === entryNormalized; + }); + + if (!alreadyExists) { + entriesToAdd.push(entry); + } + } + + if (entriesToAdd.length === 0) { + debug('All gitignore entries already exist'); + return; + } + + // Build the content to append + let appendContent = ''; + + // Ensure file ends with newline before adding our entries + if (content && !content.endsWith('\n')) { + appendContent += '\n'; + } + + appendContent += '\n# Auto Claude data directory\n'; + for (const entry of entriesToAdd) { + appendContent += entry + '\n'; + } + + if (existsSync(gitignorePath)) { + appendFileSync(gitignorePath, appendContent); + } else { + writeFileSync(gitignorePath, '# Auto Claude data directory\n' + entriesToAdd.join('\n') + '\n'); + } + + debug('Added entries to .gitignore', { entries: entriesToAdd }); +} + /** * Data directories created in .auto-claude for each project */ @@ -106,6 +167,9 @@ export function initializeProject(projectPath: string): InitializationResult { writeFileSync(path.join(dirPath, '.gitkeep'), ''); } + // Update .gitignore to exclude .auto-claude/ + ensureGitignoreEntries(projectPath, GITIGNORE_ENTRIES); + debug('Initialization complete'); return { success: true }; } catch (error) {