/** * Main code indexer class that orchestrates the complete code analysis pipeline. * * The CodeIndexer provides comprehensive analysis of TypeScript/JavaScript codebases, * extracting file structures, dependencies, and code signatures. It supports both * full project scanning and incremental updates for large projects. * * Features: * - Adaptive batch processing for memory efficiency * - Resource monitoring and automatic optimization * - Incremental file updates * - Comprehensive dependency resolution * - Project statistics and circular dependency detection * * @example Basic usage * ```typescript * import { CodeIndexer } from 'code-map'; * * // Create indexer for current directory * const indexer = new CodeIndexer(); * * // Process entire project * const index = await indexer.processProject(); * console.log(`Indexed ${index.metadata.totalFiles} files`); * ``` * * @example With filtering and progress tracking * ```typescript * import { CodeIndexer } from 'code-map'; * * const indexer = new CodeIndexer('./src', { * include: ['src/components/', 'src/utils/'], * exclude: ['test/', 'node_modules/'] * }); * * const index = await indexer.processProject((progress) => { * console.log(`${progress.step}: ${progress.current}/${progress.total}`); * }); * ``` * * @example Incremental updates * ```typescript * // Update a single file in existing index * const updatedIndex = await indexer.updateFile('src/utils.ts', existingIndex); * * // Remove a file from index * const cleanedIndex = indexer.removeFile('src/old-file.ts', existingIndex); * ``` */ import type { ProjectIndex, FilterOptions } from '../types/index.js'; /** * Configuration options for resource monitoring during batch processing. * * The indexer uses adaptive batch processing to optimize performance while * preventing memory exhaustion on large projects. These settings control * how the batch size adapts based on available system resources. * * @example Custom resource configuration * ```typescript * const indexer = new CodeIndexer('./src', {}, { * maxMemoryThresholdMB: 256, // Reduce batch size above 256MB * maxBatchSize: 20, // Never process more than 20 files at once * yieldInterval: 10 // Yield control every 10 files * }); * ``` */ export interface ResourceConfig { /** Maximum memory usage threshold (MB) before reducing batch size */ maxMemoryThresholdMB?: number; /** Minimum batch size to maintain processing efficiency */ minBatchSize?: number; /** Maximum batch size to prevent resource exhaustion */ maxBatchSize?: number; /** Number of processed files before yielding control */ yieldInterval?: number; /** Factor to reduce batch size when memory threshold is exceeded */ batchReductionFactor?: number; /** Factor to increase batch size when memory usage is low */ batchIncreaseFactor?: number; } /** * Real-time resource monitoring information during batch processing. * * This interface provides insights into current system resource usage * and processing performance, allowing for informed decisions about * batch size adjustments. */ export interface ResourceMonitor { /** Current memory usage in MB */ memoryUsageMB: number; /** Memory usage as percentage of total system memory */ memoryPercentage: number; /** Current batch size being used */ currentBatchSize: number; /** Number of batches processed so far */ batchesProcessed: number; /** Average processing time per file in milliseconds */ avgProcessingTimeMs: number; } /** * Main code indexer class that orchestrates the complete code analysis pipeline. * * @example Create indexer with default settings * ```typescript * const indexer = new CodeIndexer(); * ``` * * @example Create indexer with custom root and filters * ```typescript * const indexer = new CodeIndexer('./packages', { * include: ['src/'], * exclude: ['test/'] * }); * ``` */ export declare class CodeIndexer { private rootPath; private filterOptions; private resourceConfig; private totalSystemMemoryMB; private cpuCount; /** * Create a new CodeIndexer instance. * * @param rootPath - Root directory to analyze (defaults to current working directory) * @param filterOptions - File filtering options for inclusion/exclusion patterns * @param resourceConfig - Configuration for adaptive batch processing and resource management * * @example Basic usage * ```typescript * const indexer = new CodeIndexer(); * ``` * * @example With filtering * ```typescript * const indexer = new CodeIndexer('./src', { * include: ['src/'], * exclude: ['test/', 'node_modules/'] * }); * ``` * * @example With resource limits * ```typescript * const indexer = new CodeIndexer('./src', {}, { * maxMemoryThresholdMB: 512, * maxBatchSize: 25 * }); * ``` */ constructor(rootPath?: string, filterOptions?: FilterOptions, resourceConfig?: ResourceConfig); /** * Factory method to create a new CodeIndexer instance. * * This method provides the same functionality as the constructor but with * a more explicit interface. Useful for dependency injection or when you * prefer factory patterns. * * @param rootPath - Root directory to analyze (defaults to current working directory) * @param filterOptions - File filtering options for inclusion/exclusion patterns * @param resourceConfig - Configuration for adaptive batch processing * @returns New CodeIndexer instance * * @example * ```typescript * const indexer = CodeIndexer.create('./src', { * include: ['src/'], * exclude: ['test/'] * }); * ``` */ static create(rootPath?: string, filterOptions?: FilterOptions, resourceConfig?: ResourceConfig): CodeIndexer; /** * Process the entire project and generate a complete ProjectIndex. * * This is the main method for analyzing a codebase. It discovers all matching * files, builds the directory tree, parses each file for code signatures, * and resolves all dependencies between files. * * The process is optimized with: * - Parallel file processing with adaptive batch sizes * - Memory monitoring and automatic resource management * - Progress callbacks for long-running operations * - Graceful error handling for individual files * * @param progressCallback - Optional callback to receive progress updates during processing * @returns Promise resolving to the complete project index * * @throws {Error} When no supported files are found or processing fails * * @example Basic project analysis * ```typescript * const indexer = new CodeIndexer('./src'); * const index = await indexer.processProject(); * * console.log(`Found ${index.metadata.totalFiles} files`); * console.log(`Dependencies: ${index.edges.length}`); * ``` * * @example With progress tracking * ```typescript * const index = await indexer.processProject((progress) => { * console.log(`${progress.step}: ${progress.current}/${progress.total}`); * }); * ``` * * @example Error handling * ```typescript * try { * const index = await indexer.processProject(); * // Process successful index... * } catch (error) { * console.error('Failed to process project:', error.message); * } * ``` */ processProject(progressCallback?: (progress: { step: string; current: number; total: number; }) => void): Promise; /** * Update a single file in an existing project index. * * This method provides efficient incremental updates without requiring * a full project rescan. It's ideal for file watchers, IDE integrations, * or any scenario where you need to keep an index up-to-date as files change. * * The update process: * 1. Validates the file path and type * 2. Checks if file matches current filter patterns * 3. Parses the updated file for new signatures * 4. Resolves dependencies for the updated file * 5. Rebuilds dependency edges involving this file * 6. Updates metadata timestamps * * @param filePath - Relative path to the file to update (relative to root path) * @param existingIndex - The existing project index to update * @returns Promise resolving to the updated project index * * @throws {Error} When file type is unsupported or parsing fails * * @example Update a file after changes * ```typescript * // Initial index * const index = await indexer.processProject(); * * // File changed - update just that file * const updatedIndex = await indexer.updateFile('src/utils.ts', index); * * console.log('Index updated at:', updatedIndex.metadata.updatedAt); * ``` * * @example File watcher integration * ```typescript * let currentIndex = await indexer.processProject(); * * fileWatcher.on('change', async (filePath) => { * try { * currentIndex = await indexer.updateFile(filePath, currentIndex); * console.log(`Updated ${filePath} in index`); * } catch (error) { * console.warn(`Failed to update ${filePath}:`, error.message); * } * }); * ``` */ updateFile(filePath: string, existingIndex: ProjectIndex): Promise; /** * Remove a file from an existing project index. * * This method efficiently removes a file and all its associated data * from the index without requiring a full rescan. It's useful for * handling file deletions or when files no longer match filter patterns. * * The removal process: * 1. Removes file from the files map * 2. Removes file from the nodes array * 3. Removes all dependency edges involving the file * 4. Updates metadata and file counts * 5. Rebuilds the directory tree structure * * @param filePath - Relative path to the file to remove (relative to root path) * @param existingIndex - The existing project index to update * @returns Updated project index with the file removed * * @example Remove a deleted file * ```typescript * // File was deleted - remove from index * const updatedIndex = indexer.removeFile('src/old-utils.ts', currentIndex); * * console.log(`Removed file. New total: ${updatedIndex.metadata.totalFiles}`); * ``` * * @example File watcher deletion handling * ```typescript * fileWatcher.on('delete', (filePath) => { * currentIndex = indexer.removeFile(filePath, currentIndex); * console.log(`Removed ${filePath} from index`); * }); * ``` */ removeFile(filePath: string, existingIndex: ProjectIndex): ProjectIndex; /** * Parse files in parallel with adaptive batch processing for better performance and resource management * @param files - Array of file paths to parse * @param progressCallback - Optional progress callback * @returns Map of file paths to their FileInfo */ private parseFilesInParallel; /** * Calculate initial batch size based on system resources and file characteristics * @param files - Array of files to be processed * @returns Initial batch size */ private calculateInitialBatchSize; /** * Get current resource monitoring information * @param batchesProcessed - Number of batches processed so far * @param processingTimes - Array of recent processing times per file * @param currentBatchSize - Current batch size being used * @returns Resource monitor information */ private getResourceMonitor; /** * Adapt batch size based on current resource usage * @param currentBatchSize - Current batch size * @param resourceMonitor - Current resource monitoring information * @returns New batch size */ private adaptBatchSize; /** * Yield control to prevent blocking the event loop * @returns Promise that resolves on next tick */ private yieldControl; /** * Resolve dependencies and build dependency graph * @param fileInfos - Map of file paths to their FileInfo * @param allFiles - Array of all discovered files * @returns Object with edges and updated file infos */ private resolveDependencies; /** * Rebuild dependency edges for a specific file * @param filePath - File path to rebuild edges for * @param index - Project index * @returns Updated edges array */ private rebuildEdgesForFile; /** * Check if a file would be included with current filter options * @param filePath - Relative file path to check * @returns True if file would be included */ private wouldFileBeIncluded; /** * Check if a file path matches a glob pattern * @param filePath - File path to test * @param pattern - Glob pattern * @returns True if file matches pattern */ private matchesGlobPattern; /** * Remove a file from the index (helper method) * @param filePath - Relative path to the file to remove * @param existingIndex - Existing project index * @returns Updated project index */ private removeFileFromIndex; /** * Calculate comprehensive statistics for a project index. * * This static method analyzes the project structure to provide insights * into code organization, dependency patterns, and potential issues. * * Statistics include: * - File and dependency counts * - Average dependencies per file * - Circular dependency detection * - Entry points (files with no dependencies) * - Leaf files (files that nothing depends on) * * @param index - The project index to analyze * @returns Statistics object with comprehensive project metrics * * @example Basic project statistics * ```typescript * const index = await indexer.processProject(); * const stats = CodeIndexer.getProjectStats(index); * * console.log(`Total files: ${stats.totalFiles}`); * console.log(`Total dependencies: ${stats.totalDependencies}`); * console.log(`Average dependencies per file: ${stats.averageDependenciesPerFile}`); * ``` * * @example Detect circular dependencies * ```typescript * const stats = CodeIndexer.getProjectStats(index); * * if (stats.circularDependencies.length > 0) { * console.warn('Circular dependencies detected:'); * stats.circularDependencies.forEach((cycle, i) => { * console.log(` Cycle ${i + 1}: ${cycle.join(' → ')}`); * }); * } * ``` * * @example Identify entry points and leaf files * ```typescript * const stats = CodeIndexer.getProjectStats(index); * * console.log('Entry points (no dependencies):', stats.entryPoints); * console.log('Leaf files (nothing depends on them):', stats.leafFiles); * ``` */ static getProjectStats(index: ProjectIndex): { totalFiles: number; totalDependencies: number; averageDependenciesPerFile: number; circularDependencies: string[][]; entryPoints: string[]; leafFiles: string[]; }; } //# sourceMappingURL=indexer.d.ts.map