Files
openapi-clean-arch-gen/src/swagger/analyzer.ts

32 lines
1.1 KiB
TypeScript

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<string, unknown>;
const tags = Array.isArray(swagger.tags) ? swagger.tags : [];
const paths = (swagger.paths as Record<string, unknown>) || {};
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);
}
}