Skip to content

Commit 946cd8b

Browse files
authored
Merge pull request #3 from hsume2/anna/different-output-paths
2 parents 6195566 + d66f4cb commit 946cd8b

15 files changed

+170
-23
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ $ openapi --help
4141
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
4242
-o, --output <value> Output directory (required)
4343
-c, --client <value> HTTP client to generate [fetch, xhr, node] (default: "fetch")
44+
--outputCore <value> The relative ___location of the core output directory
45+
--outputModels <value> The relative ___location of the models output directory
46+
--outputSchemas <value> The relative ___location of the schemas output directory
47+
--outputServices <value> The relative ___location of the services output directory
4448
--useOptions Use options instead of arguments
4549
--useUnionTypes Use union types instead of enums
4650
--exportCore <value> Write core files to disk (default: true)

bin/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const params = program
1313
.requiredOption('-i, --input <value>', 'OpenAPI specification, can be a path, url or string content (required)')
1414
.requiredOption('-o, --output <value>', 'Output directory (required)')
1515
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node, axios]', 'fetch')
16+
.option('--outputCore <value>', 'The relative ___location of the core output directory')
17+
.option('--outputServices <value>', 'The relative ___location of the services output directory')
18+
.option('--outputModels <value>', 'The relative ___location of the models output directory')
19+
.option('--outputSchemas <value>', 'The relative ___location of the core output directory')
1620
.option('--useOptions', 'Use options instead of arguments')
1721
.option('--useUnionTypes', 'Use union types instead of enums')
1822
.option('--exportCore <value>', 'Write core files to disk', true)
@@ -29,6 +33,10 @@ if (OpenAPI) {
2933
OpenAPI.generate({
3034
input: params.input,
3135
output: params.output,
36+
outputCore: params.outputCore,
37+
outputServices: params.outputServices,
38+
outputModels: params.outputModels,
39+
outputSchemas: params.outputSchemas,
3240
httpClient: params.client,
3341
useOptions: params.useOptions,
3442
useUnionTypes: params.useUnionTypes,

src/index.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolve } from 'path';
12
import { HttpClient } from './HttpClient';
23
import { parse as parseV2 } from './openApi/v2';
34
import { parse as parseV3 } from './openApi/v3';
@@ -13,6 +14,10 @@ export { HttpClient } from './HttpClient';
1314
export type Options = {
1415
input: string | Record<string, any>;
1516
output: string;
17+
outputCore?: string;
18+
outputServices?: string;
19+
outputModels?: string;
20+
outputSchemas?: string;
1621
httpClient?: HttpClient;
1722
useOptions?: boolean;
1823
useUnionTypes?: boolean;
@@ -30,6 +35,10 @@ export type Options = {
3035
* service layer, etc.
3136
* @param input The relative ___location of the OpenAPI spec
3237
* @param output The relative ___location of the output directory
38+
* @param outputCore The relative ___location of the core output directory
39+
* @param outputModels The relative ___location of the models output directory
40+
* @param outputSchemas The relative ___location of the schemas output directory
41+
* @param outputServices The relative ___location of the services output directory
3342
* @param httpClient The selected httpClient (fetch or XHR)
3443
* @param useOptions Use options or arguments functions
3544
* @param useUnionTypes Use union types instead of enums
@@ -43,6 +52,10 @@ export type Options = {
4352
export async function generate({
4453
input,
4554
output,
55+
outputCore = resolve(output, 'core'),
56+
outputModels = resolve(output, 'models'),
57+
outputServices = resolve(output, 'services'),
58+
outputSchemas = resolve(output, 'schemas'),
4659
httpClient = HttpClient.FETCH,
4760
useOptions = false,
4861
useUnionTypes = false,
@@ -66,15 +79,47 @@ export async function generate({
6679
const client = parseV2(openApi);
6780
const clientFinal = postProcessClient(client);
6881
if (!write) break;
69-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
82+
await writeClient(
83+
clientFinal,
84+
templates,
85+
output,
86+
outputCore,
87+
outputModels,
88+
outputSchemas,
89+
outputServices,
90+
httpClient,
91+
useOptions,
92+
useUnionTypes,
93+
exportCore,
94+
exportServices,
95+
exportModels,
96+
exportSchemas,
97+
request
98+
);
7099
break;
71100
}
72101

73102
case OpenApiVersion.V3: {
74103
const client = parseV3(openApi);
75104
const clientFinal = postProcessClient(client);
76105
if (!write) break;
77-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
106+
await writeClient(
107+
clientFinal,
108+
templates,
109+
output,
110+
outputCore,
111+
outputModels,
112+
outputSchemas,
113+
outputServices,
114+
httpClient,
115+
useOptions,
116+
useUnionTypes,
117+
exportCore,
118+
exportServices,
119+
exportModels,
120+
exportSchemas,
121+
request
122+
);
78123
break;
79124
}
80125
}

src/templates/exportService.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{{#if imports}}
44
{{#each imports}}
5-
import type { {{{this}}} } from '../models/{{{this}}}';
5+
import type { {{{this}}} } from '{{{@root.modelsPath}}}/{{{this}}}';
66
{{/each}}
77
{{/if}}
88
import { BaseAPI, globalImportUrl, AxiosPromise } from '../core/request';

src/templates/index.hbs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{{>header}}
22
{{#if @root.exportCore}}
33

4-
export { ApiError } from './core/ApiError';
5-
export { OpenAPI } from './core/OpenAPI';
6-
export { BaseAPI } from './core/request';
4+
export { ApiError } from '{{{@root.corePath}}}/ApiError';
5+
export { OpenAPI } from '{{{@root.corePath}}}/OpenAPI';
6+
export { BaseAPI } from '{{{@root.corePath}}}/request';
77
{{/if}}
88
{{#if @root.exportModels}}
99
{{#if models}}
1010

1111
{{#each models}}
1212
{{#if @root.useUnionTypes}}
13-
export type { {{{name}}} } from './models/{{{name}}}';
13+
export type { {{{name}}} } from '{{{@root.modelsPath}}}/{{{name}}}';
1414
{{else if enum}}
15-
export { {{{name}}} } from './models/{{{name}}}';
15+
export { {{{name}}} } from '{{{@root.modelsPath}}}/{{{name}}}';
1616
{{else if enums}}
17-
export { {{{name}}} } from './models/{{{name}}}';
17+
export { {{{name}}} } from '{{{@root.modelsPath}}}/{{{name}}}';
1818
{{else}}
19-
export type { {{{name}}} } from './models/{{{name}}}';
19+
export type { {{{name}}} } from '{{{@root.modelsPath}}}/{{{name}}}';
2020
{{/if}}
2121
{{/each}}
2222
{{/if}}
@@ -25,15 +25,15 @@ export type { {{{name}}} } from './models/{{{name}}}';
2525
{{#if models}}
2626

2727
{{#each models}}
28-
export { ${{{name}}} } from './schemas/${{{name}}}';
28+
export { ${{{name}}} } from '{{{@root.schemasPath}}}/${{{name}}}';
2929
{{/each}}
3030
{{/if}}
3131
{{/if}}
3232
{{#if @root.exportServices}}
3333
{{#if services}}
3434

3535
{{#each services}}
36-
export * from './services/{{{name}}}';
36+
export * from '{{{@root.servicesPath}}}/{{{name}}}';
3737
{{/each}}
3838
{{/if}}
3939
{{/if}}

src/utils/writeClient.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import { writeClientServices } from './writeClientServices';
1616
* @param client Client object with all the models, services, etc.
1717
* @param templates Templates wrapper with all loaded Handlebars templates
1818
* @param output The relative ___location of the output directory
19+
* @param outputCore The relative ___location of the core output directory
20+
* @param outputModels The relative ___location of the models output directory
21+
* @param outputSchemas The relative ___location of the schemas output directory
22+
* @param outputServices The relative ___location of the services output directory
1923
* @param httpClient The selected httpClient (fetch, xhr or node)
2024
* @param useOptions Use options or arguments functions
2125
* @param useUnionTypes Use union types instead of enums
@@ -29,6 +33,10 @@ export async function writeClient(
2933
client: Client,
3034
templates: Templates,
3135
output: string,
36+
outputCore: string,
37+
outputModels: string,
38+
outputSchemas: string,
39+
outputServices: string,
3240
httpClient: HttpClient,
3341
useOptions: boolean,
3442
useUnionTypes: boolean,
@@ -39,10 +47,10 @@ export async function writeClient(
3947
request?: string
4048
): Promise<void> {
4149
const outputPath = resolve(process.cwd(), output);
42-
const outputPathCore = resolve(outputPath, 'core');
43-
const outputPathModels = resolve(outputPath, 'models');
44-
const outputPathSchemas = resolve(outputPath, 'schemas');
45-
const outputPathServices = resolve(outputPath, 'services');
50+
const outputPathCore = resolve(process.cwd(), outputCore);
51+
const outputPathModels = resolve(process.cwd(), outputModels);
52+
const outputPathSchemas = resolve(process.cwd(), outputSchemas);
53+
const outputPathServices = resolve(process.cwd(), outputServices);
4654

4755
if (!isSubDirectory(process.cwd(), output)) {
4856
throw new Error(`Output folder is not a subdirectory of the current working directory`);
@@ -57,7 +65,7 @@ export async function writeClient(
5765
if (exportServices) {
5866
await rmdir(outputPathServices);
5967
await mkdir(outputPathServices);
60-
await writeClientServices(client.services, templates, outputPathServices, httpClient, useUnionTypes, useOptions);
68+
await writeClientServices(client.services, templates, outputPathServices, outputPathModels, httpClient, useUnionTypes, useOptions);
6169
}
6270

6371
if (exportSchemas) {
@@ -74,6 +82,19 @@ export async function writeClient(
7482

7583
if (exportCore || exportServices || exportSchemas || exportModels) {
7684
await mkdir(outputPath);
77-
await writeClientIndex(client, templates, outputPath, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
85+
await writeClientIndex(
86+
client,
87+
templates,
88+
outputPath,
89+
outputPathCore,
90+
outputPathModels,
91+
outputPathSchemas,
92+
outputPathServices,
93+
useUnionTypes,
94+
exportCore,
95+
exportServices,
96+
exportModels,
97+
exportSchemas
98+
);
7899
}
79100
}

src/utils/writeClientCore.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ describe('writeClientCore', () => {
3838
expect(writeFile).toBeCalledWith('/ApiRequestOptions.ts', 'apiRequestOptions');
3939
expect(writeFile).toBeCalledWith('/ApiResult.ts', 'apiResult');
4040
expect(writeFile).toBeCalledWith('/request.ts', 'request');
41+
expect(writeFile).toBeCalledWith('/index.ts', 'index');
4142
});
4243
});

src/utils/writeClientCore.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ export async function writeClientCore(client: Client, templates: Templates, outp
2525
await writeFile(resolve(outputPath, 'ApiRequestOptions.ts'), templates.core.apiRequestOptions({}));
2626
await writeFile(resolve(outputPath, 'ApiResult.ts'), templates.core.apiResult({}));
2727
await writeFile(resolve(outputPath, 'request.ts'), templates.core.request(context));
28+
const indexFile = resolve(outputPath, 'index.ts');
29+
await writeFile(
30+
indexFile,
31+
templates.index({
32+
exportCore: true,
33+
exportServices: false,
34+
exportModels: false,
35+
exportSchemas: false,
36+
corePath: '.',
37+
})
38+
);
2839

2940
if (request) {
3041
const requestFile = resolve(process.cwd(), request);

src/utils/writeClientIndex.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('writeClientIndex', () => {
3030
},
3131
};
3232

33-
await writeClientIndex(client, templates, '/', true, true, true, true, true);
33+
await writeClientIndex(client, templates, '/', '/core', '/models', '/schemas', '/services', true, true, true, true, true);
3434

3535
expect(writeFile).toBeCalledWith('/index.ts', 'index');
3636
});

src/utils/writeClientIndex.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolve } from 'path';
1+
import { resolve, relative } from 'path';
22

33
import type { Client } from '../client/interfaces/Client';
44
import { writeFile } from './fileSystem';
@@ -23,12 +23,21 @@ export async function writeClientIndex(
2323
client: Client,
2424
templates: Templates,
2525
outputPath: string,
26+
corePath: string,
27+
modelsPath: string,
28+
schemasPath: string,
29+
servicesPath: string,
2630
useUnionTypes: boolean,
2731
exportCore: boolean,
2832
exportServices: boolean,
2933
exportModels: boolean,
3034
exportSchemas: boolean
3135
): Promise<void> {
36+
const modelsRelativePath = relative(outputPath, modelsPath);
37+
const schemasRelativePath = relative(outputPath, schemasPath);
38+
const servicesRelativePath = relative(outputPath, servicesPath);
39+
const coreRelativePath = relative(outputPath, corePath);
40+
3241
await writeFile(
3342
resolve(outputPath, 'index.ts'),
3443
templates.index({
@@ -41,6 +50,10 @@ export async function writeClientIndex(
4150
version: client.version,
4251
models: sortModelsByName(client.models),
4352
services: sortServicesByName(client.services),
53+
modelsPath: modelsRelativePath.startsWith('../') ? modelsRelativePath : `./${modelsRelativePath}`,
54+
schemasPath: schemasRelativePath.startsWith('../') ? schemasRelativePath : `./${schemasRelativePath}`,
55+
servicesPath: servicesRelativePath.startsWith('../') ? servicesRelativePath : `./${servicesRelativePath}`,
56+
corePath: coreRelativePath.startsWith('../') ? coreRelativePath : `./${coreRelativePath}`,
4457
})
4558
);
4659
}

0 commit comments

Comments
 (0)