Skip to content

Commit

Permalink
feat(lint): add husky, eslint, prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Mar 30, 2020
1 parent 621265f commit ec9cfac
Show file tree
Hide file tree
Showing 67 changed files with 1,684 additions and 832 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
extends: ['prettier/@typescript-eslint', 'plugin:prettier/recommended'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
ecmaFeatures: {
modules: true,
},
},
};
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"printWidth": 80,
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"trailingComma": "all"
}
3 changes: 3 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
};
19 changes: 16 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,22 @@
"rollup": "rollup -c rollup.config.js",
"prebuild": "rimraf ./lib && mkdir lib",
"build": "tsc && yarn rollup",
"lint": "tslint src/**/*.ts --project .",
"lint": "eslint src/**/*.ts",
"format": "prettier --write 'src/**/*.{js,ts}'",
"examples": "run-p example:*",
"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",
"example:file": "node lib/bin/orval.js import --input examples/petstore.yaml --output examples/petstoreFromFileSpec.ts",
"example:advanced": "node lib/bin/orval.js import --config examples/orval.config.js"
},
"husky": {
"hooks": {
"pre-commit": "yarn lint",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@types/chalk": "^2.2.0",
"@types/commander": "^2.12.2",
"@types/faker": "^4.1.8",
Expand All @@ -43,12 +52,17 @@
"@types/node": "^13.7.7",
"@types/request": "^2.48.4",
"@types/yamljs": "^0.2.30",
"@typescript-eslint/eslint-plugin": "^2.25.0",
"@typescript-eslint/parser": "^2.25.0",
"axios": "^0.19.2",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-prettier": "^3.1.2",
"faker": "^4.1.0",
"husky": "^4.2.3",
"lint-staged": "^10.0.8",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
"prettier": "^2.0.2",
"rimraf": "^3.0.2",
"rollup": "^1.32.0",
"rollup-plugin-typescript2": "^0.26.0",
Expand All @@ -63,7 +77,6 @@
"inquirer": "^7.0.6",
"lodash": "^4.17.15",
"openapi3-ts": "^1.3.0",
"request": "^2.88.2",
"swagger2openapi": "^5.3.3",
"yamljs": "^0.3.0"
},
Expand Down
20 changes: 9 additions & 11 deletions src/bin/orval-import.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import program from 'commander';
import difference from 'lodash/difference';
import {join} from 'path';
import {importSpecs} from '../core/importers/specs';
import {writeSpecs} from '../core/writers/specs';
import {ExternalConfigFile, Options} from '../types';
import {errorMessage, mismatchArgsMessage} from '../utils/messages/logs';
import { join } from 'path';
import { importSpecs } from '../core/importers/specs';
import { writeSpecs } from '../core/writers/specs';
import { ExternalConfigFile, Options } from '../types';
import { errorMessage, mismatchArgsMessage } from '../utils/messages/logs';

program.option('-o, --output [value]', 'output file destination');
program.option(
'-f, --input [value]',
'input file (yaml or json openapi specs)'
'input file (yaml or json openapi specs)',
);
program.option('--config [value]', 'override flags by a config file');
program.parse(process.argv);
Expand All @@ -25,7 +25,7 @@ if (program.config) {
// tslint:disable-next-line: no-var-requires
const config: ExternalConfigFile = require(join(
process.cwd(),
program.config
program.config,
));

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

Object.entries(config)
.filter(([backend]) =>
program.args.length === 0 ? true : program.args.includes(backend)
program.args.length === 0 ? true : program.args.includes(backend),
)
.forEach(([backend, options]) => {
importSpecs(options)
.then(writeSpecs(options, backend))
.catch(catchError);
importSpecs(options).then(writeSpecs(options, backend)).catch(catchError);
});
} else {
// Use flags as configuration
Expand Down
10 changes: 5 additions & 5 deletions src/bin/orval.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import program from 'commander';
import {startMessage} from '../utils/messages/logs';
import {getPackage} from '../utils/packages';
import { startMessage } from '../utils/messages/logs';
import { getPackage } from '../utils/packages';

const {name, version, description} = getPackage();
const { name, version, description } = getPackage();

startMessage({name, version, description});
startMessage({ name, version, description });

program
.version(version)
.command(
'import [open-api-file]',
'generate orval type-safe from OpenAPI specs'
'generate orval type-safe from OpenAPI specs',
)
.parse(process.argv);
6 changes: 3 additions & 3 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Verbs} from '../types';
import { Verbs } from '../types';

export const generalJSTypes = [
'number',
Expand All @@ -7,12 +7,12 @@ export const generalJSTypes = [
'unknown',
'undefined',
'object',
'blob'
'blob',
];

export const generalJSTypesWithArray = generalJSTypes.reduce<string[]>(
(acc, type) => [...acc, type, `Array<${type}>`, `${type}[]`],
[]
[],
);

export const VERBS_WITH_BODY = [Verbs.POST, Verbs.PUT, Verbs.PATCH];
Expand Down
26 changes: 13 additions & 13 deletions src/core/generators/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {OpenAPIObject, PathItemObject} from 'openapi3-ts';
import {OverrideOutput} from '../../types';
import {GeneratorApiResponse, GeneratorSchema} from '../../types/generator';
import {generateClient} from './client';
import {generateVerbsOptions} from './verbsOptions';
import { OpenAPIObject, PathItemObject } from 'openapi3-ts';
import { OverrideOutput } from '../../types';
import { GeneratorApiResponse, GeneratorSchema } from '../../types/generator';
import { generateClient } from './client';
import { generateVerbsOptions } from './verbsOptions';

export const generateApi = (
specs: OpenAPIObject,
override?: OverrideOutput
override?: OverrideOutput,
) => {
return Object.entries(specs.paths).reduce<GeneratorApiResponse>(
(acc, [pathRoute, verbs]: [string, PathItemObject]) => {
Expand All @@ -16,28 +16,28 @@ export const generateApi = (
verbs,
override,
route,
components: specs.components
components: specs.components,
});

const schemas = verbsOptions.reduce<GeneratorSchema[]>(
(acc, {queryParams}) => (queryParams ? [...acc, queryParams] : acc),
[]
(acc, { queryParams }) => (queryParams ? [...acc, queryParams] : acc),
[],
);

const client = generateClient(verbsOptions, {
route,
specs,
override
override,
});

return {
schemas: [...acc.schemas, ...schemas],
operations: {...acc.operations, ...client}
operations: { ...acc.operations, ...client },
};
},
{
operations: {},
schemas: []
}
schemas: [],
},
);
};
36 changes: 18 additions & 18 deletions src/core/generators/axios.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {camel} from 'case';
import {VERBS_WITH_BODY} from '../../constants';
import {Verbs} from '../../types';
import { camel } from 'case';
import { VERBS_WITH_BODY } from '../../constants';
import { Verbs } from '../../types';
import {
GeneratorOptions,
GeneratorSchema,
GeneratorVerbOptions
GeneratorVerbOptions,
} from '../../types/generator';
import {GetterBody, GetterResponse} from '../../types/getters';
import { GetterBody, GetterResponse } from '../../types/getters';

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

