From 2557568b5e20fb353670cba8ae550f75303ed7c3 Mon Sep 17 00:00:00 2001 From: "S.Gromov" Date: Wed, 1 Apr 2026 19:03:28 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=BF=D1=83=D1=82=D1=8C=20=D0=BA=20=D1=88?= =?UTF-8?q?=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D0=B0=D0=BC,=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B4=D0=B0=D1=86=D0=B8=D1=8F=20=D1=80=D0=B5=D0=B7=D1=83?= =?UTF-8?q?=D0=BB=D1=8C=D1=82=D0=B0=D1=82=D0=B0=20=D0=B3=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - путь к шаблонам теперь корректен при установке через npm (resolve(__dirname, 'templates')) - проверка существования директории шаблонов перед генерацией - проверка что файлы реально созданы после генерации - добавлена мета-информация в package.json (автор, репозиторий) - переименован AI-PROJECT-OVERVIEW.md в AGENTS.md - версия 1.0.5 --- AI-PROJECT-OVERVIEW.md => AGENTS.md | 0 package.json | 14 +++++++-- src/generator.ts | 46 ++++++++++++++++++++++++----- tests/unit/cli.test.ts | 4 ++- 4 files changed, 52 insertions(+), 12 deletions(-) rename AI-PROJECT-OVERVIEW.md => AGENTS.md (100%) diff --git a/AI-PROJECT-OVERVIEW.md b/AGENTS.md similarity index 100% rename from AI-PROJECT-OVERVIEW.md rename to AGENTS.md diff --git a/package.json b/package.json index 1731997..6ffd4c5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gromlab/api-codegen", - "version": "1.0.3", + "version": "1.0.5", "description": "CLI tool to generate TypeScript API client from OpenAPI specification", "type": "module", "bin": { @@ -57,6 +57,14 @@ "generator", "cli" ], - "author": "", - "license": "MIT" + "author": "S.Gromov", + "license": "MIT", + "homepage": "https://gromlab.ru/gromov/api-codegen", + "repository": { + "type": "git", + "url": "https://gromlab.ru/gromov/api-codegen.git" + }, + "bugs": { + "url": "https://gromlab.ru/gromov/api-codegen/issues" + } } diff --git a/src/generator.ts b/src/generator.ts index 0b20a76..864daf5 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -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 { 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 { : '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 { }, }); - // Генерация успешна + // Проверяем, что файлы были сгенерированы + 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}`); } } diff --git a/tests/unit/cli.test.ts b/tests/unit/cli.test.ts index db4087b..f389d13 100644 --- a/tests/unit/cli.test.ts +++ b/tests/unit/cli.test.ts @@ -1,5 +1,6 @@ import { describe, test, expect, beforeEach, afterEach } from 'bun:test'; import { execa } from 'execa'; +import { readFileSync } from 'fs'; import { setupTest } from '../helpers/setup.js'; import { FIXTURES } from '../helpers/fixtures.js'; import { join } from 'path'; @@ -8,6 +9,7 @@ import { fileURLToPath } from 'url'; import { dirname } from 'path'; const __filename = fileURLToPath(import.meta.url); +const pkg = JSON.parse(readFileSync(join(dirname(__filename), '../../package.json'), 'utf-8')); const __dirname = dirname(__filename); const CLI_PATH = join(__dirname, '../../src/cli.ts'); @@ -70,7 +72,7 @@ describe('CLI', () => { test('должен отображать версию с --version', async () => { const { stdout } = await execa('bun', ['run', CLI_PATH, '--version']); - expect(stdout).toContain('1.0.0'); + expect(stdout).toContain(pkg.version); }); test('должен отображать help с --help', async () => {