Skip to content

Commit 34a4ef5

Browse files
committed
add options for custom templates
1 parent b24a97c commit 34a4ef5

File tree

6 files changed

+196
-16
lines changed

6 files changed

+196
-16
lines changed

bin/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ const params = program
1919
.option('--exportCore <value>', 'Write core files to disk', true)
2020
.option('--exportServices <value>', 'Write services to disk', true)
2121
.option('--exportModels <value>', 'Write models to disk', true)
22+
.option('--exportClient <value>', 'Write main Client file to disk', true)
23+
.option('--exportIndex <value>', 'Write Index to disk', true)
2224
.option('--exportSchemas <value>', 'Write schemas to disk', false)
2325
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
2426
.option('--postfix <value>', 'Service name postfix', 'Service')
2527
.option('--request <value>', 'Path to custom request file')
2628
.option('--serviceTemplate <value>', 'Path to custom service handlebars template to generate the service files')
29+
.option('--clientTemplate <value>', 'Path to custom client handlebars template to generate the client file')
30+
.option('--indexTemplate <value>', 'Path to custom index handlebars template to generate the index file')
2731
.parse(process.argv)
2832
.opts();
2933

@@ -40,11 +44,15 @@ if (OpenAPI) {
4044
exportCore: JSON.parse(params.exportCore) === true,
4145
exportServices: JSON.parse(params.exportServices) === true,
4246
exportModels: JSON.parse(params.exportModels) === true,
47+
exportClient: JSON.parse(params.exportClient) === true,
48+
exportIndex: JSON.parse(params.exportIndex) === true,
4349
exportSchemas: JSON.parse(params.exportSchemas) === true,
4450
indent: params.indent,
4551
postfix: params.postfix,
4652
request: params.request,
4753
serviceTemplate: params.serviceTemplate,
54+
clientTemplate: params.clientTemplate,
55+
indexTemplate: params.indexTemplate,
4856
})
4957
.then(() => {
5058
process.exit(0);

src/index.ts

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { isString } from './utils/isString';
88
import { postProcessClient } from './utils/postProcessClient';
99
import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
1010
import { writeClient } from './utils/writeClient';
11-
import { writeClientServicesCustomTemplate } from './utils/writeClientServicesCustomTemplate';
11+
import { writeClientClassCustomTemplate } from './utils/writeClientCustomTemplate/clientClass';
12+
import { writeClientIndexCustomTemplate } from './utils/writeClientCustomTemplate/index';
13+
import { writeClientServicesCustomTemplate } from './utils/writeClientCustomTemplate/services';
1214

1315
export { HttpClient } from './HttpClient';
1416
export { Indent } from './Indent';
@@ -23,11 +25,15 @@ export type Options = {
2325
exportCore?: boolean;
2426
exportServices?: boolean;
2527
exportModels?: boolean;
28+
exportClient?: boolean;
29+
exportIndex?: boolean;
2630
exportSchemas?: boolean;
2731
indent?: Indent;
2832
postfix?: string;
2933
request?: string;
3034
serviceTemplate?: string;
35+
clientTemplate?: string;
36+
indexTemplate?: string;
3137
write?: boolean;
3238
};
3339

@@ -60,11 +66,15 @@ export const generate = async ({
6066
exportCore = true,
6167
exportServices = true,
6268
exportModels = true,
69+
exportClient = true,
70+
exportIndex = true,
6371
exportSchemas = false,
6472
indent = Indent.SPACE_4,
6573
postfix = 'Service',
6674
request,
6775
serviceTemplate,
76+
clientTemplate,
77+
indexTemplate,
6878
write = true,
6979
}: Options): Promise<void> => {
7080
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
@@ -75,9 +85,9 @@ export const generate = async ({
7585
useOptions,
7686
});
7787

78-
if (serviceTemplate) {
79-
exportServices = false;
80-
}
88+
if (serviceTemplate) exportServices = false;
89+
if (clientTemplate) exportClient = false;
90+
if (indexTemplate) exportIndex = false;
8191

8292
let clientFinal;
8393
switch (openApiVersion) {
@@ -95,6 +105,8 @@ export const generate = async ({
95105
exportCore,
96106
exportServices,
97107
exportModels,
108+
exportClient,
109+
exportIndex,
98110
exportSchemas,
99111
indent,
100112
postfix,
@@ -118,6 +130,8 @@ export const generate = async ({
118130
exportCore,
119131
exportServices,
120132
exportModels,
133+
exportClient,
134+
exportIndex,
121135
exportSchemas,
122136
indent,
123137
postfix,
@@ -137,7 +151,40 @@ export const generate = async ({
137151
useUnionTypes,
138152
indent,
139153
postfix,
140-
serviceTemplate
154+
serviceTemplate,
155+
exportClient,
156+
clientName
157+
);
158+
}
159+
if (clientTemplate) {
160+
await writeClientClassCustomTemplate(
161+
clientFinal,
162+
output,
163+
httpClient,
164+
useOptions,
165+
useUnionTypes,
166+
indent,
167+
postfix,
168+
clientTemplate,
169+
clientName
170+
);
171+
}
172+
if (indexTemplate) {
173+
await writeClientIndexCustomTemplate(
174+
clientFinal,
175+
output,
176+
httpClient,
177+
useOptions,
178+
useUnionTypes,
179+
indent,
180+
postfix,
181+
indexTemplate,
182+
exportCore,
183+
exportServices,
184+
exportModels,
185+
exportSchemas,
186+
exportClient,
187+
clientName
141188
);
142189
}
143190
};

src/utils/writeClient.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export const writeClient = async (
4242
exportCore: boolean,
4343
exportServices: boolean,
4444
exportModels: boolean,
45+
exportClient: boolean,
46+
exportIndex: boolean,
4547
exportSchemas: boolean,
4648
indent: Indent,
4749
postfix: string,
@@ -92,12 +94,12 @@ export const writeClient = async (
9294
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes, indent);
9395
}
9496

95-
if (isDefined(clientName)) {
97+
if (isDefined(clientName) && exportClient) {
9698
await mkdir(outputPath);
9799
await writeClientClass(client, templates, outputPath, httpClient, clientName, indent, postfix);
98100
}
99101

100-
if (exportCore || exportServices || exportSchemas || exportModels) {
102+
if ((exportCore || exportServices || exportSchemas || exportModels) && exportIndex) {
101103
await mkdir(outputPath);
102104
await writeClientIndex(
103105
client,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { mkdir, readFile, remove } from 'fs-extra';
2+
import Handlebars from 'handlebars';
3+
import { resolve } from 'path';
4+
5+
import { Client } from '../../client/interfaces/Client';
6+
import { HttpClient } from '../../HttpClient';
7+
import { Indent } from '../../Indent';
8+
import { writeFile } from '../fileSystem';
9+
import { formatCode as f } from '../formatCode';
10+
import { formatIndentation as i } from '../formatIndentation';
11+
import { getHttpRequestName } from '../getHttpRequestName.js';
12+
import { registerHandlebarTemplates } from '../registerHandlebarTemplates';
13+
import { sortModelsByName } from '../sortModelsByName.js';
14+
import { sortServicesByName } from '../sortServicesByName.js';
15+
16+
export const writeClientClassCustomTemplate = async (
17+
client: Client,
18+
outputPath: string,
19+
httpClient: HttpClient,
20+
useOptions: boolean,
21+
useUnionTypes: boolean,
22+
indent: Indent,
23+
postfix: string,
24+
templatePath: string,
25+
clientName?: string
26+
) => {
27+
registerHandlebarTemplates({
28+
httpClient,
29+
useUnionTypes,
30+
useOptions,
31+
handlebars: Handlebars, // since we're not using precompiled templates, we need a different object here
32+
});
33+
Handlebars.registerHelper('capitalize', str => {
34+
return str.charAt(0).toUpperCase() + str.slice(1);
35+
});
36+
37+
const clientClassTemplate = Handlebars.compile(await readFile(templatePath, 'utf8'));
38+
39+
const clientClassDir = resolve(outputPath, 'clientClass');
40+
await remove(clientClassDir);
41+
await mkdir(clientClassDir);
42+
43+
const templateResult = clientClassTemplate({
44+
clientName,
45+
httpClient,
46+
postfix,
47+
server: client.server,
48+
version: client.version,
49+
models: sortModelsByName(client.models),
50+
services: sortServicesByName(client.services),
51+
httpRequest: getHttpRequestName(httpClient),
52+
});
53+
54+
await writeFile(resolve(outputPath, `${clientName}.ts`), i(f(templateResult), indent));
55+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { mkdir, readFile, remove } from 'fs-extra';
2+
import Handlebars from 'handlebars';
3+
import { resolve } from 'path';
4+
5+
import { Client } from '../../client/interfaces/Client';
6+
import { HttpClient } from '../../HttpClient';
7+
import { Indent } from '../../Indent';
8+
import { writeFile } from '../fileSystem';
9+
import { formatCode as f } from '../formatCode';
10+
import { formatIndentation as i } from '../formatIndentation';
11+
import { isDefined } from '../isDefined.js';
12+
import { registerHandlebarTemplates } from '../registerHandlebarTemplates';
13+
import { sortModelsByName } from '../sortModelsByName.js';
14+
import { sortServicesByName } from '../sortServicesByName.js';
15+
16+
export const writeClientIndexCustomTemplate = async (
17+
client: Client,
18+
outputPath: string,
19+
httpClient: HttpClient,
20+
useOptions: boolean,
21+
useUnionTypes: boolean,
22+
indent: Indent,
23+
postfix: string,
24+
templatePath: string,
25+
exportCore: boolean,
26+
exportServices: boolean,
27+
exportModels: boolean,
28+
exportSchemas: boolean,
29+
exportClient: boolean,
30+
clientName?: string
31+
) => {
32+
registerHandlebarTemplates({
33+
httpClient,
34+
useUnionTypes,
35+
useOptions,
36+
handlebars: Handlebars, // since we're not using precompiled templates, we need a different object here
37+
});
38+
Handlebars.registerHelper('capitalize', str => {
39+
return str.charAt(0).toUpperCase() + str.slice(1);
40+
});
41+
42+
const indexTemplate = Handlebars.compile(await readFile(templatePath, 'utf8'));
43+
44+
const dir = resolve(outputPath);
45+
await remove(dir);
46+
await mkdir(dir);
47+
48+
const templateResult = indexTemplate({
49+
serviceBaseUrl: client.server,
50+
exportCore,
51+
exportServices,
52+
exportModels,
53+
exportSchemas,
54+
useUnionTypes,
55+
postfix,
56+
clientName,
57+
server: client.server,
58+
version: client.version,
59+
models: sortModelsByName(client.models),
60+
services: sortServicesByName(client.services),
61+
exportClient: isDefined(clientName) && exportClient,
62+
});
63+
await writeFile(resolve(outputPath, 'index.ts'), i(f(templateResult), indent));
64+
};

src/utils/writeClientServicesCustomTemplate.ts renamed to src/utils/writeClientCustomTemplate/services.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { mkdir, readFile, remove } from 'fs-extra';
22
import Handlebars from 'handlebars';
33
import { resolve } from 'path';
44

5-
import { Client } from '../client/interfaces/Client';
6-
import { HttpClient } from '../HttpClient';
7-
import { Indent } from '../Indent';
8-
import { writeFile } from './fileSystem';
9-
import { formatCode } from './formatCode';
10-
import { formatIndentation } from './formatIndentation';
11-
import { registerHandlebarTemplates } from './registerHandlebarTemplates';
5+
import { Client } from '../../client/interfaces/Client';
6+
import { HttpClient } from '../../HttpClient';
7+
import { Indent } from '../../Indent';
8+
import { writeFile } from '../fileSystem';
9+
import { formatCode as f } from '../formatCode';
10+
import { formatIndentation as i } from '../formatIndentation';
11+
import { isDefined } from '../isDefined.js';
12+
import { registerHandlebarTemplates } from '../registerHandlebarTemplates';
1213

1314
export const writeClientServicesCustomTemplate = async (
1415
client: Client,
@@ -18,7 +19,9 @@ export const writeClientServicesCustomTemplate = async (
1819
useUnionTypes: boolean,
1920
indent: Indent,
2021
postfix: string,
21-
templatePath: string
22+
templatePath: string,
23+
exportClient: boolean,
24+
clientName?: string
2225
) => {
2326
registerHandlebarTemplates({
2427
httpClient,
@@ -45,7 +48,8 @@ export const writeClientServicesCustomTemplate = async (
4548
useUnionTypes,
4649
useOptions,
4750
postfix,
51+
exportClient: isDefined(clientName) && exportClient,
4852
});
49-
await writeFile(file, formatIndentation(formatCode(templateResult), indent));
53+
await writeFile(file, i(f(templateResult), indent));
5054
}
5155
};

0 commit comments

Comments
 (0)