const generateQueryParamsProps = (
response: GetterResponse,
queryParams?: GeneratorSchema
queryParams?: GeneratorSchema,
) => {
if (!queryParams && !response.isBlob) {
return '';
Expand Down Expand Up @@ -52,7 +52,7 @@ const generateAxiosProps = ({
body,
queryParams,
response,
verb
verb,
}: {
route: string;
body: GetterBody;
Expand All @@ -62,15 +62,15 @@ const generateAxiosProps = ({
}) => {
return `\`${route}\` ${generateBodyProps(
body,
verb
verb,
)} ${generateQueryParamsProps(response, queryParams)}`;
};

const generateAxiosDefinition = ({
props,
definitionName,
response,
summary
summary,
}: GeneratorVerbOptions) => {
let value = '';

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

return `const formData = new FormData(); formData.append('file', ${camel(
body.implementation
body.implementation,
)});`;
};

Expand All @@ -101,16 +101,16 @@ const generateAxiosImplementation = (
transformer,
body,
props,
verb
verb,
}: GeneratorVerbOptions,
{route}: GeneratorOptions
{ route }: GeneratorOptions,
) => {
const axiosProps = generateAxiosProps({
route,
body,
queryParams,
response,
verb
verb,
});

return ` ${definitionName}(${props.implementation}): AxiosPromise<${
Expand All @@ -126,25 +126,25 @@ const generateAxiosImplementation = (
const generateImports = ({
response,
body,
queryParams
queryParams,
}: GeneratorVerbOptions) => [
...response.imports,
...body.imports,
...(queryParams ? [queryParams.name] : [])
...(queryParams ? [queryParams.name] : []),
];

export const generateAxiosHeader = (title: string) => ({
definition: `export interface ${title} {`,
implementation: `export const get${title} = (axios: AxiosInstance): ${title} => ({\n`
implementation: `export const get${title} = (axios: AxiosInstance): ${title} => ({\n`,
});

export const generateAxios = (
verbOptions: GeneratorVerbOptions,
options: GeneratorOptions
options: GeneratorOptions,
) => {
const imports = generateImports(verbOptions);
const definition = generateAxiosDefinition(verbOptions);
const implementation = generateAxiosImplementation(verbOptions, options);

return {definition, implementation, imports};
return { definition, implementation, imports };
};
20 changes: 12 additions & 8 deletions src/core/generators/client.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import {GeneratorOptions, GeneratorVerbsOptions} from '../../types/generator';
import {generateAxios, generateAxiosHeader} from './axios';
import {generateMock} from './mocks';
import { GeneratorOptions, GeneratorVerbsOptions } from '../../types/generator';
import { generateAxios, generateAxiosHeader } from './axios';
import { generateMock } from './mocks';

export const generateClientHeader = (title: string) => {
return {
...generateAxiosHeader(title),
implementationMock: `export const get${title}Mock = (): ${title} => ({\n`
implementationMock: `export const get${title}Mock = (): ${title} => ({\n`,
};
};

export const generateClientFooter = () => {
return {definition: '\n};', implementation: '});', implementationMock: '})'};
return {
definition: '\n};',
implementation: '});',
implementationMock: '})',
};
};

export const generateClient = (
verbsOptions: GeneratorVerbsOptions,
options: GeneratorOptions
options: GeneratorOptions,
) => {
return verbsOptions.reduce((acc, verbOption) => {
const axios = generateAxios(verbOption, options);
Expand All @@ -29,8 +33,8 @@ export const generateClient = (
imports: axios.imports,
implementationMocks: mock.implementation,
importsMocks: mock.imports,
tags: verbOption.tags
}
tags: verbOption.tags,
},
};
}, {});
};
6 changes: 3 additions & 3 deletions src/core/generators/imports.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {camel} from 'case';
import { camel } from 'case';

export const generateImports = (
imports: string[] = [],
path: string = '.',
pathOnly: boolean = false
pathOnly: boolean = false,
) => {
if (pathOnly) {
return `import {\n ${imports
Expand All @@ -12,6 +12,6 @@ export const generateImports = (
}
return imports
.sort()
.map(imp => `import { ${imp} } from \'${path}/${camel(imp)}\';`)
.map((imp) => `import { ${imp} } from \'${path}/${camel(imp)}\';`)
.join('\n');
};
Loading

0 comments on commit ec9cfac

Please sign in to comment.