trace.ai-cli
Version:
A powerful AI-powered CLI tool
122 lines (110 loc) ⢠4.59 kB
JavaScript
const TraceAI = require('./cli/traceAI');
const { analyzeFile } = require('./services/fileAnalyzer');
const { analyzeFolderStructure } = require('./services/folderAnalyzer');
const { extractTextFromImage } = require('./services/imageService');
const { processWithAI } = require('./services/aiService');
// Handle CLI arguments
async function handleCliArgs() {
const args = process.argv.slice(2);
if (args.length === 0) {
const traceAI = new TraceAI();
await traceAI.start();
return;
}
const command = args[0];
if (command === 'ask') {
const query = args.slice(1).join(' ');
if (!query) {
console.error('ā Please provide a question after "ask"');
process.exit(1);
}
try {
console.log('Processing your question...');
const result = await processWithAI(query);
console.log('\nTrace.Ai Response:');
console.log('=' .repeat(50));
console.log(result);
console.log('=' .repeat(50));
} catch (error) {
console.error('ā Error:', error.message);
if (error.code === 'ENOENT') {
console.error('File not found. Please check the path and try again.');
}
process.exit(1);
}
} else {
// Auto-detect: treat first arg as a path
const targetPath = command;
const query = args.slice(1).join(' ');
const fs = require('fs');
const path = require('path');
const { getFileType } = require('./utils/fileUtils');
try {
const resolvedPath = path.resolve(targetPath);
const stats = fs.statSync(resolvedPath);
if (stats.isDirectory()) {
console.log('š Analyzing folder...');
const result = await analyzeFolderStructure(resolvedPath, query);
console.log('\nš Folder Analysis:');
console.log('=' .repeat(50));
console.log(result.text);
console.log('=' .repeat(50));
} else if (stats.isFile()) {
const fileType = getFileType(resolvedPath);
if (fileType === 'image') {
console.log('š¼ļø Analysing Image...');
const result = await extractTextFromImage(resolvedPath, query);
console.log('\nš¼ļø Image Analysis:');
console.log('=' .repeat(50));
console.log(result);
console.log('=' .repeat(50));
} else {
console.log('š Analyzing file...');
const result = await analyzeFile(resolvedPath, query);
console.log('\nš File Analysis:');
console.log('=' .repeat(50));
console.log(result.text || result);
console.log('=' .repeat(50));
}
}
} catch (error) {
if (error.code === 'ENOENT') {
// Not a path ā show usage
console.log('Trace.Ai - AI powered CLI tool ');
console.log('\nUsage:');
console.log(' trace-ai - Start interactive mode');
console.log(' trace-ai <path> [question] - Analyze a file, folder, or image');
console.log(' trace-ai ask <question> - Ask a question');
console.log('\nExamples:');
console.log(' trace-ai ./app.js');
console.log(' trace-ai ./app.js explain this code');
console.log(' trace-ai ./src/ what is the architecture?');
console.log(' trace-ai screenshot.png describe this');
console.log(' trace-ai ask Explain quantum computing');
} else {
console.error('ā Error:', error.message);
process.exit(1);
}
}
}
}
// Error handling
process.on('unhandledRejection', (reason, promise) => {
console.error('ā Unhandled Rejection at:', promise, 'reason:', reason);
});
process.on('uncaughtException', (error) => {
console.error('ā Uncaught Exception:', error);
process.exit(1);
});
// Start the application
if (require.main === module) {
handleCliArgs().catch(console.error);
}
module.exports = {
TraceAI,
processWithAI,
extractTextFromImage,
analyzeFile,
analyzeFolderStructure
};