import fs from 'fs-extra'; import yaml from 'js-yaml'; import { logStep, logInfo, logError } from '../utils/logger'; import type { SwaggerAnalysis } from '../types'; /** Parses an OpenAPI/Swagger file and extracts tags, paths and the full document. */ export function analyzeSwagger(swaggerFile: string): SwaggerAnalysis { logStep('Analysing OpenAPI file...'); try { const fileContent = fs.readFileSync(swaggerFile, 'utf8'); const swagger = yaml.load(fileContent) as Record; const tags = Array.isArray(swagger.tags) ? swagger.tags : []; const paths = (swagger.paths as Record) || {}; logInfo(`Found ${tags.length} tags in the API`); logInfo(`Found ${Object.keys(paths).length} endpoints`); tags.forEach((tag: unknown) => { const t = tag as { name: string; description?: string }; logInfo(` - ${t.name}: ${t.description || 'No description'}`); }); return { tags, paths, swagger }; } catch (error: unknown) { const err = error as Error; logError(`Error reading the Swagger file: ${err.message}`); process.exit(1); } }