2025-10-26 22:30:58 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
import { Command } from 'commander';
|
|
|
|
|
import chalk from 'chalk';
|
2026-04-01 18:55:41 +03:00
|
|
|
import { readFileSync } from 'fs';
|
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
import { dirname, join } from 'path';
|
2025-10-26 22:30:58 +03:00
|
|
|
import { validateConfig, type GeneratorConfig } from './config.js';
|
|
|
|
|
import { generate } from './generator.js';
|
|
|
|
|
import { fileExists } from './utils/file.js';
|
|
|
|
|
|
2026-04-01 18:55:41 +03:00
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
|
const __dirname = dirname(__filename);
|
|
|
|
|
const pkg = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf-8'));
|
|
|
|
|
|
2025-10-26 22:30:58 +03:00
|
|
|
const program = new Command();
|
|
|
|
|
|
|
|
|
|
program
|
|
|
|
|
.name('api-codegen')
|
|
|
|
|
.description('Generate TypeScript API client from OpenAPI specification')
|
2026-04-01 18:55:41 +03:00
|
|
|
.version(pkg.version)
|
2025-10-26 22:30:58 +03:00
|
|
|
.requiredOption('-i, --input <path>', 'Path to OpenAPI specification file (JSON or YAML)')
|
|
|
|
|
.requiredOption('-o, --output <path>', 'Output directory for generated files')
|
|
|
|
|
.option('-n, --name <name>', 'Name of generated file (without extension)')
|
2025-10-28 10:51:14 +03:00
|
|
|
.option('--swr', 'Generate SWR hooks for React')
|
2025-10-26 22:30:58 +03:00
|
|
|
.action(async (options) => {
|
|
|
|
|
try {
|
|
|
|
|
// Создание конфигурации
|
|
|
|
|
const config: Partial<GeneratorConfig> = {
|
|
|
|
|
inputPath: options.input,
|
|
|
|
|
outputPath: options.output,
|
|
|
|
|
fileName: options.name,
|
2025-10-28 10:51:14 +03:00
|
|
|
useSwr: options.swr || false,
|
2025-10-26 22:30:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Валидация конфигурации
|
|
|
|
|
validateConfig(config);
|
|
|
|
|
|
|
|
|
|
// Проверка существования входного файла (только для локальных файлов)
|
|
|
|
|
if (!config.inputPath!.startsWith('http://') && !config.inputPath!.startsWith('https://')) {
|
|
|
|
|
if (!(await fileExists(config.inputPath!))) {
|
|
|
|
|
console.error(chalk.red(`\n❌ Error: Input file not found: ${config.inputPath}\n`));
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Генерация API
|
|
|
|
|
await generate(config as GeneratorConfig);
|
|
|
|
|
|
2025-10-28 10:51:14 +03:00
|
|
|
console.log(chalk.green('\n✨ API client generated successfully!\n'));
|
2025-10-26 22:30:58 +03:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error(chalk.red('\n❌ Error:'), error instanceof Error ? error.message : error);
|
|
|
|
|
console.error();
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
program.parse();
|
|
|
|
|
|
2026-04-01 18:55:41 +03:00
|
|
|
|
|
|
|
|
|