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.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
import { existsSync, mkdirSync, writeFileSync, readFileSync, appendFileSync } from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,6 +16,67 @@ function debug(message: string, data?: Record<string, unknown>): 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
|
* Data directories created in .auto-claude for each project
|
||||||
*/
|
*/
|
||||||
@@ -106,6 +167,9 @@ export function initializeProject(projectPath: string): InitializationResult {
|
|||||||
writeFileSync(path.join(dirPath, '.gitkeep'), '');
|
writeFileSync(path.join(dirPath, '.gitkeep'), '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update .gitignore to exclude .auto-claude/
|
||||||
|
ensureGitignoreEntries(projectPath, GITIGNORE_ENTRIES);
|
||||||
|
|
||||||
debug('Initialization complete');
|
debug('Initialization complete');
|
||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user