UNPKG

intellify

Version:

Detect JavaScript & TypeScript errors and make codes more optimized.

128 lines (93 loc) 3.43 kB
import fs from 'fs'; import path from 'path'; import { debugLog, getJsFiles } from '../utils/helpers.js'; import { findUnusedVariables, findComments, findSyntaxErrors } from './code.js'; export async function analyzeProject(projectPath) { debugLog(`Starting project analysis for: ${projectPath}`); const results = { syntaxErrors: [], unusedVariables: [], unusedPackages: [], comments: [], analyzedFiles: [] }; const stats = { filesAnalyzed: 0, linesOfCode: 0, totalSize: 0, fileTypes: {} }; try { const files = getJsFiles(projectPath); debugLog(`Found ${files.length} JavaScript/TypeScript files`); for (const file of files) { try { const code = fs.readFileSync(file, 'utf-8'); const fileStats = fs.statSync(file); const lines = code.split('\n').length; const ext = path.extname(file).substring(1); stats.filesAnalyzed++; stats.linesOfCode += lines; stats.totalSize += fileStats.size; stats.fileTypes[ext] = (stats.fileTypes[ext] || 0) + 1; results.analyzedFiles.push({ name: path.basename(file), path: file, size: fileStats.size, lines: lines, type: ext }); const errors = findSyntaxErrors(code, file); if (errors.length > 0) { results.syntaxErrors.push(...errors); continue; } const unusedVars = findUnusedVariables(code, file); if (unusedVars.length > 0) { results.unusedVariables.push(...unusedVars); } const comments = findComments(code, file); if (comments.length > 0) { results.comments.push(...comments); } } catch (fileError) { debugLog(`Error analyzing file ${file}: ${fileError.message}`); } } const packageJsonPath = path.join(projectPath, 'package.json'); if (fs.existsSync(packageJsonPath)) { try { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); const dependencies = packageJson.dependencies || {}; const devDependencies = packageJson.devDependencies || {}; for (const pkg of [...Object.keys(dependencies), ...Object.keys(devDependencies)]) { let isUsed = false; for (const file of files) { try { const code = fs.readFileSync(file, 'utf-8'); if (code.includes(`import`) && code.includes(pkg) || code.includes(`require`) && code.includes(pkg)) { isUsed = true; break; } } catch (error) { } } if (!isUsed) { results.unusedPackages.push({ name: pkg, type: dependencies[pkg] ? 'dependency' : 'devDependency' }); } } } catch (packageError) { debugLog(`Error analyzing package.json: ${packageError.message}`); } } debugLog('Project analysis completed successfully'); } catch (error) { debugLog(`Project analysis failed: ${error.message}`); } return { results, stats }; } export default analyzeProject;