Skip to content

Commit

Permalink
Merge pull request #10 from nicolas-chaulet/fix/autoformat-option
Browse files Browse the repository at this point in the history
fix(config): support autoformat option flag
  • Loading branch information
mrlubos authored Feb 2, 2024
2 parents 74adb1a + 059558e commit c8e7915
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const params = program
.option('--name <value>', 'Custom client class name')
.option('--useOptions', 'Use options instead of arguments')
.option('--useUnionTypes', 'Use union types instead of enums')
.option('--autoformat <value>', 'Process generated files with autoformatter', false)
.option('--exportCore <value>', 'Write core files to disk', true)
.option('--exportServices <value>', 'Write services to disk', true)
.option('--exportModels <value>', 'Write models to disk', true)
Expand Down Expand Up @@ -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),
Expand Down
14 changes: 14 additions & 0 deletions bin/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Options = {
clientName?: string;
useOptions?: boolean;
useUnionTypes?: boolean;
autoformat?: boolean;
exportCore?: boolean;
exportServices?: boolean | string;
exportModels?: boolean | string;
Expand Down Expand Up @@ -57,6 +58,7 @@ export const generate = async ({
clientName,
useOptions = false,
useUnionTypes = false,
autoformat = false,
exportCore = true,
exportServices = true,
exportModels = true,
Expand Down Expand Up @@ -100,6 +102,7 @@ export const generate = async ({
httpClient,
useOptions,
useUnionTypes,
autoformat,
exportCore,
exportServices,
exportModels,
Expand Down
1 change: 1 addition & 0 deletions src/utils/writeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('writeClient', () => {
HttpClient.FETCH,
false,
false,
false,
true,
true,
true,
Expand Down
13 changes: 13 additions & 0 deletions src/utils/writeClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { spawnSync } from 'child_process';
import { createRequire } from 'module';
import { resolve } from 'path';

import type { Client } from '../client/interfaces/Client';
Expand Down Expand Up @@ -40,6 +42,7 @@ export const writeClient = async (
httpClient: HttpClient,
useOptions: boolean,
useUnionTypes: boolean,
autoformat: boolean,
exportCore: boolean,
exportServices: boolean | string,
exportModels: boolean | string,
Expand Down Expand Up @@ -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]);
}
}
};
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('v2', () => {
httpClient: HttpClient.FETCH,
useOptions: false,
useUnionTypes: false,
autoformat: false,
exportCore: true,
exportSchemas: true,
exportModels: true,
Expand All @@ -32,6 +33,7 @@ describe('v3', () => {
httpClient: HttpClient.FETCH,
useOptions: false,
useUnionTypes: false,
autoformat: false,
exportCore: true,
exportSchemas: true,
exportModels: true,
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Options = {
clientName?: string;
useOptions?: boolean;
useUnionTypes?: boolean;
autoformat?: boolean;
exportCore?: boolean;
exportServices?: boolean | string;
exportModels?: boolean | string;
Expand Down

0 comments on commit c8e7915

Please sign in to comment.