Skip to content

Commit c8e7915

Browse files
authored
Merge pull request ferdikoomen#10 from nicolas-chaulet/fix/autoformat-option
fix(config): support autoformat option flag
2 parents 74adb1a + 059558e commit c8e7915

File tree

8 files changed

+37
-0
lines changed

8 files changed

+37
-0
lines changed

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const params = program
1616
.option('--name <value>', 'Custom client class name')
1717
.option('--useOptions', 'Use options instead of arguments')
1818
.option('--useUnionTypes', 'Use union types instead of enums')
19+
.option('--autoformat <value>', 'Process generated files with autoformatter', false)
1920
.option('--exportCore <value>', 'Write core files to disk', true)
2021
.option('--exportServices <value>', 'Write services to disk', true)
2122
.option('--exportModels <value>', 'Write models to disk', true)
@@ -45,6 +46,7 @@ if (OpenAPI) {
4546
clientName: params.name,
4647
useOptions: params.useOptions,
4748
useUnionTypes: params.useUnionTypes,
49+
autoformat: JSON.parse(params.autoformat) === true,
4850
exportCore: JSON.parse(params.exportCore) === true,
4951
exportServices: parseBooleanOrString(params.exportServices),
5052
exportModels: parseBooleanOrString(params.exportModels),

bin/index.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ describe('bin', () => {
5959
expect(result.stderr.toString()).toBe('');
6060
});
6161

62+
it('should autoformat with Prettier', async () => {
63+
const result = crossSpawn.sync('node', [
64+
'./bin/index.js',
65+
'--input',
66+
'./test/spec/v3.json',
67+
'--output',
68+
'./test/generated/bin',
69+
'--autoformat',
70+
'true',
71+
]);
72+
expect(result.stdout.toString()).toBe('');
73+
expect(result.stderr.toString()).toBe('');
74+
});
75+
6276
it('it should throw error without params', async () => {
6377
const result = crossSpawn.sync('node', ['./bin/index.js']);
6478
expect(result.stdout.toString()).toBe('');

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type Options = {
1919
clientName?: string;
2020
useOptions?: boolean;
2121
useUnionTypes?: boolean;
22+
autoformat?: boolean;
2223
exportCore?: boolean;
2324
exportServices?: boolean | string;
2425
exportModels?: boolean | string;
@@ -57,6 +58,7 @@ export const generate = async ({
5758
clientName,
5859
useOptions = false,
5960
useUnionTypes = false,
61+
autoformat = false,
6062
exportCore = true,
6163
exportServices = true,
6264
exportModels = true,
@@ -100,6 +102,7 @@ export const generate = async ({
100102
httpClient,
101103
useOptions,
102104
useUnionTypes,
105+
autoformat,
103106
exportCore,
104107
exportServices,
105108
exportModels,

src/utils/writeClient.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe('writeClient', () => {
4343
HttpClient.FETCH,
4444
false,
4545
false,
46+
false,
4647
true,
4748
true,
4849
true,

src/utils/writeClient.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { spawnSync } from 'child_process';
2+
import { createRequire } from 'module';
13
import { resolve } from 'path';
24

35
import type { Client } from '../client/interfaces/Client';
@@ -40,6 +42,7 @@ export const writeClient = async (
4042
httpClient: HttpClient,
4143
useOptions: boolean,
4244
useUnionTypes: boolean,
45+
autoformat: boolean,
4346
exportCore: boolean,
4447
exportServices: boolean | string,
4548
exportModels: boolean | string,
@@ -125,4 +128,14 @@ export const writeClient = async (
125128
clientName
126129
);
127130
}
131+
132+
if (autoformat) {
133+
const pathPackageJson = resolve(process.cwd(), 'package.json');
134+
const require = createRequire('/');
135+
const json = require(pathPackageJson);
136+
const usesPrettier = [json.dependencies, json.devDependencies].some(deps => Boolean(deps.prettier));
137+
if (usesPrettier) {
138+
spawnSync('prettier', ['--ignore-unknown', '--write', output]);
139+
}
140+
}
128141
};

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const generate = async (input, output) => {
1010
httpClient: OpenAPI.HttpClient.FETCH,
1111
useOptions: true,
1212
useUnionTypes: false,
13+
autoformat: false,
1314
exportCore: true,
1415
exportSchemas: true,
1516
exportModels: true,

test/index.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('v2', () => {
1111
httpClient: HttpClient.FETCH,
1212
useOptions: false,
1313
useUnionTypes: false,
14+
autoformat: false,
1415
exportCore: true,
1516
exportSchemas: true,
1617
exportModels: true,
@@ -32,6 +33,7 @@ describe('v3', () => {
3233
httpClient: HttpClient.FETCH,
3334
useOptions: false,
3435
useUnionTypes: false,
36+
autoformat: false,
3537
exportCore: true,
3638
exportSchemas: true,
3739
exportModels: true,

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type Options = {
1919
clientName?: string;
2020
useOptions?: boolean;
2121
useUnionTypes?: boolean;
22+
autoformat?: boolean;
2223
exportCore?: boolean;
2324
exportServices?: boolean | string;
2425
exportModels?: boolean | string;

0 commit comments

Comments
 (0)