fix: исправлен путь к шаблонам, добавлена валидация результата генерации
- путь к шаблонам теперь корректен при установке через npm (resolve(__dirname, 'templates')) - проверка существования директории шаблонов перед генерацией - проверка что файлы реально созданы после генерации - добавлена мета-информация в package.json (автор, репозиторий) - переименован AI-PROJECT-OVERVIEW.md в AGENTS.md - версия 1.0.5
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { generateApi as swaggerGenerateApi } from 'swagger-typescript-api';
|
||||
import { resolve } from 'path';
|
||||
import { resolve, join } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname } from 'path';
|
||||
import { existsSync } from 'fs';
|
||||
import type { GeneratorConfig } from './config.js';
|
||||
import { ensureDir, readJsonFile } from './utils/file.js';
|
||||
|
||||
@@ -16,7 +17,9 @@ export async function generate(config: GeneratorConfig): Promise<void> {
|
||||
await ensureDir(config.outputPath);
|
||||
|
||||
// Путь к кастомным шаблонам
|
||||
const templatesPath = resolve(__dirname, '../src/templates');
|
||||
// В dev-режиме (bun run src/cli.ts): __dirname = .../src, шаблоны в ./templates
|
||||
// В собранной версии (dist/cli.js): __dirname = .../dist, шаблоны в ./templates (скопированы при сборке)
|
||||
const templatesPath = resolve(__dirname, 'templates');
|
||||
|
||||
// Проверяем тип входного пути
|
||||
const isUrl = config.inputPath.startsWith('http://') || config.inputPath.startsWith('https://');
|
||||
@@ -45,11 +48,22 @@ export async function generate(config: GeneratorConfig): Promise<void> {
|
||||
: 'Api';
|
||||
}
|
||||
|
||||
// Проверяем, что директория с шаблонами существует
|
||||
if (!existsSync(templatesPath)) {
|
||||
throw new Error(
|
||||
`Templates directory not found: ${templatesPath}. ` +
|
||||
`Make sure the package is built correctly (run "bun run build").`
|
||||
);
|
||||
}
|
||||
|
||||
const outputDir = resolve(config.outputPath);
|
||||
const outputFileName = `${fileName}.ts`;
|
||||
|
||||
try {
|
||||
await swaggerGenerateApi({
|
||||
const { files } = await swaggerGenerateApi({
|
||||
...(isUrl ? { url } : { input: inputPath }),
|
||||
output: resolve(config.outputPath),
|
||||
fileName: `${fileName}.ts`,
|
||||
output: outputDir,
|
||||
fileName: outputFileName,
|
||||
httpClientType: 'fetch',
|
||||
modular: false,
|
||||
templates: templatesPath,
|
||||
@@ -112,10 +126,26 @@ export async function generate(config: GeneratorConfig): Promise<void> {
|
||||
},
|
||||
});
|
||||
|
||||
// Генерация успешна
|
||||
// Проверяем, что файлы были сгенерированы
|
||||
if (!files || files.length === 0) {
|
||||
throw new Error(
|
||||
'Generation completed but no files were produced. ' +
|
||||
'Check that the OpenAPI specification is valid.'
|
||||
);
|
||||
}
|
||||
|
||||
// Проверяем, что выходной файл существует на диске
|
||||
const outputFilePath = join(outputDir, outputFileName);
|
||||
if (!existsSync(outputFilePath)) {
|
||||
throw new Error(
|
||||
`Generation completed but output file was not created: ${outputFilePath}`
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ Generation failed:', error);
|
||||
throw error;
|
||||
if (error instanceof Error && error.message.startsWith('Generation completed')) {
|
||||
throw error;
|
||||
}
|
||||
throw new Error(`Generation failed: ${error instanceof Error ? error.message : error}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user