Skip to content

Commit 15f7955

Browse files
committed
- Working indentation
1 parent 6f2a714 commit 15f7955

20 files changed

+110
-44
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ $ openapi --help
4747
--exportServices <value> Write services to disk (default: true)
4848
--exportModels <value> Write models to disk (default: true)
4949
--exportSchemas <value> Write schemas to disk (default: false)
50-
--postfix <value> Service name postfix (default: "Service")
50+
--indent <value> Service name postfix (default: "Service")
51+
--postfix <value> Indentation options [4, 2, tab] (default: "5")
5152
--request <value> Path to custom request file
5253
-h, --help display help for command
5354

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const params = program
1919
.option('--exportServices <value>', 'Write services to disk', true)
2020
.option('--exportModels <value>', 'Write models to disk', true)
2121
.option('--exportSchemas <value>', 'Write schemas to disk', false)
22+
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
2223
.option('--postfix <value>', 'Service name postfix', 'Service')
2324
.option('--request <value>', 'Path to custom request file')
2425
.parse(process.argv)
@@ -37,6 +38,7 @@ if (OpenAPI) {
3738
exportServices: JSON.parse(params.exportServices) === true,
3839
exportModels: JSON.parse(params.exportModels) === true,
3940
exportSchemas: JSON.parse(params.exportSchemas) === true,
41+
indent: params.indent,
4042
postfix: params.postfix,
4143
request: params.request,
4244
})

src/Indent.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum Indent {
2+
SPACE_4 = '4',
3+
SPACE_2 = '2',
4+
TAB = 'tab',
5+
}

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { HttpClient } from './HttpClient';
2+
import { Indent } from './Indent';
23
import { parse as parseV2 } from './openApi/v2';
34
import { parse as parseV3 } from './openApi/v3';
45
import { getOpenApiSpec } from './utils/getOpenApiSpec';
@@ -9,6 +10,7 @@ import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
910
import { writeClient } from './utils/writeClient';
1011

1112
export { HttpClient } from './HttpClient';
13+
export { Indent } from './Indent';
1214

