Skip to content

Commit

Permalink
feat: 配置项由 openAPIs 数组调整为 modules 对象
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Jul 3, 2024
1 parent 8961587 commit 47c638c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
20 changes: 12 additions & 8 deletions src/generators/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import type {
OpenAPIOptions,
StrictGeneratorOptions,
} from './types';
import { isString } from '../utils/type-is';

export class Generator extends Emitter<GeneratorEmits> {
static defaults: StrictGeneratorOptions = {
cwd: process.cwd(),
dest: '/src/apis',
openAPIs: [],
modules: {},
};

options: StrictGeneratorOptions;
Expand All @@ -31,14 +32,16 @@ export class Generator extends Emitter<GeneratorEmits> {
}

async generate() {
const count = this.options.openAPIs.length;
const entries = Object.entries(this.options.modules);
const count = entries.length;
const payload: GeneratorPayload = { count };
this.emit('start', payload);

try {
let index = 0;
for (const openAPI of this.options.openAPIs) {
await this.generateOpenAPI(index, count, openAPI, this.options);
for (const [name, module] of entries) {
const openAPI: OpenAPIOptions = isString(module) ? { document: module } : module;
await this.generateOpenAPI(index, count, name, openAPI, this.options);
index++;
}
} catch (cause) {
Expand All @@ -50,9 +53,9 @@ export class Generator extends Emitter<GeneratorEmits> {
this.emit('end', payload);
}

protected async generateOpenAPI(index: number, count: number, openAPIOptions: OpenAPIOptions, generatorOptions: StrictGeneratorOptions) {
protected async generateOpenAPI(index: number, count: number, name: string, openAPIOptions: OpenAPIOptions, generatorOptions: StrictGeneratorOptions) {
const { cwd, dest, ...globalPrinter } = generatorOptions;
const { name, document, ...scopePrinter } = openAPIOptions;
const { document, ...scopePrinter } = openAPIOptions;
const fileName = `${name}.ts`;
const filePath = path.join(cwd, dest, fileName);

Expand All @@ -67,6 +70,7 @@ export class Generator extends Emitter<GeneratorEmits> {
const makePayload = (step: GeneratingStage): GeneratingPayload => ({
index,
count,
name,
stage: step,
options,
filePath,
Expand All @@ -81,12 +85,12 @@ export class Generator extends Emitter<GeneratorEmits> {
// 3. 输出
this.emit('process', makePayload('printing'));
const printer = new Printer(openAPIV3Document, printerOptions);
const text = printer.print();
const code = printer.print();

// 4. 写入
this.emit('process', makePayload('writing'));
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, await formatTsCode(text), 'utf8');
fs.writeFileSync(filePath, await formatTsCode(code), 'utf8');

this.emit('process', makePayload('generated'));
}
Expand Down
2 changes: 1 addition & 1 deletion src/generators/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Logger {
console.log(
chalk.cyanBright('▷'),
chalk.yellowBright(`${step}/${payload.count}`),
payload.options.name,
payload.name,
payload.stage,
payload.stage === 'generated' ? path.relative(payload.options.cwd, payload.filePath) : '',
);
Expand Down
10 changes: 3 additions & 7 deletions src/generators/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import type { OpenApi3 } from '../types/openapi';
type RequiredWith<T, K extends keyof T> = T & { [P in K]-?: T[P] };

export type OpenAPIOptions = PrinterOptions & {
/**
* openapi 的名称,将会生成 ${name}.ts 文件
*/
name: string;

/**
* openapi 的 document,可以是一个链接地址,也可以是本地路径,也可以是一个对象
*/
Expand All @@ -33,9 +28,9 @@ export type GeneratorOptions = PrinterOptions & {
prettierOptions?: Options;

/**
* openapi 配置列表
* openapi 模块配置
*/
openAPIs: OpenAPIOptions[];
modules: Record<string, OpenAPIOptions | string>;
};
export type StrictGeneratorOptions = RequiredWith<GeneratorOptions, 'cwd' | 'dest'>;

Expand All @@ -48,6 +43,7 @@ export interface GeneratorPayload {
export interface GeneratingPayload {
index: number;
count: number;
name: string;
stage: GeneratingStage;
options: GeneratingOptions;
filePath: string;
Expand Down

0 comments on commit 47c638c

Please sign in to comment.