From 15f7955ce4e48f8b3e38c590fc48b636e055b801 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Tue, 25 Jan 2022 10:16:09 +0100 Subject: [PATCH] - Working indentation --- README.md | 3 ++- bin/index.js | 2 ++ src/Indent.ts | 5 +++++ src/index.ts | 7 +++++++ .../{format.spec.ts => formatCode.spec.ts} | 14 +++++++------- src/utils/{format.ts => formatCode.ts} | 2 +- src/utils/formatIndentation.ts | 18 ++++++++++++++++++ src/utils/indent.ts | 9 --------- src/utils/writeClient.spec.ts | 16 +++++++++++++++- src/utils/writeClient.ts | 10 +++++++--- src/utils/writeClientCore.spec.ts | 3 ++- src/utils/writeClientCore.ts | 17 ++++++++++------- src/utils/writeClientModels.spec.ts | 3 ++- src/utils/writeClientModels.ts | 11 +++++++---- src/utils/writeClientSchemas.spec.ts | 3 ++- src/utils/writeClientSchemas.ts | 11 +++++++---- src/utils/writeClientServices.spec.ts | 3 ++- src/utils/writeClientServices.ts | 9 ++++++--- test/index.js | 1 + types/index.d.ts | 7 +++++++ 20 files changed, 110 insertions(+), 44 deletions(-) create mode 100644 src/Indent.ts rename src/utils/{format.spec.ts => formatCode.spec.ts} (55%) rename src/utils/{format.ts => formatCode.ts} (92%) create mode 100644 src/utils/formatIndentation.ts delete mode 100644 src/utils/indent.ts diff --git a/README.md b/README.md index ec076f73..985dc7c5 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,8 @@ $ openapi --help --exportServices Write services to disk (default: true) --exportModels Write models to disk (default: true) --exportSchemas Write schemas to disk (default: false) - --postfix Service name postfix (default: "Service") + --indent Service name postfix (default: "Service") + --postfix Indentation options [4, 2, tab] (default: "5") --request Path to custom request file -h, --help display help for command diff --git a/bin/index.js b/bin/index.js index 35c1c803..b1952439 100755 --- a/bin/index.js +++ b/bin/index.js @@ -19,6 +19,7 @@ const params = program .option('--exportServices ', 'Write services to disk', true) .option('--exportModels ', 'Write models to disk', true) .option('--exportSchemas ', 'Write schemas to disk', false) + .option('--indent ', 'Indentation options [4, 2, tabs]', '4') .option('--postfix ', 'Service name postfix', 'Service') .option('--request ', 'Path to custom request file') .parse(process.argv) @@ -37,6 +38,7 @@ if (OpenAPI) { exportServices: JSON.parse(params.exportServices) === true, exportModels: JSON.parse(params.exportModels) === true, exportSchemas: JSON.parse(params.exportSchemas) === true, + indent: params.indent, postfix: params.postfix, request: params.request, }) diff --git a/src/Indent.ts b/src/Indent.ts new file mode 100644 index 00000000..4203f34c --- /dev/null +++ b/src/Indent.ts @@ -0,0 +1,5 @@ +export enum Indent { + SPACE_4 = '4', + SPACE_2 = '2', + TAB = 'tab', +} diff --git a/src/index.ts b/src/index.ts index c27910c3..2edaa308 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { HttpClient } from './HttpClient'; +import { Indent } from './Indent'; import { parse as parseV2 } from './openApi/v2'; import { parse as parseV3 } from './openApi/v3'; import { getOpenApiSpec } from './utils/getOpenApiSpec'; @@ -9,6 +10,7 @@ import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates'; import { writeClient } from './utils/writeClient'; export { HttpClient } from './HttpClient'; +export { Indent } from './Indent'; export type Options = { input: string | Record; @@ -20,6 +22,7 @@ export type Options = { exportServices?: boolean; exportModels?: boolean; exportSchemas?: boolean; + indent?: Indent; postfix?: string; request?: string; write?: boolean; @@ -38,6 +41,7 @@ export type Options = { * @param exportServices: Generate services * @param exportModels: Generate models * @param exportSchemas: Generate schemas + * @param indent: Indentation options (4, 2 or tab) * @param postfix: Service name postfix * @param request: Path to custom request file * @param write Write the files to disk (true or false) @@ -52,6 +56,7 @@ export async function generate({ exportServices = true, exportModels = true, exportSchemas = false, + indent = Indent.SPACE_4, postfix = 'Service', request, write = true, @@ -80,6 +85,7 @@ export async function generate({ exportServices, exportModels, exportSchemas, + indent, postfix, request ); @@ -101,6 +107,7 @@ export async function generate({ exportServices, exportModels, exportSchemas, + indent, postfix, request ); diff --git a/src/utils/format.spec.ts b/src/utils/formatCode.spec.ts similarity index 55% rename from src/utils/format.spec.ts rename to src/utils/formatCode.spec.ts index 64751315..df46263b 100644 --- a/src/utils/format.spec.ts +++ b/src/utils/formatCode.spec.ts @@ -1,4 +1,4 @@ -import { format } from './format'; +import { formatCode } from './formatCode'; const input1 = `{ foo: true }`; @@ -30,11 +30,11 @@ const output4 = `{ describe('format', () => { it('should produce correct result', () => { - expect(format(``)).toEqual(''); - expect(format(`{}`)).toEqual('{}'); - expect(format(input1)).toEqual(output1); - expect(format(input2)).toEqual(output2); - expect(format(input3)).toEqual(output3); - expect(format(input4)).toEqual(output4); + expect(formatCode(``)).toEqual(''); + expect(formatCode(`{}`)).toEqual('{}'); + expect(formatCode(input1)).toEqual(output1); + expect(formatCode(input2)).toEqual(output2); + expect(formatCode(input3)).toEqual(output3); + expect(formatCode(input4)).toEqual(output4); }); }); diff --git a/src/utils/format.ts b/src/utils/formatCode.ts similarity index 92% rename from src/utils/format.ts rename to src/utils/formatCode.ts index 8479852a..17e45774 100644 --- a/src/utils/format.ts +++ b/src/utils/formatCode.ts @@ -1,6 +1,6 @@ import { EOL } from 'os'; -export function format(s: string): string { +export function formatCode(s: string): string { let indent: number = 0; let lines = s.split(EOL); lines = lines.map(line => { diff --git a/src/utils/formatIndentation.ts b/src/utils/formatIndentation.ts new file mode 100644 index 00000000..903eec79 --- /dev/null +++ b/src/utils/formatIndentation.ts @@ -0,0 +1,18 @@ +import { EOL } from 'os'; + +import { Indent } from '../Indent'; + +export function formatIndentation(s: string, indent: Indent): string { + let lines = s.split(EOL); + lines = lines.map(line => { + switch (indent) { + case Indent.SPACE_4: + return line.replace(/\t/g, ' '); + case Indent.SPACE_2: + return line.replace(/\t/g, ' '); + case Indent.TAB: + return line; // Default output is tab formatted + } + }); + return lines.join(EOL); +} diff --git a/src/utils/indent.ts b/src/utils/indent.ts deleted file mode 100644 index e6566997..00000000 --- a/src/utils/indent.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { EOL } from 'os'; - -export function indent(s: string): string { - let lines = s.split(EOL); - lines = lines.map(line => { - return line.replace(/\t/g, ' '); - }); - return lines.join(EOL); -} diff --git a/src/utils/writeClient.spec.ts b/src/utils/writeClient.spec.ts index 26802757..5c472bea 100644 --- a/src/utils/writeClient.spec.ts +++ b/src/utils/writeClient.spec.ts @@ -1,5 +1,6 @@ import type { Client } from '../client/interfaces/Client'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { mkdir, rmdir, writeFile } from './fileSystem'; import { Templates } from './registerHandlebarTemplates'; import { writeClient } from './writeClient'; @@ -32,7 +33,20 @@ describe('writeClient', () => { }, }; - await writeClient(client, templates, './dist', HttpClient.FETCH, false, false, true, true, true, true, ''); + await writeClient( + client, + templates, + './dist', + HttpClient.FETCH, + false, + false, + true, + true, + true, + true, + Indent.SPACE_4, + '' + ); expect(rmdir).toBeCalled(); expect(mkdir).toBeCalled(); diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index f1858bee..9f09ecd9 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -2,6 +2,7 @@ import { resolve } from 'path'; import type { Client } from '../client/interfaces/Client'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { mkdir, rmdir } from './fileSystem'; import { isSubDirectory } from './isSubdirectory'; import { Templates } from './registerHandlebarTemplates'; @@ -24,6 +25,7 @@ import { writeClientServices } from './writeClientServices'; * @param exportModels: Generate models * @param exportSchemas: Generate schemas * @param exportSchemas: Generate schemas + * @param indent: Indentation options (4, 2 or tab) * @param postfix: Service name postfix * @param request: Path to custom request file */ @@ -38,6 +40,7 @@ export async function writeClient( exportServices: boolean, exportModels: boolean, exportSchemas: boolean, + indent: Indent, postfix: string, request?: string ): Promise { @@ -54,7 +57,7 @@ export async function writeClient( if (exportCore) { await rmdir(outputPathCore); await mkdir(outputPathCore); - await writeClientCore(client, templates, outputPathCore, httpClient, request); + await writeClientCore(client, templates, outputPathCore, httpClient, indent, request); } if (exportServices) { @@ -67,6 +70,7 @@ export async function writeClient( httpClient, useUnionTypes, useOptions, + indent, postfix ); } @@ -74,13 +78,13 @@ export async function writeClient( if (exportSchemas) { await rmdir(outputPathSchemas); await mkdir(outputPathSchemas); - await writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes); + await writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes, indent); } if (exportModels) { await rmdir(outputPathModels); await mkdir(outputPathModels); - await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes); + await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes, indent); } if (exportCore || exportServices || exportSchemas || exportModels) { diff --git a/src/utils/writeClientCore.spec.ts b/src/utils/writeClientCore.spec.ts index 0b6e0c01..b96780ad 100644 --- a/src/utils/writeClientCore.spec.ts +++ b/src/utils/writeClientCore.spec.ts @@ -1,5 +1,6 @@ import type { Client } from '../client/interfaces/Client'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { writeFile } from './fileSystem'; import { Templates } from './registerHandlebarTemplates'; import { writeClientCore } from './writeClientCore'; @@ -32,7 +33,7 @@ describe('writeClientCore', () => { }, }; - await writeClientCore(client, templates, '/', HttpClient.FETCH); + await writeClientCore(client, templates, '/', HttpClient.FETCH, Indent.SPACE_4); expect(writeFile).toBeCalledWith('/OpenAPI.ts', 'settings'); expect(writeFile).toBeCalledWith('/ApiError.ts', 'apiError'); diff --git a/src/utils/writeClientCore.ts b/src/utils/writeClientCore.ts index b2545603..fe498f6b 100644 --- a/src/utils/writeClientCore.ts +++ b/src/utils/writeClientCore.ts @@ -2,8 +2,9 @@ import { resolve } from 'path'; import type { Client } from '../client/interfaces/Client'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { copyFile, exists, writeFile } from './fileSystem'; -import { indent } from './indent'; +import { formatIndentation as i } from './formatIndentation'; import { Templates } from './registerHandlebarTemplates'; /** @@ -12,6 +13,7 @@ import { Templates } from './registerHandlebarTemplates'; * @param templates The loaded handlebar templates * @param outputPath Directory to write the generated files to * @param httpClient The selected httpClient (fetch, xhr, node or axios) + * @param indent: Indentation options (4, 2 or tab) * @param request: Path to custom request file */ export async function writeClientCore( @@ -19,6 +21,7 @@ export async function writeClientCore( templates: Templates, outputPath: string, httpClient: HttpClient, + indent: Indent, request?: string ): Promise { const context = { @@ -27,12 +30,12 @@ export async function writeClientCore( version: client.version, }; - await writeFile(resolve(outputPath, 'OpenAPI.ts'), indent(templates.core.settings(context))); - await writeFile(resolve(outputPath, 'ApiError.ts'), indent(templates.core.apiError({}))); - await writeFile(resolve(outputPath, 'ApiRequestOptions.ts'), indent(templates.core.apiRequestOptions({}))); - await writeFile(resolve(outputPath, 'ApiResult.ts'), indent(templates.core.apiResult({}))); - await writeFile(resolve(outputPath, 'CancelablePromise.ts'), indent(templates.core.cancelablePromise({}))); - await writeFile(resolve(outputPath, 'request.ts'), indent(templates.core.request(context))); + await writeFile(resolve(outputPath, 'OpenAPI.ts'), i(templates.core.settings(context), indent)); + await writeFile(resolve(outputPath, 'ApiError.ts'), i(templates.core.apiError(context), indent)); + await writeFile(resolve(outputPath, 'ApiRequestOptions.ts'), i(templates.core.apiRequestOptions(context), indent)); + await writeFile(resolve(outputPath, 'ApiResult.ts'), i(templates.core.apiResult(context), indent)); + await writeFile(resolve(outputPath, 'CancelablePromise.ts'), i(templates.core.cancelablePromise(context), indent)); + await writeFile(resolve(outputPath, 'request.ts'), i(templates.core.request(context), indent)); if (request) { const requestFile = resolve(process.cwd(), request); diff --git a/src/utils/writeClientModels.spec.ts b/src/utils/writeClientModels.spec.ts index 67b093fe..359849dc 100644 --- a/src/utils/writeClientModels.spec.ts +++ b/src/utils/writeClientModels.spec.ts @@ -1,5 +1,6 @@ import type { Model } from '../client/interfaces/Model'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { writeFile } from './fileSystem'; import { Templates } from './registerHandlebarTemplates'; import { writeClientModels } from './writeClientModels'; @@ -45,7 +46,7 @@ describe('writeClientModels', () => { }, }; - await writeClientModels(models, templates, '/', HttpClient.FETCH, false); + await writeClientModels(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4); expect(writeFile).toBeCalledWith('/User.ts', 'model'); }); diff --git a/src/utils/writeClientModels.ts b/src/utils/writeClientModels.ts index ba8538e4..7c58bd85 100644 --- a/src/utils/writeClientModels.ts +++ b/src/utils/writeClientModels.ts @@ -2,9 +2,10 @@ import { resolve } from 'path'; import type { Model } from '../client/interfaces/Model'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { writeFile } from './fileSystem'; -import { format } from './format'; -import { indent } from './indent'; +import { formatCode as f } from './formatCode'; +import { formatIndentation as i } from './formatIndentation'; import { Templates } from './registerHandlebarTemplates'; /** @@ -14,13 +15,15 @@ import { Templates } from './registerHandlebarTemplates'; * @param outputPath Directory to write the generated files to * @param httpClient The selected httpClient (fetch, xhr, node or axios) * @param useUnionTypes Use union types instead of enums + * @param indent: Indentation options (4, 2 or tab) */ export async function writeClientModels( models: Model[], templates: Templates, outputPath: string, httpClient: HttpClient, - useUnionTypes: boolean + useUnionTypes: boolean, + indent: Indent ): Promise { for (const model of models) { const file = resolve(outputPath, `${model.name}.ts`); @@ -29,6 +32,6 @@ export async function writeClientModels( httpClient, useUnionTypes, }); - await writeFile(file, indent(format(templateResult))); + await writeFile(file, i(f(templateResult), indent)); } } diff --git a/src/utils/writeClientSchemas.spec.ts b/src/utils/writeClientSchemas.spec.ts index dccc79fb..4c1f34ce 100644 --- a/src/utils/writeClientSchemas.spec.ts +++ b/src/utils/writeClientSchemas.spec.ts @@ -1,5 +1,6 @@ import type { Model } from '../client/interfaces/Model'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { writeFile } from './fileSystem'; import { Templates } from './registerHandlebarTemplates'; import { writeClientSchemas } from './writeClientSchemas'; @@ -45,7 +46,7 @@ describe('writeClientSchemas', () => { }, }; - await writeClientSchemas(models, templates, '/', HttpClient.FETCH, false); + await writeClientSchemas(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4); expect(writeFile).toBeCalledWith('/$User.ts', 'schema'); }); diff --git a/src/utils/writeClientSchemas.ts b/src/utils/writeClientSchemas.ts index 68a551ed..74fdc0c9 100644 --- a/src/utils/writeClientSchemas.ts +++ b/src/utils/writeClientSchemas.ts @@ -2,9 +2,10 @@ import { resolve } from 'path'; import type { Model } from '../client/interfaces/Model'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { writeFile } from './fileSystem'; -import { format } from './format'; -import { indent } from './indent'; +import { formatCode as f } from './formatCode'; +import { formatIndentation as i } from './formatIndentation'; import { Templates } from './registerHandlebarTemplates'; /** @@ -14,13 +15,15 @@ import { Templates } from './registerHandlebarTemplates'; * @param outputPath Directory to write the generated files to * @param httpClient The selected httpClient (fetch, xhr, node or axios) * @param useUnionTypes Use union types instead of enums + * @param indent: Indentation options (4, 2 or tab) */ export async function writeClientSchemas( models: Model[], templates: Templates, outputPath: string, httpClient: HttpClient, - useUnionTypes: boolean + useUnionTypes: boolean, + indent: Indent ): Promise { for (const model of models) { const file = resolve(outputPath, `$${model.name}.ts`); @@ -29,6 +32,6 @@ export async function writeClientSchemas( httpClient, useUnionTypes, }); - await writeFile(file, indent(format(templateResult))); + await writeFile(file, i(f(templateResult), indent)); } } diff --git a/src/utils/writeClientServices.spec.ts b/src/utils/writeClientServices.spec.ts index 1f6798ef..1f185db8 100644 --- a/src/utils/writeClientServices.spec.ts +++ b/src/utils/writeClientServices.spec.ts @@ -1,5 +1,6 @@ import type { Service } from '../client/interfaces/Service'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { writeFile } from './fileSystem'; import { Templates } from './registerHandlebarTemplates'; import { writeClientServices } from './writeClientServices'; @@ -33,7 +34,7 @@ describe('writeClientServices', () => { }, }; - await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, 'Service'); + await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4, 'Service'); expect(writeFile).toBeCalledWith('/UserService.ts', 'service'); }); diff --git a/src/utils/writeClientServices.ts b/src/utils/writeClientServices.ts index 91124ecb..f094f69b 100644 --- a/src/utils/writeClientServices.ts +++ b/src/utils/writeClientServices.ts @@ -2,9 +2,10 @@ import { resolve } from 'path'; import type { Service } from '../client/interfaces/Service'; import { HttpClient } from '../HttpClient'; +import { Indent } from '../Indent'; import { writeFile } from './fileSystem'; -import { format } from './format'; -import { indent } from './indent'; +import { formatCode as f } from './formatCode'; +import { formatIndentation as i } from './formatIndentation'; import { Templates } from './registerHandlebarTemplates'; const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION'; @@ -17,6 +18,7 @@ const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION'; * @param httpClient The selected httpClient (fetch, xhr, node or axios) * @param useUnionTypes Use union types instead of enums * @param useOptions Use options or arguments functions + * @param indent: Indentation options (4, 2 or tab) * @param postfix: Service name postfix */ export async function writeClientServices( @@ -26,6 +28,7 @@ export async function writeClientServices( httpClient: HttpClient, useUnionTypes: boolean, useOptions: boolean, + indent: Indent, postfix: string ): Promise { for (const service of services) { @@ -39,6 +42,6 @@ export async function writeClientServices( useOptions, postfix, }); - await writeFile(file, indent(format(templateResult))); + await writeFile(file, i(f(templateResult), indent)); } } diff --git a/test/index.js b/test/index.js index 7817170f..db0a8d4b 100644 --- a/test/index.js +++ b/test/index.js @@ -14,6 +14,7 @@ const generate = async (input, output) => { exportSchemas: true, exportModels: true, exportServices: true, + // indent: OpenAPI.Indent.SPACE_2, // postfix: 'Api', // request: './test/custom/request.ts', }); diff --git a/types/index.d.ts b/types/index.d.ts index 261800bd..9104791f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -5,6 +5,12 @@ export declare enum HttpClient { AXIOS = 'axios', } +export declare enum Indent { + SPACE_4 = '4', + SPACE_2 = '2', + TAB = 'tab', +} + export type Options = { input: string | Record; output: string; @@ -15,6 +21,7 @@ export type Options = { exportServices?: boolean; exportModels?: boolean; exportSchemas?: boolean; + indent?: Indent | '4' | '2' | 'tab'; postfix?: string; request?: string; write?: boolean;