Skip to content

Commit b3691fd

Browse files
committed
use specified paths
1 parent 05694ea commit b3691fd

File tree

7 files changed

+59
-20
lines changed

7 files changed

+59
-20
lines changed

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/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: 9 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,6 +23,10 @@ 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,
@@ -41,6 +45,10 @@ export async function writeClientIndex(
4145
version: client.version,
4246
models: sortModelsByName(client.models),
4347
services: sortServicesByName(client.services),
48+
modelsPath: relative(outputPath, modelsPath),
49+
schemasPath: relative(outputPath, schemasPath),
50+
servicesPath: relative(outputPath, servicesPath),
51+
corePath: relative(outputPath, corePath),
4452
})
4553
);
4654
}

src/utils/writeClientServices.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ describe('writeClientServices', () => {
3232
},
3333
};
3434

35-
await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false);
35+
await writeClientServices(services, templates, '/', '../models', HttpClient.FETCH, false, false);
3636

3737
expect(writeFile).toBeCalledWith('/MyService.ts', 'service');
38+
expect(writeFile).toBeCalledWith('/index.ts', 'index');
3839
});
3940
});

src/utils/writeClientServices.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,21 @@ const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION';
1717
* @param useUnionTypes Use union types instead of enums
1818
* @param useOptions Use options or arguments functions
1919
*/
20-
export async function writeClientServices(services: Service[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean, useOptions: boolean): Promise<void> {
20+
export async function writeClientServices(
21+
services: Service[],
22+
templates: Templates,
23+
outputPath: string,
24+
modelsOutputPath: string,
25+
httpClient: HttpClient,
26+
useUnionTypes: boolean,
27+
useOptions: boolean
28+
): Promise<void> {
2129
for (const service of services) {
2230
const file = resolve(outputPath, `${service.name}.ts`);
2331
const useVersion = service.operations.some(operation => operation.path.includes(VERSION_TEMPLATE_STRING));
2432
const templateResult = templates.exports.service({
2533
...service,
34+
modelsPath: relative(outputPath, modelsOutputPath),
2635
httpClient,
2736
useUnionTypes,
2837
useVersion,

0 commit comments

Comments
 (0)