diff --git a/bin/index.js b/bin/index.js index 36664e351..ecc915aa8 100755 --- a/bin/index.js +++ b/bin/index.js @@ -16,6 +16,7 @@ const params = program .option('--name ', 'Custom client class name') .option('--useOptions', 'Use options instead of arguments') .option('--useUnionTypes', 'Use union types instead of enums') + .option('--autoformat ', 'Process generated files with autoformatter', false) .option('--exportCore ', 'Write core files to disk', true) .option('--exportServices ', 'Write services to disk', true) .option('--exportModels ', 'Write models to disk', true) @@ -45,6 +46,7 @@ if (OpenAPI) { clientName: params.name, useOptions: params.useOptions, useUnionTypes: params.useUnionTypes, + autoformat: JSON.parse(params.autoformat) === true, exportCore: JSON.parse(params.exportCore) === true, exportServices: parseBooleanOrString(params.exportServices), exportModels: parseBooleanOrString(params.exportModels), diff --git a/bin/index.spec.js b/bin/index.spec.js index ff1cff75a..5d6d86aec 100755 --- a/bin/index.spec.js +++ b/bin/index.spec.js @@ -59,6 +59,20 @@ describe('bin', () => { expect(result.stderr.toString()).toBe(''); }); + it('should autoformat with Prettier', async () => { + const result = crossSpawn.sync('node', [ + './bin/index.js', + '--input', + './test/spec/v3.json', + '--output', + './test/generated/bin', + '--autoformat', + 'true', + ]); + expect(result.stdout.toString()).toBe(''); + expect(result.stderr.toString()).toBe(''); + }); + it('it should throw error without params', async () => { const result = crossSpawn.sync('node', ['./bin/index.js']); expect(result.stdout.toString()).toBe(''); diff --git a/src/index.ts b/src/index.ts index 749a2cfb2..f6b9b27b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,7 @@ export type Options = { clientName?: string; useOptions?: boolean; useUnionTypes?: boolean; + autoformat?: boolean; exportCore?: boolean; exportServices?: boolean | string; exportModels?: boolean | string; @@ -57,6 +58,7 @@ export const generate = async ({ clientName, useOptions = false, useUnionTypes = false, + autoformat = false, exportCore = true, exportServices = true, exportModels = true, @@ -100,6 +102,7 @@ export const generate = async ({ httpClient, useOptions, useUnionTypes, + autoformat, exportCore, exportServices, exportModels, diff --git a/src/utils/writeClient.spec.ts b/src/utils/writeClient.spec.ts index 3c06a95a5..2bfc3e284 100644 --- a/src/utils/writeClient.spec.ts +++ b/src/utils/writeClient.spec.ts @@ -43,6 +43,7 @@ describe('writeClient', () => { HttpClient.FETCH, false, false, + false, true, true, true, diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index 6cc491ecd..06b1e0b72 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -1,3 +1,5 @@ +import { spawnSync } from 'child_process'; +import { createRequire } from 'module'; import { resolve } from 'path'; import type { Client } from '../client/interfaces/Client'; @@ -40,6 +42,7 @@ export const writeClient = async ( httpClient: HttpClient, useOptions: boolean, useUnionTypes: boolean, + autoformat: boolean, exportCore: boolean, exportServices: boolean | string, exportModels: boolean | string, @@ -125,4 +128,14 @@ export const writeClient = async ( clientName ); } + + if (autoformat) { + const pathPackageJson = resolve(process.cwd(), 'package.json'); + const require = createRequire('/'); + const json = require(pathPackageJson); + const usesPrettier = [json.dependencies, json.devDependencies].some(deps => Boolean(deps.prettier)); + if (usesPrettier) { + spawnSync('prettier', ['--ignore-unknown', '--write', output]); + } + } }; diff --git a/test/index.js b/test/index.js index 6d276c412..f60c9b047 100644 --- a/test/index.js +++ b/test/index.js @@ -10,6 +10,7 @@ const generate = async (input, output) => { httpClient: OpenAPI.HttpClient.FETCH, useOptions: true, useUnionTypes: false, + autoformat: false, exportCore: true, exportSchemas: true, exportModels: true, diff --git a/test/index.spec.ts b/test/index.spec.ts index 78a0197d5..118a293ad 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -11,6 +11,7 @@ describe('v2', () => { httpClient: HttpClient.FETCH, useOptions: false, useUnionTypes: false, + autoformat: false, exportCore: true, exportSchemas: true, exportModels: true, @@ -32,6 +33,7 @@ describe('v3', () => { httpClient: HttpClient.FETCH, useOptions: false, useUnionTypes: false, + autoformat: false, exportCore: true, exportSchemas: true, exportModels: true, diff --git a/types/index.d.ts b/types/index.d.ts index 127ec7af9..4b4b2beda 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -19,6 +19,7 @@ export type Options = { clientName?: string; useOptions?: boolean; useUnionTypes?: boolean; + autoformat?: boolean; exportCore?: boolean; exportServices?: boolean | string; exportModels?: boolean | string;