From 384f19830267359536901996bfb138e743c6036a Mon Sep 17 00:00:00 2001 From: Patryk Tomczyk <13100280+patzick@users.noreply.github.com> Date: Thu, 1 Feb 2024 13:44:23 +0100 Subject: [PATCH] perf(api-gen): small improvements for schema generator to avoid unnceessary IO operations (#558) perf(api-gen): small improvements for schema generator to avoid unnecessary IO operations --- packages/api-gen/src/cli.ts | 2 +- packages/api-gen/src/commands/generate.ts | 39 +++++++++++++---------- packages/api-gen/src/patches.ts | 2 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/packages/api-gen/src/cli.ts b/packages/api-gen/src/cli.ts index ee63c37d2..4226dbfd0 100644 --- a/packages/api-gen/src/cli.ts +++ b/packages/api-gen/src/cli.ts @@ -10,7 +10,7 @@ export interface CommonOptions { cwd: string; } -function commonOptions(args: Argv<{}>): Argv { +function commonOptions(args: Argv): Argv { return args.option("cwd", { alias: "C", default: process.cwd(), diff --git a/packages/api-gen/src/commands/generate.ts b/packages/api-gen/src/commands/generate.ts index bfc319f95..1617f5c9d 100644 --- a/packages/api-gen/src/commands/generate.ts +++ b/packages/api-gen/src/commands/generate.ts @@ -46,24 +46,29 @@ export async function generate(args: { cwd: string; filename: string }) { const patchesToApply = allPatches.filter((patch) => { return semver.satisfies(semverVersion, patch); }); - patchesToApply.forEach((patchName) => { + for (const patchName of patchesToApply) { schemaForPatching = patches[patchName].patch(schemaForPatching); - }); - patchesToApply.length && - console.log("Applied", patchesToApply.length, "patches"); + } - const formatted = await format(JSON.stringify(schemaForPatching), { - semi: false, - parser: "json", - }); - const content = formatted.trim(); - writeFileSync(fullInputFilePath, content, { - encoding: "utf-8", - }); + if (patchesToApply.length) { + patchesToApply.length && + console.log("Applied", patchesToApply.length, "patches"); - const readedContentFromFile = readFileSync(fullInputFilePath, { - encoding: "utf-8", - }); + const formatted = await format(JSON.stringify(schemaForPatching), { + semi: false, + parser: "json", + }); + const content = formatted.trim(); + writeFileSync(fullInputFilePath, content, { + encoding: "utf-8", + }); + } + + const readedContentFromFile = !patchesToApply.length + ? schemaFile + : readFileSync(fullInputFilePath, { + encoding: "utf-8", + }); const originalSchema = JSON.parse(readedContentFromFile); const { paths } = originalSchema; @@ -175,7 +180,7 @@ export async function generate(args: { cwd: string; filename: string }) { (acc, path) => { const pathObject = paths[path]; const methods = Object.keys(pathObject); - methods.forEach((method) => { + for (const method of methods) { const methodObject = pathObject[method] as MethodObject; const { operationId } = methodObject; const queryParamNames = @@ -202,7 +207,7 @@ export async function generate(args: { cwd: string; filename: string }) { queryParamNames, finalPath, }; - }); + } return acc; }, {} as OperationsMap, diff --git a/packages/api-gen/src/patches.ts b/packages/api-gen/src/patches.ts index 5b8f2f458..934244bcb 100644 --- a/packages/api-gen/src/patches.ts +++ b/packages/api-gen/src/patches.ts @@ -15,7 +15,7 @@ export const patches = { schema.components.schemas ??= {}; schema.paths ??= {}; - schema.components.schemas["ContextTokenResponse"] ??= { + schema.components.schemas.ContextTokenResponse ??= { type: "object", properties: { contextToken: {