Skip to content

Commit 720dd9a

Browse files
authored
Merge pull request ferdikoomen#96 from nicolas-chaulet/fix/lint-flag
fix(config): set lint to false by default
2 parents 69506ac + 11a8f19 commit 720dd9a

File tree

13 files changed

+81
-44
lines changed

13 files changed

+81
-44
lines changed

README.md

+24-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Installation](#installation)
1111
- [Configuration](#configuration)
1212
- [Formatting](#formatting)
13+
- [Linting](#linting)
1314
- [Enums](#enums)
1415
- [Config API](#config-api)
1516
- [Contributing](#contributing)
@@ -97,19 +98,35 @@ export default defineConfig({
9798

9899
### Formatting
99100

100-
By default, `openapi-ts` will automatically format your client according to your project configuration. To disable automatic formatting, set `autoformat` to false
101+
By default, `openapi-ts` will automatically format your client according to your project configuration. To disable automatic formatting, set `format` to false
101102

102103
```ts
103104
import { defineConfig } from '@nicolas-chaulet/openapi-typescript-codegen';
104105

105106
export default defineConfig({
106-
autoformat: false,
107+
format: false,
107108
input: 'path/to/openapi.json',
108109
output: 'src/client',
109110
})
110111
```
111112

112-
You can also prevent your client from being processed by formatters and linters by adding your output path to the tool's ignore file (e.g. `.eslintignore`, `.prettierignore`).
113+
You can also prevent your client from being processed by formatters by adding your output path to the tool's ignore file (e.g. `.prettierignore`).
114+
115+
### Linting
116+
117+
For performance reasons, `openapi-ts` does not automatically lint your client. To enable this feature, set `lint` to true
118+
119+
```ts
120+
import { defineConfig } from '@nicolas-chaulet/openapi-typescript-codegen';
121+
122+
export default defineConfig({
123+
input: 'path/to/openapi.json',
124+
lint: true,
125+
output: 'src/client',
126+
})
127+
```
128+
129+
You can also prevent your client from being processed by linters by adding your output path to the tool's ignore file (e.g. `.eslintignore`).
113130

114131
### Enums
115132

@@ -148,13 +165,16 @@ $ openapi-ts --help
148165
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
149166
--name <value> Custom client class name
150167
--useOptions <value> Use options instead of arguments (default: false)
151-
--no-autoformat Disable processing generated files with formatter
152168
--base <value> Manually set base in OpenAPI config instead of inferring from server value
153169
--enums Generate JavaScript objects from enum definitions (default: false)
154170
--exportCore <value> Write core files to disk (default: true)
155171
--exportServices <value> Write services to disk [true, false, regexp] (default: true)
156172
--exportModels <value> Write models to disk [true, false, regexp] (default: true)
157173
--exportSchemas <value> Write schemas to disk (default: false)
174+
--format Process output folder with formatter?
175+
--no-format Disable processing output folder with formatter
176+
--lint Process output folder with linter?
177+
--no-lint Disable processing output folder with linter
158178
--no-operationId Use path URL to generate operation ID
159179
--postfixServices Service name postfix (default: "Service")
160180
--postfixModels Model name postfix

bin/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ const params = program
1717
.option('-c, --client <value>', 'HTTP client to generate [angular, axios, fetch, node, xhr]')
1818
.option('--name <value>', 'Custom client class name')
1919
.option('--useOptions [value]', 'Use options instead of arguments')
20-
.option('--autoformat', 'Process generated files with formatter?')
21-
.option('--no-autoformat', 'Disable processing generated files with formatter')
2220
.option('--base [value]', 'Manually set base in OpenAPI config instead of inferring from server value')
2321
.option('--enums', 'Generate JavaScript objects from enum definitions')
2422
.option('--exportCore <value>', 'Write core files to disk')
2523
.option('--exportServices <value>', 'Write services to disk')
2624
.option('--exportModels <value>', 'Write models to disk')
2725
.option('--exportSchemas <value>', 'Write schemas to disk')
26+
.option('--format', 'Process output folder with formatter?')
27+
.option('--no-format', 'Disable processing output folder with formatter')
28+
.option('--lint', 'Process output folder with linter?')
29+
.option('--no-lint', 'Disable processing output folder with linter')
2830
.option('--operationId', 'Use operationd ID?')
2931
.option('--no-operationId', 'Use path URL to generate operation ID')
3032
.option('--postfixServices <value>', 'Service name postfix')
@@ -39,7 +41,7 @@ const params = program
3941

4042
async function start() {
4143
try {
42-
const { createClient } = await import(new URL('../dist/index.js', import.meta.url));
44+
const { createClient } = await import(new URL('../dist/node/index.js', import.meta.url));
4345
await createClient(params);
4446
process.exit(0);
4547
} catch (error) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"email": "[email protected]"
3333
}
3434
],
35-
"main": "./dist/index.js",
35+
"main": "./dist/node/index.js",
3636
"types": "./dist/node/index.d.ts",
3737
"bin": {
3838
"openapi-ts": "bin/index.js"

rollup.config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)).
7272
function createConfig(isProduction: boolean) {
7373
return defineConfig({
7474
external: [...Object.keys(pkg.dependencies)],
75-
input: path.resolve(__dirname, 'src/index.ts'),
75+
input: path.resolve(__dirname, 'src/node/index.ts'),
7676
output: {
77-
file: path.resolve(__dirname, 'dist/index.js'),
77+
file: path.resolve(__dirname, 'dist/node/index.js'),
7878
format: 'esm',
7979
},
8080
plugins: [

src/index.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ describe('parseOpenApiSpecification', () => {
4242
});
4343

4444
const options: Parameters<typeof parseOpenApiSpecification>[1] = {
45-
autoformat: true,
4645
client: 'fetch',
4746
enums: true,
4847
exportCore: true,
4948
exportModels: true,
5049
exportSchemas: true,
5150
exportServices: true,
51+
format: true,
5252
input: '',
53+
lint: false,
5354
operationId: true,
5455
output: '',
5556
postfixModels: '',

src/index.ts

+21-19
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@ type Dependencies = Record<string, unknown>;
1919
// add support for `openapi-ts.config.ts`
2020
const configFiles = ['openapi-ts.config.js'];
2121

22-
export const parseOpenApiSpecification = (openApi: Awaited<ReturnType<typeof getOpenApiSpec>>, options: Config) => {
23-
if ('swagger' in openApi) {
24-
return parseV2(openApi, options);
25-
}
22+
export const parseOpenApiSpecification = (openApi: Awaited<ReturnType<typeof getOpenApiSpec>>, config: Config) => {
2623
if ('openapi' in openApi) {
27-
return parseV3(openApi, options);
24+
return parseV3(openApi, config);
25+
}
26+
if ('swagger' in openApi) {
27+
return parseV2(openApi, config);
2828
}
2929
throw new Error(`Unsupported Open API specification: ${JSON.stringify(openApi, null, 2)}`);
3030
};
3131

32-
const formatClient = (options: Config, dependencies: Dependencies) => {
33-
if (!options.autoformat) {
34-
return;
35-
}
36-
37-
if (dependencies.prettier) {
38-
console.log('✨ Running Prettier');
39-
sync('prettier', ['--ignore-unknown', options.output, '--write', '--ignore-path', './.prettierignore']);
32+
const processOutput = (config: Config, dependencies: Dependencies) => {
33+
if (config.format) {
34+
if (dependencies.prettier) {
35+
console.log('✨ Running Prettier');
36+
sync('prettier', ['--ignore-unknown', config.output, '--write', '--ignore-path', './.prettierignore']);
37+
}
4038
}
4139

42-
if (dependencies.eslint) {
43-
console.log('✨ Running Eslint');
44-
sync('eslint', [options.output, '--fix', '--quiet', '--ignore-path', './.eslintignore']);
40+
if (config.lint) {
41+
if (dependencies.eslint) {
42+
console.log('✨ Running ESLint');
43+
sync('eslint', [config.output, '--fix', '--quiet', '--ignore-path', './.eslintignore']);
44+
}
4545
}
4646
};
4747

@@ -89,14 +89,15 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
8989
}
9090

9191
const {
92-
autoformat = true,
9392
base,
9493
enums = false,
9594
exportCore = true,
9695
exportModels = true,
9796
exportSchemas = false,
9897
exportServices = true,
98+
format = true,
9999
input,
100+
lint = false,
100101
name,
101102
operationId = true,
102103
output,
@@ -112,15 +113,16 @@ const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
112113
const client = userConfig.client || inferClient(dependencies);
113114

114115
const config: Config = {
115-
autoformat,
116116
base,
117117
client,
118118
enums,
119119
exportCore,
120120
exportModels,
121121
exportSchemas,
122122
exportServices,
123+
format,
123124
input,
125+
lint,
124126
name,
125127
operationId,
126128
output,
@@ -174,7 +176,7 @@ export async function createClient(userConfig: UserConfig): Promise<Client> {
174176
if (config.write) {
175177
logClientMessage(config.client);
176178
await writeClient(client, templates, config);
177-
formatClient(config, dependencies);
179+
processOutput(config, dependencies);
178180
}
179181

180182
console.log('✨ Done! Your client is located in:', config.output);

src/openApi/v2/parser/getServices.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { getServices } from './getServices';
33
describe('getServices', () => {
44
it('should create a unnamed service if tags are empty', () => {
55
const options: Parameters<typeof getServices>[1] = {
6-
autoformat: false,
76
client: 'fetch',
87
enums: false,
98
exportCore: true,
109
exportModels: true,
1110
exportSchemas: true,
1211
exportServices: true,
12+
format: false,
1313
input: '',
14+
lint: false,
1415
operationId: false,
1516
output: '',
1617
postfixModels: '',

src/openApi/v3/parser/__tests__/getServices.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { getServices } from '../getServices';
33
describe('getServices', () => {
44
it('should create a unnamed service if tags are empty', () => {
55
const options: Parameters<typeof getServices>[1] = {
6-
autoformat: false,
76
client: 'fetch',
87
enums: false,
98
exportCore: true,
109
exportModels: true,
1110
exportSchemas: true,
1211
exportServices: true,
12+
format: false,
1313
input: '',
14+
lint: false,
1415
operationId: true,
1516
output: '',
1617
postfixModels: '',

src/types/config.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
export interface UserConfig {
2-
/**
3-
* Process generated files with autoformatter
4-
* @default true
5-
*/
6-
autoformat?: boolean;
72
/**
83
* Manually set base in OpenAPI config instead of inferring from server value
94
*/
@@ -38,10 +33,20 @@ export interface UserConfig {
3833
* @default true
3934
*/
4035
exportServices?: boolean | string;
36+
/**
37+
* Process output folder with formatter?
38+
* @default true
39+
*/
40+
format?: boolean;
4141
/**
4242
* The relative location of the OpenAPI spec
4343
*/
4444
input: string | Record<string, unknown>;
45+
/**
46+
* Process output folder with linter?
47+
* @default false
48+
*/
49+
lint?: boolean;
4550
/**
4651
* Custom client class name
4752
*/

src/utils/__tests__/handlebars.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ describe('registerHandlebarHelpers', () => {
66
it('should register the helpers', () => {
77
registerHandlebarHelpers(
88
{
9-
autoformat: true,
109
client: 'fetch',
1110
enums: true,
1211
exportCore: true,
1312
exportModels: true,
1413
exportSchemas: true,
1514
exportServices: true,
15+
format: true,
1616
input: '',
17+
lint: false,
1718
operationId: true,
1819
output: '',
1920
postfixModels: '',
@@ -58,14 +59,15 @@ describe('registerHandlebarTemplates', () => {
5859
it('should return correct templates', () => {
5960
const templates = registerHandlebarTemplates(
6061
{
61-
autoformat: true,
6262
client: 'fetch',
6363
enums: true,
6464
exportCore: true,
6565
exportModels: true,
6666
exportSchemas: true,
6767
exportServices: true,
68+
format: true,
6869
input: '',
70+
lint: false,
6971
operationId: true,
7072
output: '',
7173
postfixModels: '',

src/utils/write/__tests__/client.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ describe('writeClient', () => {
3535
};
3636

3737
await writeClient(client, templates, {
38-
autoformat: true,
3938
client: 'fetch',
4039
enums: true,
4140
exportCore: true,
4241
exportModels: true,
4342
exportSchemas: true,
4443
exportServices: true,
44+
format: true,
4545
input: '',
46+
lint: false,
4647
operationId: true,
4748
output: './dist',
4849
postfixModels: 'AppClient',

src/utils/write/__tests__/services.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ describe('writeClientServices', () => {
4343
};
4444

4545
await writeClientServices(client, templates, '/', {
46-
autoformat: false,
4746
client: 'fetch',
4847
enums: false,
4948
exportCore: true,
5049
exportModels: true,
5150
exportSchemas: true,
5251
exportServices: true,
52+
format: false,
5353
input: '',
54+
lint: false,
5455
operationId: true,
5556
output: '',
5657
postfixModels: '',

test/bin.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('bin', () => {
134134
expect(result.stderr.toString()).toBe('');
135135
});
136136

137-
it('autoformats output with Prettier', async () => {
137+
it('formats output with Prettier', async () => {
138138
const result = sync('node', [
139139
'./bin/index.js',
140140
'--input',
@@ -146,15 +146,16 @@ describe('bin', () => {
146146
expect(result.stderr.toString()).toBe('');
147147
});
148148

149-
it('auto fixs output with Eslint', async () => {
149+
it('lints output with ESLint', async () => {
150150
const result = sync('node', [
151151
'./bin/index.js',
152152
'--input',
153153
'./test/spec/v3.json',
154154
'--output',
155155
'./test/generated/bin',
156+
'--lint',
156157
]);
157-
expect(result.stdout.toString()).toContain('Eslint');
158+
expect(result.stdout.toString()).toContain('ESLint');
158159
expect(result.stderr.toString()).toBe('');
159160
});
160161

0 commit comments

Comments
 (0)