33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import fs from 'fs-extra';
|
|
import yaml from 'js-yaml';
|
|
import { logStep, logSuccess, logError, logDetail } 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>) || {};
|
|
|
|
logDetail('analyze', `Input: ${swaggerFile}`);
|
|
logDetail('analyze', `Found ${tags.length} tags, ${Object.keys(paths).length} endpoints`);
|
|
tags.forEach((tag: unknown) => {
|
|
const t = tag as { name: string; description?: string };
|
|
logDetail('analyze', ` - ${t.name}: ${t.description || 'No description'}`);
|
|
});
|
|
|
|
logSuccess(`${tags.length} tags, ${Object.keys(paths).length} endpoints found`);
|
|
|
|
return { tags, paths, swagger };
|
|
} catch (error: unknown) {
|
|
const err = error as Error;
|
|
logError(`Error reading the Swagger file: ${err.message}`);
|
|
process.exit(1);
|
|
}
|
|
}
|