feat: добавить split-режим генерации REST-клиента
- добавлен режим генерации single, split и both - добавлены отдельные operation-файлы и createApiClient - удалена генерация SWR-хуков и зависимости React/SWR - обновлены CLI, шаблоны, примеры, документация и тесты - версия пакета повышена до 3.0.0
This commit is contained in:
33
src/cli.ts
33
src/cli.ts
@@ -15,14 +15,26 @@ const pkg = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf-8')
|
||||
|
||||
const program = new Command();
|
||||
|
||||
const translateCommanderError = (message: string): string => {
|
||||
return message
|
||||
.replace(/^error:/, 'ошибка:')
|
||||
.replace('unknown option', 'неизвестная опция')
|
||||
.replace('too many arguments', 'слишком много аргументов')
|
||||
.replace('option', 'опция');
|
||||
};
|
||||
|
||||
program
|
||||
.name('api-codegen')
|
||||
.description('Generate TypeScript API client from OpenAPI specification')
|
||||
.configureOutput({
|
||||
writeErr: (message) => process.stderr.write(translateCommanderError(message)),
|
||||
})
|
||||
.description('Генерация TypeScript API клиента из OpenAPI спецификации')
|
||||
.version(pkg.version)
|
||||
.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)')
|
||||
.option('--swr', 'Generate SWR hooks for React')
|
||||
.option('-i, --input <path>', 'Путь к OpenAPI спецификации (JSON/YAML файл или URL)')
|
||||
.option('-o, --output <path>', 'Директория для сохранения сгенерированных файлов')
|
||||
.option('-n, --name <name>', 'Имя монолитного клиента без расширения .ts')
|
||||
.option('--mode <mode>', 'Режим генерации: single, split, both', 'single')
|
||||
.option('--single-file', 'Устаревший алиас для --mode single')
|
||||
.action(async (options) => {
|
||||
try {
|
||||
// Создание конфигурации
|
||||
@@ -30,7 +42,7 @@ program
|
||||
inputPath: options.input,
|
||||
outputPath: options.output,
|
||||
fileName: options.name,
|
||||
useSwr: options.swr || false,
|
||||
mode: options.singleFile ? 'single' : options.mode,
|
||||
};
|
||||
|
||||
// Валидация конфигурации
|
||||
@@ -39,7 +51,7 @@ program
|
||||
// Проверка существования входного файла (только для локальных файлов)
|
||||
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`));
|
||||
console.error(chalk.red(`\n❌ Ошибка: входной файл не найден: ${config.inputPath}\n`));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -47,15 +59,12 @@ program
|
||||
// Генерация API
|
||||
await generate(config as GeneratorConfig);
|
||||
|
||||
console.log(chalk.green('\n✨ API client generated successfully!\n'));
|
||||
console.log(chalk.green('\n✨ API клиент успешно сгенерирован!\n'));
|
||||
} catch (error) {
|
||||
console.error(chalk.red('\n❌ Error:'), error instanceof Error ? error.message : error);
|
||||
console.error(chalk.red('\n❌ Ошибка:'), error instanceof Error ? error.message : error);
|
||||
console.error();
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
program.parse();
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user