trace.ai-cli
Version:
A powerful AI-powered CLI tool
101 lines (92 loc) • 2.66 kB
JavaScript
const fs = require('fs').promises;
const path = require('path');
// File type detection
function getFileType(filePath) {
const ext = path.extname(filePath).toLowerCase();
const codeExtensions = {
'.js': 'JavaScript',
'.jsx': 'React/JavaScript',
'.ts': 'TypeScript',
'.tsx': 'React/TypeScript',
'.py': 'Python',
'.java': 'Java',
'.cpp': 'C++',
'.c': 'C',
'.cs': 'C#',
'.php': 'PHP',
'.rb': 'Ruby',
'.go': 'Go',
'.rs': 'Rust',
'.swift': 'Swift',
'.kt': 'Kotlin',
'.scala': 'Scala',
'.html': 'HTML',
'.css': 'CSS',
'.scss': 'SCSS',
'.sass': 'SASS',
'.less': 'LESS',
'.vue': 'Vue.js',
'.svelte': 'Svelte',
'.json': 'JSON',
'.xml': 'XML',
'.yaml': 'YAML',
'.yml': 'YAML',
'.toml': 'TOML',
'.ini': 'INI',
'.conf': 'Config',
'.md': 'Markdown',
'.txt': 'Text',
'.sql': 'SQL',
'.sh': 'Shell Script',
'.bash': 'Bash Script',
'.zsh': 'Zsh Script',
'.ps1': 'PowerShell',
'.bat': 'Batch Script',
'.dockerfile': 'Dockerfile',
'.gitignore': 'Git Ignore',
'.env': 'Environment Variables'
};
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.svg'];
if (imageExtensions.includes(ext)) {
return 'image';
}
return codeExtensions[ext] || 'Unknown';
}
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
const mimeTypes = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.bmp': 'image/bmp',
'.svg': 'image/svg+xml'
};
return mimeTypes[ext] || 'image/jpeg';
}
// File reading and processing
async function readFileContent(filePath) {
try {
const stats = await fs.stat(filePath);
if (stats.isDirectory()) {
throw new Error('Path is a directory, not a file');
}
// Check file size (limit to 1MB for safety)
if (stats.size > 1024 * 1024) {
throw new Error('File too large. Maximum size is 1MB');
}
const content = await fs.readFile(filePath, 'utf8');
return content;
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(`File not found: ${filePath}`);
}
throw error;
}
}
module.exports = {
getFileType,
getMimeType,
readFileContent
};