Skip to content

Commit f3bee6b

Browse files
committed
feat(printer): 添加错误处理和依赖检查
- 在 Content 类中添加 errors 属性和 pushError 方法,用于收集错误信息 - 在打印结果中包含错误信息,以便用户查看 - 增加对 axios、zod、faker 等依赖项的检查 - 如果缺少必要的依赖项,将错误信息添加到打印结果中
1 parent 671d261 commit f3bee6b

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/printer/Content.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ export class Content {
4141
const parts = [...contentTypes].map(t => this.parts.get(t)).filter(Boolean) as string[][];
4242
return parts.map(p => p.join('\n')).join('\n\n');
4343
}
44+
45+
errors: string[] = [];
46+
pushError(message: string) {
47+
this.errors.push(message);
48+
}
4449
}

src/printer/index.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
OpenApiLatest_Schema,
1010
} from './helpers';
1111
import type { PrinterConfigs, PrinterOptions, PrintResults } from './types';
12+
import { isPackageExists } from 'local-pkg';
1213
import { pkgName, pkgVersion } from '../const';
1314
import { OpenAPIVersion } from '../types/openapi';
1415
import { toImportPath, toRelative } from '../utils/path';
@@ -304,22 +305,22 @@ export class Printer {
304305
main: {
305306
lang: 'ts',
306307
code: this.#mainContent.print(),
307-
errors: [],
308+
errors: this.#mainContent.errors,
308309
},
309310
type: {
310311
lang: 'ts',
311312
code: this.#typeContent.print(),
312-
errors: [],
313+
errors: this.#typeContent.errors,
313314
},
314315
zod: {
315316
lang: 'ts',
316317
code: this.#zodContent.print(),
317-
errors: [],
318+
errors: this.#zodContent.errors,
318319
},
319320
mock: {
320321
lang: 'ts',
321322
code: this.#mockContent.print(),
322-
errors: [],
323+
errors: this.#mockContent.errors,
323324
},
324325
};
325326
}
@@ -389,6 +390,20 @@ export class Printer {
389390
`import type * as ${TYPE_FILE_EXPORT_NAME} from "${toRelative(typeFile, mainFile)}";`,
390391
]);
391392

393+
// 依赖 axios
394+
if (!axiosImportFile || axiosImportFile === AXIOS_IMPORT_FILE) {
395+
if (!isPackageExists(AXIOS_IMPORT_FILE)) {
396+
this.#mockContent.pushError(`需要安装 ${AXIOS_IMPORT_FILE}`);
397+
}
398+
}
399+
400+
// 依赖 zod
401+
if ((runtimeValidate || runtimeMock) && (!zodImportFile || zodImportFile === ZOD_IMPORT_FILE)) {
402+
if (!isPackageExists(ZOD_IMPORT_FILE)) {
403+
this.#zodContent.errors.push(`需要安装 ${ZOD_IMPORT_FILE}`);
404+
}
405+
}
406+
392407
if (runtimeValidate) {
393408
const zodNames = [...this.#pathZodNames.values()].join(',');
394409
this.#mainContent.push('import', [
@@ -408,6 +423,20 @@ export class Printer {
408423
toImportString(FAKER_IMPORT_NAME, fakerImportName, fakerImportPath),
409424
`import {${zodNames}} from "${toRelative(zodFile, mainFile)}";`,
410425
]);
426+
427+
if (!fakerImportFile || fakerImportFile === FAKER_IMPORT_FILE) {
428+
if (!isPackageExists(FAKER_IMPORT_FILE)) {
429+
this.#mockContent.pushError(`需要安装 ${FAKER_IMPORT_FILE}`);
430+
}
431+
}
432+
433+
if (!isPackageExists('@anatine/zod-mock')) {
434+
this.#mockContent.pushError(`需要安装 @anatine/zod-mock`);
435+
}
436+
437+
if (!isPackageExists('axios-mock-adapter')) {
438+
this.#mockContent.pushError(`需要安装 axios-mock-adapter`);
439+
}
411440
}
412441

413442
this.#zodContent.push('import', toImportString(ZOD_IMPORT_NAME, zodImportName, zodImportPath));

0 commit comments

Comments
 (0)