ctx-gen
Version:
AI-Enhanced Documentation Generator for Code Understanding
94 lines • 4.63 kB
JavaScript
import { Command } from 'commander';
import chalk from 'chalk';
import fs from 'fs-extra';
import path from 'path';
import { generateFullDocs, updateDocs } from '../core/ctxGen.js';
// Get package version (safely handle projects without package.json)
let version = '1.0.2'; // Default version
const packageJsonPath = path.join(process.cwd(), 'package.json');
try {
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
version = packageJson.version || version;
}
}
catch (_error) {
// Silently continue with default version
}
// Create the program
const program = new Command();
// Set up program metadata
program
.name('ctx-gen')
.description('AI-Enhanced Documentation Generator for Code Understanding')
.version(version);
// The 'full' command for generating complete documentation
program
.command('full')
.description('Generate full documentation for the codebase')
.option('--docs-dir <path>', 'Output directory for documentation', 'docs')
.option('--cache-file <path>', 'Path to the cache file', '.doc_cache.json')
.option('--exclude <paths>', 'Comma-separated list of paths to exclude', '.git,node_modules,dist,venv,.venv,env,.env,__pycache__,*.pyc')
.option('--languages <langs>', 'Comma-separated list of languages to analyze', 'typescript,javascript')
.option('--diagrams <types>', 'Comma-separated list of diagram types to generate', 'call-graph,data-flow,module-deps,class-diagram')
.option('--machine-formats', 'Generate JSON/XML outputs for AI consumption', false)
.option('--include-examples', 'Include usage examples in documentation', false)
.option('--no-ai', 'Disable AI-powered features')
.option('--openai-key <key>', 'OpenAI API key for AI features')
.option('--auto-ignore', 'Auto-add docsDir to .gitignore', true)
.option('--respect-gitignore', 'Respect .gitignore patterns when analyzing files', true)
.option('--concurrent-ai <number>', 'Maximum number of concurrent AI analyses', '3')
.action(async (options) => {
try {
// Convert kebab-case options to camelCase
const camelCaseOptions = {
...options,
respectGitignore: options.respectGitignore,
concurrentAiAnalyses: parseInt(options.concurrentAi, 10) || 3
};
console.log(chalk.blue('Generating full documentation...'));
await generateFullDocs(camelCaseOptions);
console.log(chalk.green('Documentation generation complete!'));
}
catch (error) {
console.error(chalk.red('Error generating documentation:'), error);
process.exit(1);
}
});
// The 'update' command for updating existing documentation
program
.command('update')
.description('Update existing documentation')
.option('--docs-dir <path>', 'Output directory for documentation', 'docs')
.option('--cache-file <path>', 'Path to the cache file', '.doc_cache.json')
.option('--exclude <paths>', 'Comma-separated list of paths to exclude', '.git,node_modules,dist,venv,.venv,env,.env,__pycache__,*.pyc')
.option('--languages <langs>', 'Comma-separated list of languages to analyze', 'typescript,javascript')
.option('--diagrams <types>', 'Comma-separated list of diagram types to generate', 'call-graph,data-flow,module-deps,class-diagram')
.option('--machine-formats', 'Generate JSON/XML outputs for AI consumption', false)
.option('--include-examples', 'Include usage examples in documentation', false)
.option('--no-ai', 'Disable AI-powered features')
.option('--openai-key <key>', 'OpenAI API key for AI features')
.option('--auto-ignore', 'Auto-add docsDir to .gitignore', true)
.option('--respect-gitignore', 'Respect .gitignore patterns when analyzing files', true)
.option('--concurrent-ai <number>', 'Maximum number of concurrent AI analyses', '3')
.action(async (options) => {
try {
// Convert kebab-case options to camelCase
const camelCaseOptions = {
...options,
respectGitignore: options.respectGitignore,
concurrentAiAnalyses: parseInt(options.concurrentAi, 10) || 3
};
console.log(chalk.blue('Updating documentation...'));
await updateDocs(camelCaseOptions);
console.log(chalk.green('Documentation update complete!'));
}
catch (error) {
console.error(chalk.red('Error updating documentation:'), error);
process.exit(1);
}
});
// Parse command line arguments
program.parse();
//# sourceMappingURL=index.js.map