Skip to content

Commit ec9cfac

Browse files
committed
feat(lint): add husky, eslint, prettier
1 parent 621265f commit ec9cfac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1684
-832
lines changed

.eslintrc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
extends: ['prettier/@typescript-eslint', 'plugin:prettier/recommended'],
3+
parser: '@typescript-eslint/parser',
4+
parserOptions: {
5+
ecmaVersion: 6,
6+
sourceType: 'module',
7+
ecmaFeatures: {
8+
modules: true,
9+
},
10+
},
11+
};

.prettierrc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"arrowParens": "always",
3+
"bracketSpacing": true,
4+
"printWidth": 80,
5+
"tabWidth": 2,
6+
"semi": true,
7+
"singleQuote": true,
8+
"trailingComma": "all"
9+
}

commitlint.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional'],
3+
};

package.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,22 @@
2828
"rollup": "rollup -c rollup.config.js",
2929
"prebuild": "rimraf ./lib && mkdir lib",
3030
"build": "tsc && yarn rollup",
31-
"lint": "tslint src/**/*.ts --project .",
31+
"lint": "eslint src/**/*.ts",
32+
"format": "prettier --write 'src/**/*.{js,ts}'",
3233
"examples": "run-p example:*",
3334
"example:github": "node lib/bin/orval.js import --input https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v3.0/petstore.yaml --output examples/petstoreFromGithubSpec.ts",
3435
"example:file": "node lib/bin/orval.js import --input examples/petstore.yaml --output examples/petstoreFromFileSpec.ts",
3536
"example:advanced": "node lib/bin/orval.js import --config examples/orval.config.js"
3637
},
38+
"husky": {
39+
"hooks": {
40+
"pre-commit": "yarn lint",
41+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
42+
}
43+
},
3744
"devDependencies": {
45+
"@commitlint/cli": "^8.3.5",
46+
"@commitlint/config-conventional": "^8.3.4",
3847
"@types/chalk": "^2.2.0",
3948
"@types/commander": "^2.12.2",
4049
"@types/faker": "^4.1.8",
@@ -43,12 +52,17 @@
4352
"@types/node": "^13.7.7",
4453
"@types/request": "^2.48.4",
4554
"@types/yamljs": "^0.2.30",
55+
"@typescript-eslint/eslint-plugin": "^2.25.0",
56+
"@typescript-eslint/parser": "^2.25.0",
4657
"axios": "^0.19.2",
58+
"eslint": "^6.8.0",
59+
"eslint-config-prettier": "^6.10.1",
60+
"eslint-plugin-prettier": "^3.1.2",
4761
"faker": "^4.1.0",
4862
"husky": "^4.2.3",
4963
"lint-staged": "^10.0.8",
5064
"npm-run-all": "^4.1.5",
51-
"prettier": "^1.19.1",
65+
"prettier": "^2.0.2",
5266
"rimraf": "^3.0.2",
5367
"rollup": "^1.32.0",
5468
"rollup-plugin-typescript2": "^0.26.0",
@@ -63,7 +77,6 @@
6377
"inquirer": "^7.0.6",
6478
"lodash": "^4.17.15",
6579
"openapi3-ts": "^1.3.0",
66-
"request": "^2.88.2",
6780
"swagger2openapi": "^5.3.3",
6881
"yamljs": "^0.3.0"
6982
},

src/bin/orval-import.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import program from 'commander';
22
import difference from 'lodash/difference';
3-
import {join} from 'path';
4-
import {importSpecs} from '../core/importers/specs';
5-
import {writeSpecs} from '../core/writers/specs';
6-
import {ExternalConfigFile, Options} from '../types';
7-
import {errorMessage, mismatchArgsMessage} from '../utils/messages/logs';
3+
import { join } from 'path';
4+
import { importSpecs } from '../core/importers/specs';
5+
import { writeSpecs } from '../core/writers/specs';
6+
import { ExternalConfigFile, Options } from '../types';
7+
import { errorMessage, mismatchArgsMessage } from '../utils/messages/logs';
88

