Skip to content

Commit eef0119

Browse files
JsonLee12138Json_Lee
andauthored
fix: 处理优先读取命令行参数模式, 修复增量模式index的抛出被清空的问题 (#239)
* fix: merge index bug --------- Co-authored-by: Json_Lee <2622336659@qq.com>
1 parent 3a659f1 commit eef0119

File tree

2 files changed

+77
-36
lines changed

2 files changed

+77
-36
lines changed

src/bin/openapi.ts

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
import { program } from 'commander';
2+
import { type OptionValues, program } from 'commander';
33
import { pickBy } from 'lodash';
44
import { join } from 'path';
55

@@ -109,7 +109,52 @@ function getPath(path: string) {
109109
return join(process.cwd(), path);
110110
}
111111

112+
const baseGenerate = (_params_: OptionValues): GenerateServiceProps => {
113+
const input = getPath(_params_.input as string);
114+
const output = getPath(_params_.output as string);
115+
const options: GenerateServiceProps = {
116+
schemaPath: input,
117+
serversPath: output,
118+
requestLibPath: _params_.requestLibPath as string,
119+
enableLogging: JSON.parse(_params_.enableLogging as string) === true,
120+
priorityRule: _params_.priorityRule as IPriorityRule,
121+
includeTags: _params_.includeTags as string[],
122+
includePaths: _params_.includePaths as string[],
123+
excludeTags: _params_.excludeTags as string[],
124+
excludePaths: _params_.excludePaths as string[],
125+
requestOptionsType: _params_.requestOptionsType as string,
126+
apiPrefix: _params_.apiPrefix as string,
127+
isGenReactQuery: JSON.parse(_params_.isGenReactQuery as string) === true,
128+
reactQueryMode: _params_.reactQueryMode as IReactQueryMode,
129+
isGenJavaScript: JSON.parse(_params_.isGenJavaScript as string) === true,
130+
isDisplayTypeLabel:
131+
JSON.parse(_params_.isDisplayTypeLabel as string) === true,
132+
isGenJsonSchemas: JSON.parse(_params_.isGenJsonSchemas as string) === true,
133+
mockFolder: _params_.mockFolder as string,
134+
authorization: _params_.authorization as string,
135+
nullable: JSON.parse(_params_.nullable as string) === true,
136+
isTranslateToEnglishTag:
137+
JSON.parse(_params_.isTranslateToEnglishTag as string) === true,
138+
isOnlyGenTypeScriptType:
139+
JSON.parse(_params_.isOnlyGenTypeScriptType as string) === true,
140+
isCamelCase: JSON.parse(_params_.isCamelCase as string) === true,
141+
isSupportParseEnumDesc:
142+
JSON.parse(_params_.isSupportParseEnumDesc as string) === true,
143+
};
144+
return options;
145+
};
146+
112147
async function run() {
148+
if (params.input && params.output) {
149+
const options = baseGenerate(params);
150+
await generateService(
151+
pickBy(
152+
options,
153+
(value) => value !== null && value !== undefined && value !== ''
154+
) as GenerateServiceProps
155+
);
156+
process.exit(0);
157+
}
113158
const cnf = await readConfig<GenerateServiceProps | GenerateServiceProps[]>({
114159
fallbackName: 'openapi-ts-request',
115160
filePath: params.configFilePath as string,
@@ -148,40 +193,7 @@ async function run() {
148193
);
149194
process.exit(1);
150195
}
151-
const input = getPath(params.input as string);
152-
const output = getPath(params.output as string);
153-
154-
const options: GenerateServiceProps = {
155-
schemaPath: input,
156-
serversPath: output,
157-
requestLibPath: params.requestLibPath as string,
158-
enableLogging: JSON.parse(params.enableLogging as string) === true,
159-
priorityRule: params.priorityRule as IPriorityRule,
160-
includeTags: params.includeTags as string[],
161-
includePaths: params.includePaths as string[],
162-
excludeTags: params.excludeTags as string[],
163-
excludePaths: params.excludePaths as string[],
164-
requestOptionsType: params.requestOptionsType as string,
165-
apiPrefix: params.apiPrefix as string,
166-
isGenReactQuery: JSON.parse(params.isGenReactQuery as string) === true,
167-
reactQueryMode: params.reactQueryMode as IReactQueryMode,
168-
isGenJavaScript: JSON.parse(params.isGenJavaScript as string) === true,
169-
isDisplayTypeLabel:
170-
JSON.parse(params.isDisplayTypeLabel as string) === true,
171-
isGenJsonSchemas:
172-
JSON.parse(params.isGenJsonSchemas as string) === true,
173-
mockFolder: params.mockFolder as string,
174-
authorization: params.authorization as string,
175-
nullable: JSON.parse(params.nullable as string) === true,
176-
isTranslateToEnglishTag:
177-
JSON.parse(params.isTranslateToEnglishTag as string) === true,
178-
isOnlyGenTypeScriptType:
179-
JSON.parse(params.isOnlyGenTypeScriptType as string) === true,
180-
isCamelCase: JSON.parse(params.isCamelCase as string) === true,
181-
isSupportParseEnumDesc:
182-
JSON.parse(params.isSupportParseEnumDesc as string) === true,
183-
};
184-
196+
const options = baseGenerate(params);
185197
await generateService(
186198
pickBy(
187199
options,

src/generator/merge.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
type ClassDeclarationStructure,
33
type EnumDeclarationStructure,
4+
type ExportDeclarationStructure,
45
type FunctionDeclarationOverloadStructure,
56
type FunctionDeclarationStructure,
67
type ImportDeclarationStructure,
@@ -19,7 +20,6 @@ const sortMapByKey = <T = unknown>(map: Map<string, T>) => {
1920
.sort((a, b) => a[0].localeCompare(b[0]))
2021
.map(([k, v]) => ({ k, v }));
2122
};
22-
2323
export class Merger {
2424
#project: Project;
2525
#sourceFile: SourceFile;
@@ -56,6 +56,30 @@ export class Merger {
5656
this.#mergedFile = this.#project.createSourceFile('_merged_.ts');
5757
}
5858

59+
// TODO: 需要重构合并导出, 主要是export * from 'xxx' 和 export {a} from 'xxx' 的合并
60+
#mergeExport(destFile: SourceFile) {
61+
const exportMap = new Map<string, ExportDeclarationStructure>();
62+
// const exportArray: ExportDeclarationStructure[] = [];
63+
this.#sourceFile.getExportDeclarations()?.forEach((e) => {
64+
const eStructure = e.getStructure();
65+
const path = e.getModuleSpecifierValue();
66+
if (path) {
67+
exportMap.set(path, eStructure);
68+
}
69+
});
70+
71+
destFile.getExportDeclarations()?.forEach((e) => {
72+
const eStructure = e.getStructure();
73+
const path = e.getModuleSpecifierValue();
74+
if (path) {
75+
exportMap.set(path, eStructure);
76+
}
77+
});
78+
sortMapByKey<ExportDeclarationStructure>(exportMap).forEach(({ v }) => {
79+
this.#mergedFile.addExportDeclaration(v);
80+
});
81+
}
82+
5983
#mergeClass(destFile: SourceFile) {
6084
const classMap = new Map<string, ClassDeclarationStructure>();
6185
this.#sourceFile.getClasses().forEach((c) => {
@@ -258,6 +282,11 @@ export class Merger {
258282
this.#mergeEnums(destFile);
259283
this.#mergeInterfaces(destFile);
260284
this.#mergeFunctions(destFile);
285+
this.#mergeExport(destFile);
286+
// console.log(source, '>>>>');
287+
// if (srcPath?.includes('index.ts')) {
288+
// console.log(source, '>>>>');
289+
// }
261290
const leadingComment = this.#leadingCommentRanges.join('\n');
262291
if (leadingComment) {
263292
return `${leadingComment}\n${this.#mergedFile.getFullText()}`;

0 commit comments

Comments
 (0)