Skip to content

Commit

Permalink
perf(api-gen): small improvements for schema generator to avoid unnce…
Browse files Browse the repository at this point in the history
…essary IO operations (#558)

perf(api-gen): small improvements for schema generator to avoid unnecessary IO operations
  • Loading branch information
patzick authored Feb 1, 2024
1 parent 11a2706 commit 384f198
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/api-gen/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface CommonOptions {
cwd: string;
}

function commonOptions(args: Argv<{}>): Argv<CommonOptions> {
function commonOptions(args: Argv<unknown>): Argv<CommonOptions> {
return args.option("cwd", {
alias: "C",
default: process.cwd(),
Expand Down
39 changes: 22 additions & 17 deletions packages/api-gen/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand All @@ -202,7 +207,7 @@ export async function generate(args: { cwd: string; filename: string }) {
queryParamNames,
finalPath,
};
});
}
return acc;
},
{} as OperationsMap,
Expand Down
2 changes: 1 addition & 1 deletion packages/api-gen/src/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const patches = {
schema.components.schemas ??= {};
schema.paths ??= {};

schema.components.schemas["ContextTokenResponse"] ??= {
schema.components.schemas.ContextTokenResponse ??= {
type: "object",
properties: {
contextToken: {
Expand Down

0 comments on commit 384f198

Please sign in to comment.