UNPKG

repomix

Version:

A tool to pack repository contents to single file for AI consumption

90 lines (89 loc) 3.14 kB
import * as path from 'node:path'; import { Parser, Query } from 'web-tree-sitter'; import { RepomixError } from '../../shared/errorHandle.js'; import { logger } from '../../shared/logger.js'; import { getLanguageConfigByExtension, getLanguageConfigByName } from './languageConfig.js'; import { loadLanguage } from './loadLanguage.js'; export class LanguageParser { loadedResources = new Map(); initialized = false; getFileExtension(filePath) { return path.extname(filePath).toLowerCase().slice(1); } async prepareLang(name) { try { const config = getLanguageConfigByName(name); if (!config) { throw new RepomixError(`Language configuration not found for: ${name}`); } const lang = await loadLanguage(name); const parser = new Parser(); parser.setLanguage(lang); const query = new Query(lang, config.query); const strategy = config.createStrategy(); const resources = { lang: name, parser, query, strategy, }; this.loadedResources.set(name, resources); return resources; } catch (error) { const message = error instanceof Error ? error.message : String(error); throw new RepomixError(`Failed to prepare language ${name}: ${message}`); } } async getResources(name) { if (!this.initialized) { throw new RepomixError('LanguageParser is not initialized. Call init() first.'); } const resources = this.loadedResources.get(name); if (!resources) { return this.prepareLang(name); } return resources; } async getParserForLang(name) { const resources = await this.getResources(name); return resources.parser; } async getQueryForLang(name) { const resources = await this.getResources(name); return resources.query; } async getStrategyForLang(name) { const resources = await this.getResources(name); return resources.strategy; } guessTheLang(filePath) { const ext = this.getFileExtension(filePath); const config = getLanguageConfigByExtension(ext); if (!config) { logger.debug(`No language configuration found for extension: ${ext}`); } return config?.name; } async init() { if (this.initialized) { return; } try { await Parser.init(); this.initialized = true; } catch (error) { const message = error instanceof Error ? error.message : String(error); throw new RepomixError(`Failed to initialize parser: ${message}`); } } async dispose() { for (const resources of this.loadedResources.values()) { resources.parser.delete(); logger.debug(`Deleted parser for language: ${resources.lang}`); } this.loadedResources.clear(); this.initialized = false; } }