99
program.option('-o, --output [value]', 'output file destination');
1010
program.option(
1111
'-f, --input [value]',
12-
'input file (yaml or json openapi specs)'
12+
'input file (yaml or json openapi specs)',
1313
);
1414
program.option('--config [value]', 'override flags by a config file');
1515
program.parse(process.argv);
@@ -25,7 +25,7 @@ if (program.config) {
2525
// tslint:disable-next-line: no-var-requires
2626
const config: ExternalConfigFile = require(join(
2727
process.cwd(),
28-
program.config
28+
program.config,
2929
));
3030

3131
const mismatchArgs = difference(program.args, Object.keys(config));
@@ -35,12 +35,10 @@ if (program.config) {
3535

3636
Object.entries(config)
3737
.filter(([backend]) =>
38-
program.args.length === 0 ? true : program.args.includes(backend)
38+
program.args.length === 0 ? true : program.args.includes(backend),
3939
)
4040
.forEach(([backend, options]) => {
41-
importSpecs(options)
42-
.then(writeSpecs(options, backend))
43-
.catch(catchError);
41+
importSpecs(options).then(writeSpecs(options, backend)).catch(catchError);
4442
});
4543
} else {
4644
// Use flags as configuration

src/bin/orval.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import program from 'commander';
2-
import {startMessage} from '../utils/messages/logs';
3-
import {getPackage} from '../utils/packages';
2+
import { startMessage } from '../utils/messages/logs';
3+
import { getPackage } from '../utils/packages';
44

5-
const {name, version, description} = getPackage();
5+
const { name, version, description } = getPackage();
66

7-
startMessage({name, version, description});
7+
startMessage({ name, version, description });
88

99
program
1010
.version(version)
1111
.command(
1212
'import [open-api-file]',
13-
'generate orval type-safe from OpenAPI specs'
13+
'generate orval type-safe from OpenAPI specs',
1414
)
1515
.parse(process.argv);

src/constants/index.ts

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

33
export const generalJSTypes = [
44
'number',
@@ -7,12 +7,12 @@ export const generalJSTypes = [
77
'unknown',
88
'undefined',
99
'object',
10-
'blob'
10+
'blob',
1111
];
1212

1313
export const generalJSTypesWithArray = generalJSTypes.reduce<string[]>(
1414
(acc, type) => [...acc, type, `Array<${type}>`, `${type}[]`],
15-
[]
15+
[],
1616
);
1717

1818
export const VERBS_WITH_BODY = [Verbs.POST, Verbs.PUT, Verbs.PATCH];

src/core/generators/api.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {OpenAPIObject, PathItemObject} from 'openapi3-ts';
2-
import {OverrideOutput} from '../../types';
3-
import {GeneratorApiResponse, GeneratorSchema} from '../../types/generator';
4-
import {generateClient} from './client';
5-
import {generateVerbsOptions} from './verbsOptions';
1+
import { OpenAPIObject, PathItemObject } from 'openapi3-ts';
2+
import { OverrideOutput } from '../../types';
3+
import { GeneratorApiResponse, GeneratorSchema } from '../../types/generator';
4+
import { generateClient } from './client';
5+
import { generateVerbsOptions } from './verbsOptions';
66

77
export const generateApi = (
88
specs: OpenAPIObject,
9-
override?: OverrideOutput
9+
override?: OverrideOutput,
1010
) => {
1111
return Object.entries(specs.paths).reduce<GeneratorApiResponse>(
1212
(acc, [pathRoute, verbs]: [string, PathItemObject]) => {
@@ -16,28 +16,28 @@ export const generateApi = (
1616
verbs,
1717
override,
1818
route,
19-
components: specs.components
19+
components: specs.components,
2020
});
2121

2222
const schemas = verbsOptions.reduce<GeneratorSchema[]>(
23-
(acc, {queryParams}) => (queryParams ? [...acc, queryParams] : acc),
24-
[]
23+
(acc, { queryParams }) => (queryParams ? [...acc, queryParams] : acc),
24+
[],
2525
);
2626

2727
const client = generateClient(verbsOptions, {
2828
route,
2929
specs,
30-
override
30+
override,
3131
});
3232

3333
return {
3434
schemas: [...acc.schemas, ...schemas],
35-
operations: {...acc.operations, ...client}
35+
operations: { ...acc.operations, ...client },
3636
};
3737
},
3838
{
3939
operations: {},
40-
schemas: []
41-
}
40+
schemas: [],
41+
},
4242
);
4343
};

src/core/generators/axios.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {camel} from 'case';
2-
import {VERBS_WITH_BODY} from '../../constants';
3-
import {Verbs} from '../../types';
1+
import { camel } from 'case';
2+
import { VERBS_WITH_BODY } from '../../constants';
3+
import { Verbs } from '../../types';
44
import {
55
GeneratorOptions,
66
GeneratorSchema,
7-
GeneratorVerbOptions
7+
GeneratorVerbOptions,
88
} from '../../types/generator';
9-
import {GetterBody, GetterResponse} from '../../types/getters';
9+
import { GetterBody, GetterResponse } from '../../types/getters';
1010

1111
const generateBodyProps = (body: GetterBody, verb: Verbs) => {
1212
if (!VERBS_WITH_BODY.includes(verb)) {
@@ -22,7 +22,7 @@ const generateBodyProps = (body: GetterBody, verb: Verbs) => {
2222

2323
const generateQueryParamsProps = (
2424
response: GetterResponse,
25-
queryParams?: GeneratorSchema
25+
queryParams?: GeneratorSchema,
2626
) => {
2727
if (!queryParams && !response.isBlob) {
2828
return '';
@@ -52,7 +52,7 @@ const generateAxiosProps = ({
5252
body,
5353
queryParams,
5454
response,
55-
verb
55+
verb,
5656
}: {
5757
route: string;
5858
body: GetterBody;
@@ -62,15 +62,15 @@ const generateAxiosProps = ({
6262
}) => {
6363
return `\`${route}\` ${generateBodyProps(
6464
body,
65-
verb
65+
verb,
6666
)} ${generateQueryParamsProps(response, queryParams)}`;
6767
};
6868

6969
const generateAxiosDefinition = ({
7070
props,
7171
definitionName,
7272
response,
73-
summary
73+
summary,
7474
}: GeneratorVerbOptions) => {
7575
let value = '';
7676

@@ -89,7 +89,7 @@ const generateFormData = (body: GetterBody) => {
8989
}
9090

9191
return `const formData = new FormData(); formData.append('file', ${camel(
92-
body.implementation
92+
body.implementation,
9393
)});`;
9494
};
9595

@@ -101,16 +101,16 @@ const generateAxiosImplementation = (
101101
transformer,
102102
body,
103103
props,
104-
verb
104+
verb,
105105
}: GeneratorVerbOptions,
106-
{route}: GeneratorOptions
106+
{ route }: GeneratorOptions,
107107
) => {
108108
const axiosProps = generateAxiosProps({
109109
route,
110110
body,
111111
queryParams,
112112
response,
113-
verb
113+
verb,
114114
});
115115

116116
return ` ${definitionName}(${props.implementation}): AxiosPromise<${
@@ -126,25 +126,25 @@ const generateAxiosImplementation = (
126126
const generateImports = ({
127127
response,
128128
body,
129-
queryParams
129+
queryParams,
130130
}: GeneratorVerbOptions) => [
131131
...response.imports,
132132
...body.imports,
133-
...(queryParams ? [queryParams.name] : [])
133+
...(queryParams ? [queryParams.name] : []),
134134
];
135135

136136
export const generateAxiosHeader = (title: string) => ({
137137
definition: `export interface ${title} {`,
138-
implementation: `export const get${title} = (axios: AxiosInstance): ${title} => ({\n`
138+
implementation: `export const get${title} = (axios: AxiosInstance): ${title} => ({\n`,
139139
});
140140

141141
export const generateAxios = (
142142
verbOptions: GeneratorVerbOptions,
143-
options: GeneratorOptions
143+
options: GeneratorOptions,
144144
) => {
145145
const imports = generateImports(verbOptions);
146146
const definition = generateAxiosDefinition(verbOptions);
147147
const implementation = generateAxiosImplementation(verbOptions, options);
148148

149-
return {definition, implementation, imports};
149+
return { definition, implementation, imports };
150150
};

src/core/generators/client.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
import {GeneratorOptions, GeneratorVerbsOptions} from '../../types/generator';
2-
import {generateAxios, generateAxiosHeader} from './axios';
3-
import {generateMock} from './mocks';
1+
import { GeneratorOptions, GeneratorVerbsOptions } from '../../types/generator';
2+
import { generateAxios, generateAxiosHeader } from './axios';
3+
import { generateMock } from './mocks';
44

55
export const generateClientHeader = (title: string) => {
66
return {
77
...generateAxiosHeader(title),
8-
implementationMock: `export const get${title}Mock = (): ${title} => ({\n`
8+
implementationMock: `export const get${title}Mock = (): ${title} => ({\n`,
99
};
1010
};
1111

1212
export const generateClientFooter = () => {
13-
return {definition: '\n};', implementation: '});', implementationMock: '})'};
13+
return {
14+
definition: '\n};',
15+
implementation: '});',
16+
implementationMock: '})',
17+
};
1418
};
1519

1620
export const generateClient = (
1721
verbsOptions: GeneratorVerbsOptions,
18-
options: GeneratorOptions
22+
options: GeneratorOptions,
1923
) => {
2024
return verbsOptions.reduce((acc, verbOption) => {
2125
const axios = generateAxios(verbOption, options);
@@ -29,8 +33,8 @@ export const generateClient = (
2933
imports: axios.imports,
3034
implementationMocks: mock.implementation,
3135
importsMocks: mock.imports,
32-
tags: verbOption.tags
33-
}
36+
tags: verbOption.tags,
37+
},
3438
};
3539
}, {});
3640
};

0 commit comments

Comments
 (0)