Skip to content

Commit 7ef510e

Browse files
committed
feat(printer): 添加文件头尾信息配置项
1 parent ae6f4a7 commit 7ef510e

File tree

10 files changed

+52
-36
lines changed

10 files changed

+52
-36
lines changed

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
<https://github.com/orgs/FrontEndDev-org/projects/1/views/1>
2+
3+
- [ ] 默认选项提取

src/printer/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,22 @@ export class Printer {
8181

8282
print(configs?: PrinterConfigs) {
8383
Object.assign(this.configs, configs);
84-
const { hideLintComments, hideInfo, hideComponents, hideImports, hidePaths } = this.configs;
85-
const eslintComments = [
84+
const { hideHeaders, hideFooters, hideInfo, hideComponents, hideImports, hidePaths } = this.configs;
85+
const defaultHeaders = [
8686
//
8787
'/* eslint-disable @typescript-eslint/ban-ts-comment */',
8888
'/* eslint-disable @typescript-eslint/no-explicit-any */',
89-
].join('\n');
89+
];
90+
const headers = (this.options?.headers || defaultHeaders).join('\n');
91+
const footers = (this.options?.footers || []).join('\n');
9092

9193
return [
92-
!hideLintComments && eslintComments,
94+
!hideHeaders && headers,
9395
!hideInfo && this._printInfo(),
9496
!hideImports && this._printImports(),
9597
!hideComponents && this._printComponents(),
9698
!hidePaths && this._printPaths(),
99+
!hideFooters && footers,
97100
]
98101
.filter(Boolean)
99102
.join('\n\n');

src/printer/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ export type PrinterOptions = {
106106
* @returns {string}
107107
*/
108108
operationIdNormalize?: OperationIdNormalize;
109+
110+
/**
111+
* 生成文件的头部信息
112+
*/
113+
headers?: string[];
114+
115+
/**
116+
* 生成文件的尾部信息
117+
*/
118+
footers?: string[];
109119
};
110120

111121
export type PrinterConfigs = {
@@ -119,7 +129,8 @@ export type PrinterConfigs = {
119129
*/
120130
file?: string;
121131

122-
hideLintComments?: boolean;
132+
hideHeaders?: boolean;
133+
hideFooters?: boolean;
123134
hideInfo?: boolean;
124135
hideImports?: boolean;
125136
hideComponents?: boolean;

test/printer/path.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test('1路径 + 1请求', () => {
1616

1717
expect(
1818
printer.print({
19-
hideLintComments: true,
19+
hideHeaders: true,
2020
hideInfo: true,
2121
hideImports: true,
2222
}),
@@ -94,7 +94,7 @@ test('1路径 + 2请求', () => {
9494

9595
expect(
9696
printer.print({
97-
hideLintComments: true,
97+
hideHeaders: true,
9898
hideInfo: true,
9999
hideImports: true,
100100
}),
@@ -154,7 +154,7 @@ test('1路径 + 1请求 + 1query', () => {
154154

155155
expect(
156156
printer.print({
157-
hideLintComments: true,
157+
hideHeaders: true,
158158
hideInfo: true,
159159
hideImports: true,
160160
}),
@@ -205,7 +205,7 @@ test('1路径 + 1请求 + 1query with duplicate', () => {
205205

206206
expect(
207207
printer.print({
208-
hideLintComments: true,
208+
hideHeaders: true,
209209
hideInfo: true,
210210
hideImports: true,
211211
}),
@@ -257,7 +257,7 @@ test('1路径 + 1请求 + 1path', () => {
257257

258258
expect(
259259
printer.print({
260-
hideLintComments: true,
260+
hideHeaders: true,
261261
hideInfo: true,
262262
hideImports: true,
263263
}),
@@ -315,7 +315,7 @@ test('1路径 + 1请求 + 2path', () => {
315315

316316
expect(
317317
printer.print({
318-
hideLintComments: true,
318+
hideHeaders: true,
319319
hideInfo: true,
320320
hideImports: true,
321321
}),
@@ -380,7 +380,7 @@ test('1路径 + 1请求 + 2query', () => {
380380

381381
expect(
382382
printer.print({
383-
hideLintComments: true,
383+
hideHeaders: true,
384384
hideInfo: true,
385385
hideImports: true,
386386
}),
@@ -456,7 +456,7 @@ test('1路径 + 1请求 + 2query + 1path', () => {
456456

457457
expect(
458458
printer.print({
459-
hideLintComments: true,
459+
hideHeaders: true,
460460
hideInfo: true,
461461
hideImports: true,
462462
}),
@@ -543,7 +543,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request primitive', () => {
543543

544544
expect(
545545
printer.print({
546-
hideLintComments: true,
546+
hideHeaders: true,
547547
hideInfo: true,
548548
hideImports: true,
549549
}),
@@ -638,7 +638,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object', () => {
638638

639639
expect(
640640
printer.print({
641-
hideLintComments: true,
641+
hideHeaders: true,
642642
hideInfo: true,
643643
hideImports: true,
644644
}),
@@ -745,7 +745,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object + 1response primitive
745745

746746
expect(
747747
printer.print({
748-
hideLintComments: true,
748+
hideHeaders: true,
749749
hideInfo: true,
750750
hideImports: true,
751751
}),
@@ -870,7 +870,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object + 1response object',
870870

871871
expect(
872872
printer.print({
873-
hideLintComments: true,
873+
hideHeaders: true,
874874
hideInfo: true,
875875
hideImports: true,
876876
}),

test/printer/ref-parameter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test('ref-parameter', () => {
4040
});
4141
expect(
4242
printer.print({
43-
hideLintComments: true,
43+
hideHeaders: true,
4444
hideInfo: true,
4545
hideImports: true,
4646
}),

test/printer/ref-request.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test('ref-request', () => {
4747
});
4848
expect(
4949
printer.print({
50-
hideLintComments: true,
50+
hideHeaders: true,
5151
hideInfo: true,
5252
hideImports: true,
5353
}),

test/printer/ref-response.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ test('ref-response', () => {
4949
});
5050
expect(
5151
printer.print({
52-
hideLintComments: true,
52+
hideHeaders: true,
5353
hideInfo: true,
5454
hideImports: true,
5555
}),
@@ -132,7 +132,7 @@ test('ref-response in object', () => {
132132
});
133133
expect(
134134
printer.print({
135-
hideLintComments: true,
135+
hideHeaders: true,
136136
hideInfo: true,
137137
hideImports: true,
138138
}),

test/printer/schemas.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('number', () => {
1919
});
2020
expect(
2121
printer.print({
22-
hideLintComments: true,
22+
hideHeaders: true,
2323
hideInfo: true,
2424
hideImports: true,
2525
}),
@@ -52,7 +52,7 @@ test('number enum', () => {
5252
});
5353
expect(
5454
printer.print({
55-
hideLintComments: true,
55+
hideHeaders: true,
5656
hideInfo: true,
5757
hideImports: true,
5858
}),
@@ -85,7 +85,7 @@ test('[number, null] enum', () => {
8585
});
8686
expect(
8787
printer.print({
88-
hideLintComments: true,
88+
hideHeaders: true,
8989
hideInfo: true,
9090
hideImports: true,
9191
}),
@@ -119,7 +119,7 @@ test('type[]', () => {
119119
});
120120
expect(
121121
printer.print({
122-
hideLintComments: true,
122+
hideHeaders: true,
123123
hideInfo: true,
124124
hideImports: true,
125125
}),
@@ -173,7 +173,7 @@ test('AllOf primitive', () => {
173173
});
174174
expect(
175175
printer.print({
176-
hideLintComments: true,
176+
hideHeaders: true,
177177
hideInfo: true,
178178
hideImports: true,
179179
}),
@@ -229,7 +229,7 @@ test('explicit array', () => {
229229
});
230230
expect(
231231
printer.print({
232-
hideLintComments: true,
232+
hideHeaders: true,
233233
hideInfo: true,
234234
hideImports: true,
235235
}),
@@ -275,7 +275,7 @@ test('generic array', () => {
275275
});
276276
expect(
277277
printer.print({
278-
hideLintComments: true,
278+
hideHeaders: true,
279279
hideInfo: true,
280280
hideImports: true,
281281
}),
@@ -326,7 +326,7 @@ test('explicit object', () => {
326326
});
327327
expect(
328328
printer.print({
329-
hideLintComments: true,
329+
hideHeaders: true,
330330
hideInfo: true,
331331
hideImports: true,
332332
}),
@@ -375,7 +375,7 @@ test('generic object', () => {
375375
});
376376
expect(
377377
printer.print({
378-
hideLintComments: true,
378+
hideHeaders: true,
379379
hideInfo: true,
380380
hideImports: true,
381381
}),
@@ -406,7 +406,7 @@ test('additionalProperties true', () => {
406406
});
407407
expect(
408408
printer.print({
409-
hideLintComments: true,
409+
hideHeaders: true,
410410
hideInfo: true,
411411
hideImports: true,
412412
}),
@@ -435,7 +435,7 @@ test('additionalProperties false', () => {
435435
});
436436
expect(
437437
printer.print({
438-
hideLintComments: true,
438+
hideHeaders: true,
439439
hideInfo: true,
440440
hideImports: true,
441441
}),
@@ -475,7 +475,7 @@ test('additionalProperties schema type', () => {
475475
});
476476
expect(
477477
printer.print({
478-
hideLintComments: true,
478+
hideHeaders: true,
479479
hideInfo: true,
480480
hideImports: true,
481481
}),
@@ -524,7 +524,7 @@ test('additionalProperties schema ref', () => {
524524
});
525525
expect(
526526
printer.print({
527-
hideLintComments: true,
527+
hideHeaders: true,
528528
hideInfo: true,
529529
hideImports: true,
530530
}),

test/printer/unique-name.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test('unique name', () => {
4141

4242
expect(
4343
printer.print({
44-
hideLintComments: true,
44+
hideHeaders: true,
4545
hideInfo: true,
4646
hideImports: true,
4747
}),

test/printer/v31.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test('v3.1 schema', () => {
4646

4747
expect(
4848
printer.print({
49-
hideLintComments: true,
49+
hideHeaders: true,
5050
hideInfo: true,
5151
hideImports: true,
5252
}),

0 commit comments

Comments
 (0)