1315
export type Options = {
1416
input: string | Record<string, any>;
@@ -20,6 +22,7 @@ export type Options = {
2022
exportServices?: boolean;
2123
exportModels?: boolean;
2224
exportSchemas?: boolean;
25+
indent?: Indent;
2326
postfix?: string;
2427
request?: string;
2528
write?: boolean;
@@ -38,6 +41,7 @@ export type Options = {
3841
* @param exportServices: Generate services
3942
* @param exportModels: Generate models
4043
* @param exportSchemas: Generate schemas
44+
* @param indent: Indentation options (4, 2 or tab)
4145
* @param postfix: Service name postfix
4246
* @param request: Path to custom request file
4347
* @param write Write the files to disk (true or false)
@@ -52,6 +56,7 @@ export async function generate({
5256
exportServices = true,
5357
exportModels = true,
5458
exportSchemas = false,
59+
indent = Indent.SPACE_4,
5560
postfix = 'Service',
5661
request,
5762
write = true,
@@ -80,6 +85,7 @@ export async function generate({
8085
exportServices,
8186
exportModels,
8287
exportSchemas,
88+
indent,
8389
postfix,
8490
request
8591
);
@@ -101,6 +107,7 @@ export async function generate({
101107
exportServices,
102108
exportModels,
103109
exportSchemas,
110+
indent,
104111
postfix,
105112
request
106113
);

src/utils/format.spec.ts renamed to src/utils/formatCode.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { format } from './format';
1+
import { formatCode } from './formatCode';
22

33
const input1 = `{ foo: true }`;
44

@@ -30,11 +30,11 @@ const output4 = `{
3030

3131
describe('format', () => {
3232
it('should produce correct result', () => {
33-
expect(format(``)).toEqual('');
34-
expect(format(`{}`)).toEqual('{}');
35-
expect(format(input1)).toEqual(output1);
36-
expect(format(input2)).toEqual(output2);
37-
expect(format(input3)).toEqual(output3);
38-
expect(format(input4)).toEqual(output4);
33+
expect(formatCode(``)).toEqual('');
34+
expect(formatCode(`{}`)).toEqual('{}');
35+
expect(formatCode(input1)).toEqual(output1);
36+
expect(formatCode(input2)).toEqual(output2);
37+
expect(formatCode(input3)).toEqual(output3);
38+
expect(formatCode(input4)).toEqual(output4);
3939
});
4040
});

src/utils/format.ts renamed to src/utils/formatCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EOL } from 'os';
22

3-
export function format(s: string): string {
3+
export function formatCode(s: string): string {
44
let indent: number = 0;
55
let lines = s.split(EOL);
66
lines = lines.map(line => {

src/utils/formatIndentation.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { EOL } from 'os';
2+
3+
import { Indent } from '../Indent';
4+
5+
export function formatIndentation(s: string, indent: Indent): string {
6+
let lines = s.split(EOL);
7+
lines = lines.map(line => {
8+
switch (indent) {
9+
case Indent.SPACE_4:
10+
return line.replace(/\t/g, ' ');
11+
case Indent.SPACE_2:
12+
return line.replace(/\t/g, ' ');
13+
case Indent.TAB:
14+
return line; // Default output is tab formatted
15+
}
16+
});
17+
return lines.join(EOL);
18+
}

src/utils/indent.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/utils/writeClient.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Client } from '../client/interfaces/Client';
22
import { HttpClient } from '../HttpClient';
3+
import { Indent } from '../Indent';
34
import { mkdir, rmdir, writeFile } from './fileSystem';
45
import { Templates } from './registerHandlebarTemplates';
56
import { writeClient } from './writeClient';
@@ -32,7 +33,20 @@ describe('writeClient', () => {
3233
},
3334
};
3435

35-
await writeClient(client, templates, './dist', HttpClient.FETCH, false, false, true, true, true, true, '');
36+
await writeClient(
37+
client,
38+
templates,
39+
'./dist',
40+
HttpClient.FETCH,
41+
false,
42+
false,
43+
true,
44+
true,
45+
true,
46+
true,
47+
Indent.SPACE_4,
48+
''
49+
);
3650

3751
expect(rmdir).toBeCalled();
3852
expect(mkdir).toBeCalled();

src/utils/writeClient.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from 'path';
22

33
import type { Client } from '../client/interfaces/Client';
44
import { HttpClient } from '../HttpClient';
5+
import { Indent } from '../Indent';
56
import { mkdir, rmdir } from './fileSystem';
67
import { isSubDirectory } from './isSubdirectory';
78
import { Templates } from './registerHandlebarTemplates';
@@ -24,6 +25,7 @@ import { writeClientServices } from './writeClientServices';
2425
* @param exportModels: Generate models
2526
* @param exportSchemas: Generate schemas
2627
* @param exportSchemas: Generate schemas
28+
* @param indent: Indentation options (4, 2 or tab)
2729
* @param postfix: Service name postfix
2830
* @param request: Path to custom request file
2931
*/
@@ -38,6 +40,7 @@ export async function writeClient(
3840
exportServices: boolean,
3941
exportModels: boolean,
4042
exportSchemas: boolean,
43+
indent: Indent,
4144
postfix: string,
4245
request?: string
4346
): Promise<void> {
@@ -54,7 +57,7 @@ export async function writeClient(
5457
if (exportCore) {
5558
await rmdir(outputPathCore);
5659
await mkdir(outputPathCore);
57-
await writeClientCore(client, templates, outputPathCore, httpClient, request);
60+
await writeClientCore(client, templates, outputPathCore, httpClient, indent, request);
5861
}
5962

6063
if (exportServices) {
@@ -67,20 +70,21 @@ export async function writeClient(
6770
httpClient,
6871
useUnionTypes,
6972
useOptions,
73+
indent,
7074
postfix
7175
);
7276
}
7377

7478
if (exportSchemas) {
7579
await rmdir(outputPathSchemas);
7680
await mkdir(outputPathSchemas);
77-
await writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes);
81+
await writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes, indent);
7882
}
7983

8084
if (exportModels) {
8185
await rmdir(outputPathModels);
8286
await mkdir(outputPathModels);
83-
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes);
87+
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes, indent);
8488
}
8589

8690
if (exportCore || exportServices || exportSchemas || exportModels) {

0 commit comments

Comments
 (0)