feat: auto-update .gitignore during init command

- Add gitignore-updater.js module to manage .gitignore entries
- Automatically add Claude Flow generated files to .gitignore
- Use --force flag to update existing .gitignore entries
- Update help documentation to mention .gitignore handling

Implements #255
This commit is contained in:
rUv committed 2025-07-14 21:22:34 +00:00
1 parent 093488eec0
commit 2d3395cedd
3 files changed
+164 -1

No files matched your search

@@ -0,0 +1,136 @@
/**
* GitIgnore updater for Claude Flow initialization
* Ensures Claude Flow generated files are properly ignored
*/
import { existsSync, readTextFile, writeTextFile } from '../../node-compat.js';
/**
* Default gitignore entries for Claude Flow
*/
const CLAUDE_FLOW_GITIGNORE_ENTRIES = `
# Claude Flow generated files
.claude/settings.local.json
.claude/mcp.json
.swarm/
.hive-mind/
memory/claude-flow-data.json
memory/sessions/*
!memory/sessions/README.md
memory/agents/*
!memory/agents/README.md
coordination/memory_bank/*
coordination/subtasks/*
coordination/orchestration/*
*.db
*.db-journal
*.db-wal
*.sqlite
*.sqlite-journal
*.sqlite-wal
claude-flow
claude-flow.bat
claude-flow.ps1
hive-mind-prompt-*.txt
`;
/**
* Update or create .gitignore with Claude Flow entries
* @param {string} workingDir - The working directory
* @param {boolean} force - Whether to force update even if entries exist
* @param {boolean} dryRun - Whether to run in dry-run mode
* @returns {Promise<{success: boolean, message: string}>}
*/
export async function updateGitignore(workingDir, force = false, dryRun = false) {
const gitignorePath = `${workingDir}/.gitignore`;
try {
let gitignoreContent = '';
let fileExists = false;
// Check if .gitignore exists
if (existsSync(gitignorePath)) {
fileExists = true;
gitignoreContent = await readTextFile(gitignorePath);
}
// Check if Claude Flow section already exists
const claudeFlowMarker = '# Claude Flow generated files';
if (gitignoreContent.includes(claudeFlowMarker) && !force) {
return {
success: true,
message: '.gitignore already contains Claude Flow entries'
};
}
// Prepare the new content
let newContent = gitignoreContent;
// Remove existing Claude Flow section if force updating
if (force && gitignoreContent.includes(claudeFlowMarker)) {
const startIndex = gitignoreContent.indexOf(claudeFlowMarker);
const endIndex = gitignoreContent.indexOf('\n# ', startIndex + 1);
if (endIndex !== -1) {
newContent = gitignoreContent.substring(0, startIndex) + gitignoreContent.substring(endIndex);
} else {
// Claude Flow section is at the end
newContent = gitignoreContent.substring(0, startIndex);
}
}
// Add Claude Flow entries
if (!newContent.endsWith('\n') && newContent.length > 0) {
newContent += '\n';
}
newContent += CLAUDE_FLOW_GITIGNORE_ENTRIES;
// Write the file
if (!dryRun) {
await writeTextFile(gitignorePath, newContent);
}
return {
success: true,
message: fileExists
? (dryRun ? '[DRY RUN] Would update' : 'Updated') + ' existing .gitignore with Claude Flow entries'
: (dryRun ? '[DRY RUN] Would create' : 'Created') + ' .gitignore with Claude Flow entries'
};
} catch (error) {
return {
success: false,
message: `Failed to update .gitignore: ${error.message}`
};
}
}
/**
* Check if gitignore needs updating
* @param {string} workingDir - The working directory
* @returns {Promise<boolean>}
*/
export async function needsGitignoreUpdate(workingDir) {
const gitignorePath = `${workingDir}/.gitignore`;
if (!existsSync(gitignorePath)) {
return true;
}
try {
const content = await readTextFile(gitignorePath);
return !content.includes('# Claude Flow generated files');
} catch {
return true;
}
}
/**
* Get list of files that should be gitignored
* @returns {string[]}
*/
export function getGitignorePatterns() {
return CLAUDE_FLOW_GITIGNORE_ENTRIES
.split('\n')
.filter(line => line.trim() && !line.startsWith('#') && !line.startsWith('!'))
.map(line => line.trim());
}
+3 -1
View File
@@ -10,7 +10,7 @@ export function showInitHelp() {
console.log(' Creates CLAUDE.md & .claude/commands for MCP integration');
console.log();
console.log('Standard Options:');
console.log(' --force, -f Overwrite existing files');
console.log(' --force, -f Overwrite existing files (also updates .gitignore)');
console.log(' --dry-run, -d Preview what would be created without making changes');
console.log(' --help, -h Show this help message');
console.log();
@@ -60,11 +60,13 @@ export function showInitHelp() {
console.log('What gets created:');
console.log(' • .claude/settings.json - Global configuration with hooks and features');
console.log(' • .claude/settings.local.json - Pre-approved MCP permissions (no prompts!)');
console.log(' • .claude/mcp.json - MCP server configuration for easy setup');
console.log(' • .claude/commands/ directory with 20+ Claude Code slash commands');
console.log(' • CLAUDE.md with project instructions (v2.0.0 enhanced by default)');
console.log(' • memory/ directory for persistent context storage');
console.log(' • coordination/ directory for agent orchestration');
console.log(' • ./claude-flow local executable wrapper');
console.log(' • .gitignore entries for Claude Flow generated files');
console.log(' • Automatic MCP server setup if Claude Code CLI is installed');
console.log(' • Pre-configured for TDD, architecture, and code generation');
console.log();
+25
View File
@@ -50,6 +50,7 @@ import {
COMMAND_STRUCTURE
} from './templates/enhanced-templates.js';
import { getIsolatedNpxEnv } from '../../../utils/npx-isolated-cache.js';
import { updateGitignore, needsGitignoreUpdate } from './gitignore-updater.js';
/**
* Check if Claude Code CLI is installed
@@ -485,6 +486,18 @@ export async function initCommand(subArgs, flags) {
}
}
// Update .gitignore
const gitignoreResult = await updateGitignore(workingDir, initForce, initDryRun);
if (gitignoreResult.success) {
if (!initDryRun) {
console.log(` ✅ ${gitignoreResult.message}`);
} else {
console.log(` ${gitignoreResult.message}`);
}
} else {
console.log(` ⚠️ ${gitignoreResult.message}`);
}
console.log('\n💡 Tips:');
console.log(' • Type \'/\' in Claude Code to see all available slash commands');
console.log(' • Use \'./claude-flow status\' to check system health');
@@ -1177,6 +1190,18 @@ ${commands.map(cmd => `- [${cmd}](./${cmd}.md)`).join('\n')}
}
}
// Update .gitignore with Claude Flow entries
const gitignoreResult = await updateGitignore(workingDir, force, dryRun);
if (gitignoreResult.success) {
if (!dryRun) {
printSuccess(`✓ ${gitignoreResult.message}`);
} else {
console.log(gitignoreResult.message);
}
} else {
console.log(` ⚠️ ${gitignoreResult.message}`);
}
// Check for Claude Code and set up MCP servers (always enabled by default)
if (!dryRun && isClaudeCodeInstalled()) {
console.log('\n🔍 Claude Code CLI detected!');