Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | import fs from 'fs-extra'; import path from 'path'; import chalk from 'chalk'; /** * Analyzes codebase patterns, file structures, and architecture indicators. * Returns structured pattern data for ideation scoring. */ /** * Main function to analyze codebase patterns. */ export async function analyzeCodePatterns(projectPath) { const patterns = { // File patterns hasPackageJson: await fs.pathExists(path.join(projectPath, 'package.json')), hasGoMod: await fs.pathExists(path.join(projectPath, 'go.mod')), hasCargoToml: await fs.pathExists(path.join(projectPath, 'Cargo.toml')), hasRequirementsTxt: await fs.pathExists(path.join(projectPath, 'requirements.txt')), hasPyprojectToml: await fs.pathExists(path.join(projectPath, 'pyproject.toml')), hasGemfile: await fs.pathExists(path.join(projectPath, 'Gemfile')), // Directory patterns directories: await detectDirectoryStructure(projectPath), // Architecture patterns architecture: await detectArchitecturePatterns(projectPath), // Testing patterns testing: await detectTestingPatterns(projectPath), // Source file patterns sourceFiles: await detectSourcePatterns(projectPath) }; return patterns; } /** * Detect directory structure patterns. */ async function detectDirectoryStructure(projectPath) { const directories = {}; const checkDir = async (dirName) => { const dirPath = path.join(projectPath, dirName); return await fs.pathExists(dirPath); }; const checkDirs = async (dirNames) => { for (const dir of dirNames) { if (await checkDir(dir)) { return dir; } } return null; }; // Common directory patterns directories.tests = await checkDirs(['tests', 'test', '__tests__', '__test__', 'spec']); directories.src = await checkDirs(['src', 'source', 'app']); directories.lib = await checkDir('lib'); directories.api = await checkDirs(['api', 'src/api', 'app/api']); directories.components = await checkDirs(['components', 'src/components', 'app/components']); directories.pages = await checkDirs(['pages', 'src/pages', 'app/pages']); directories.hooks = await checkDirs(['hooks', 'src/hooks']); directories.utils = await checkDirs(['utils', 'src/utils', 'lib/utils']); directories.models = await checkDirs(['models', 'src/models', 'app/models']); directories.controllers = await checkDirs(['controllers', 'src/controllers', 'app/controllers']); directories.routes = await checkDirs(['routes', 'src/routes', 'app/routes']); directories.middlewares = await checkDirs(['middleware', 'src/middleware', 'app/middleware']); directories.services = await checkDirs(['services', 'src/services', 'app/services']); directories.repositories = await checkDirs(['repositories', 'src/repositories', 'app/repositories']); directories.migrations = await checkDirs(['migrations', 'db/migrations', 'prisma/migrations']); directories.seeds = await checkDirs(['seeds', 'db/seeds', 'prisma/seeds']); directories.public = await checkDir('public'); directories.static = await checkDirs(['static', 'public/static']); directories.config = await checkDirs(['config', 'src/config', '.config']); directories.docker = await checkDir('docker'); directories.cicd = await checkDirs(['.github', '.gitlab-ci', '.circleci', 'scripts']); directories.infrastructure = await checkDirs(['infrastructure', 'infra', 'terraform', 'k8s']); return directories; } /** * Detect architecture patterns from file structure. */ async function detectArchitecturePatterns(projectPath) { const architecture = { pattern: 'unknown', indicators: [] }; const entries = await fs.readdir(projectPath, { withFileTypes: true }); const subdirs = entries.filter(e => e.isDirectory()).map(e => e.name); // Detect monorepo if (subdirs.includes('packages') || subdirs.includes('apps') || subdirs.includes('services')) { architecture.pattern = 'monorepo'; architecture.indicators.push('Has packages/ or apps/ directory'); } // Detect Next.js app router const appDir = path.join(projectPath, 'app'); if (await fs.pathExists(appDir)) { const appContents = await fs.readdir(appDir, { withFileTypes: true }); if (appContents.some(e => e.name.endsWith('.tsx') || e.name.endsWith('.ts'))) { architecture.pattern = 'nextjs-app-router'; architecture.indicators.push('App router structure detected'); } } // Detect pages router const pagesDir = path.join(projectPath, 'pages'); if (await fs.pathExists(pagesDir)) { const pagesContents = await fs.readdir(pagesDir, { withFileTypes: true }); if (pagesContents.some(e => e.name.endsWith('.tsx') || e.name.endsWith('.ts') || e.name.endsWith('.jsx') || e.name.endsWith('.js'))) { if (architecture.pattern === 'unknown') { architecture.pattern = 'nextjs-pages-router'; architecture.indicators.push('Pages router structure detected'); } } } // Detect MVC pattern const hasModels = await fs.pathExists(path.join(projectPath, 'models')) || await fs.pathExists(path.join(projectPath, 'src/models')) || await fs.pathExists(path.join(projectPath, 'app/models')); const hasViews = await fs.pathExists(path.join(projectPath, 'views')) || await fs.pathExists(path.join(projectPath, 'src/views')); const hasControllers = await fs.pathExists(path.join(projectPath, 'controllers')) || await fs.pathExists(path.join(projectPath, 'src/controllers')) || await fs.pathExists(path.join(projectPath, 'app/controllers')); if (hasModels && hasViews && hasControllers) { architecture.pattern = 'mvc'; architecture.indicators.push('MVC structure detected'); } // Detect layered architecture const hasServices = await fs.pathExists(path.join(projectPath, 'services')) || await fs.pathExists(path.join(projectPath, 'src/services')); const hasRepositories = await fs.pathExists(path.join(projectPath, 'repositories')) || await fs.pathExists(path.join(projectPath, 'src/repositories')); if (hasServices || hasRepositories) { if (architecture.pattern === 'unknown') { architecture.pattern = 'layered'; architecture.indicators.push('Layered architecture detected'); } } // Detect microservices if (subdirs.filter(d => d.startsWith('service-') || d.startsWith('svc-')).length >= 2) { architecture.pattern = 'microservices'; architecture.indicators.push('Multiple service directories detected'); } return architecture; } /** * Detect testing patterns and frameworks. */ async function detectTestingPatterns(projectPath) { const testing = { hasTests: false, frameworks: [], structure: 'none', indicators: [] }; const entries = await fs.readdir(projectPath, { withFileTypes: true }); const subdirs = entries.filter(e => e.isDirectory()).map(e => e.name); // Check for test directories const testDirs = subdirs.filter(d => d.includes('test') || d.includes('spec')); if (testDirs.length > 0) { testing.hasTests = true; testing.structure = testDirs[0]; testing.indicators.push(`Test directory: ${testDirs[0]}`); } // Scan for test files (limited depth to avoid long scans) const testFilePatterns = [ '*.test.js', '*.test.ts', '*.test.tsx', '*.spec.js', '*.spec.ts', '*.spec.tsx', '*_test.js', '*_test.ts', '*_test.tsx', '*_spec.js', '*_spec.ts', '*_spec.tsx' ]; // Quick scan of root and immediate subdirectories const searchDirs = ['.', 'src', 'lib', 'app', ...subdirs.slice(0, 5)]; for (const dir of searchDirs) { const dirPath = path.join(projectPath, dir); if (!await fs.pathExists(dirPath)) continue; try { const files = await fs.readdir(dirPath); const testFiles = files.filter(f => testFilePatterns.some(pattern => f.match(pattern.replace('*', '.*'))) ); if (testFiles.length > 0) { testing.hasTests = true; testing.indicators.push(`${testFiles.length} test file(s) in ${dir}`); break; } } catch (err) { // Skip directories we can't read } } // Check for test framework indicators const packageJsonPath = path.join(projectPath, 'package.json'); if (await fs.pathExists(packageJsonPath)) { try { const pkg = await fs.readJson(packageJsonPath); const deps = { ...pkg.dependencies, ...pkg.devDependencies }; if (deps.jest || deps['@jest/globals']) testing.frameworks.push('jest'); if (deps.mocha) testing.frameworks.push('mocha'); if (deps.chai) testing.frameworks.push('chai'); if (deps.vitest) testing.frameworks.push('vitest'); if (deps['@playwright/test']) testing.frameworks.push('playwright'); if (deps.cypress) testing.frameworks.push('cypress'); if (deps['@testing-library/react']) testing.frameworks.push('testing-library'); if (deps['@testing-library/vue']) testing.frameworks.push('testing-library'); if (deps.jasmine) testing.frameworks.push('jasmine'); if (deps['@supertest/supertest']) testing.frameworks.push('supertest'); } catch (err) { // Ignore parse errors } } // Check for Python test files const testPyPattern = /test_.*\.py$/; for (const dir of searchDirs) { const dirPath = path.join(projectPath, dir); if (!await fs.pathExists(dirPath)) continue; try { const files = await fs.readdir(dirPath); const testFiles = files.filter(f => testPyPattern.test(f)); if (testFiles.length > 0) { testing.hasTests = true; if (!testing.frameworks.includes('pytest')) { testing.frameworks.push('pytest'); } testing.indicators.push(`Python test files in ${dir}`); break; } } catch (err) { // Skip directories we can't read } } // Check for Go test files const testGoPattern = /_test\.go$/; for (const dir of searchDirs) { const dirPath = path.join(projectPath, dir); if (!await fs.pathExists(dirPath)) continue; try { const files = await fs.readdir(dirPath); const testFiles = files.filter(f => testGoPattern.test(f)); if (testFiles.length > 0) { testing.hasTests = true; testing.frameworks.push('go-testing'); testing.indicators.push(`Go test files in ${dir}`); break; } } catch (err) { // Skip directories we can't read } } return testing; } /** * Detect source file patterns and languages. */ async function detectSourcePatterns(projectPath) { const sourceFiles = { languages: [], fileCounts: {}, indicators: [] }; const languageExtensions = { javascript: ['.js', '.jsx', '.mjs', '.cjs'], typescript: ['.ts', '.tsx'], python: ['.py'], go: ['.go'], rust: ['.rs'], ruby: ['.rb'], java: ['.java'], csharp: ['.cs'], php: ['.php'], html: ['.html', '.htm'], css: ['.css', '.scss', '.sass', '.less'], sql: ['.sql'], yaml: ['.yml', '.yaml'], json: ['.json'], markdown: ['.md'] }; // Quick scan of common source directories const searchDirs = ['src', 'lib', 'app', 'components', 'pages', 'models', 'controllers', 'services']; for (const dir of searchDirs) { const dirPath = path.join(projectPath, dir); if (!await fs.pathExists(dirPath)) continue; try { await scanDirectory(dirPath, sourceFiles, languageExtensions, 2); // Max depth 2 if (Object.keys(sourceFiles.fileCounts).length > 0) break; } catch (err) { // Skip directories we can't read } } // Also scan root directory for source files try { const rootFiles = await fs.readdir(projectPath); const rootSourceFiles = rootFiles.filter(f => { const ext = path.extname(f).toLowerCase(); return Object.values(languageExtensions).flat().includes(ext); }); rootSourceFiles.forEach(f => { const ext = path.extname(f).toLowerCase(); for (const [lang, exts] of Object.entries(languageExtensions)) { if (exts.includes(ext)) { sourceFiles.fileCounts[lang] = (sourceFiles.fileCounts[lang] || 0) + 1; break; } } }); } catch (err) { // Ignore scan errors } // Determine primary languages const sortedLanguages = Object.entries(sourceFiles.fileCounts) .sort((a, b) => b[1] - a[1]) .map(([lang]) => lang); sourceFiles.languages = sortedLanguages; if (sortedLanguages.length > 0) { sourceFiles.indicators.push(`Primary language: ${sortedLanguages[0]}`); if (sortedLanguages.length > 1) { sourceFiles.indicators.push(`Secondary languages: ${sortedLanguages.slice(1).join(', ')}`); } } return sourceFiles; } /** * Helper: Recursively scan directory for source files. */ async function scanDirectory(dirPath, sourceFiles, languageExtensions, maxDepth, currentDepth = 0) { if (currentDepth >= maxDepth) return; try { const entries = await fs.readdir(dirPath, { withFileTypes: true }); for (const entry of entries) { if (entry.isDirectory()) { // Skip common directories to avoid long scans if (['node_modules', '.git', 'dist', 'build', '.next', 'coverage', '__pycache__'].includes(entry.name)) { continue; } await scanDirectory(path.join(dirPath, entry.name), sourceFiles, languageExtensions, maxDepth, currentDepth + 1); } else if (entry.isFile()) { const ext = path.extname(entry.name).toLowerCase(); for (const [lang, exts] of Object.entries(languageExtensions)) { if (exts.includes(ext)) { sourceFiles.fileCounts[lang] = (sourceFiles.fileCounts[lang] || 0) + 1; break; } } } } } catch (err) { // Skip directories we can't read } } |