Skip to content

Commit

Permalink
feat: classify code generator (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon authored Apr 5, 2021
1 parent 881625c commit 09f8509
Show file tree
Hide file tree
Showing 57 changed files with 287 additions and 273 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const main = () => {
const params: CodeGenerator.Params = {
entryPoint: "your/openapi/spec.yml", // support .yml, .yaml, .json
option: {
rewriteCodeAfterTypeDeclaration: (context: ts.TransformationContext, codeGeneratorParamsList: CodeGenerator.Converter.v3.CodeGeneratorParams[]): ts.Statement[] => {
rewriteCodeAfterTypeDeclaration: (context: Pick<ts.TransformationContext, "factory">, codeGeneratorParamsList: CodeGenerator.Converter.v3.CodeGeneratorParams[]): ts.Statement[] => {
const factory = context.factory; // https://ts-ast-viewer.com/ is very very very useful !
return []; // generate no api client
},
Expand Down
2 changes: 1 addition & 1 deletion docs/ja/README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const main = () => {
const params: CodeGenerator.Params = {
entryPoint: "your/openapi/spec.yml", // support .yml, .yaml, .json
option: {
rewriteCodeAfterTypeDeclaration: (context: ts.TransformationContext, codeGeneratorParamsList: CodeGenerator.Converter.v3.CodeGeneratorParams[]): ts.Statement[] => {
rewriteCodeAfterTypeDeclaration: (context: Pick<ts.TransformationContext, "factory">, codeGeneratorParamsList: CodeGenerator.Converter.v3.CodeGeneratorParams[]): ts.Statement[] => {
const factory = context.factory; // https://ts-ast-viewer.com/ is very very very useful !
return []; // generate no api client
},
Expand Down
74 changes: 28 additions & 46 deletions scripts/testCodeGen.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,41 @@
import * as fs from "fs";

import * as CodeGenerator from "../lib";
import { CodeGenerator, DefaultCodeTemplate } from "../lib";

const gen = (name: string, enableValidate = true): void => {
const params: CodeGenerator.OpenApiTsCodeGen.Configuration = {
entryPoint: `test/${name}/index.yml`,
codeGenerator: {
templates: {
Default: CodeGenerator.DefaultCodeTemplate.makeApiClient,
},
},
typeDefinitionGenerator: {
additional: {
template: "Default",
},
},
validator: {
openapiSchema: enableValidate,
logger: {
displayLogLines: 1,
},
},
};
fs.mkdirSync("test/code", { recursive: true });
const output = CodeGenerator.make(params);
fs.writeFileSync(`test/code/${name}.ts`, output.typeDefinition.value, { encoding: "utf-8" });
const codeGenerator = new CodeGenerator(`test/${name}/index.yml`);
// codeGenerator.validate();
const code = codeGenerator.generateTypeDefinition<DefaultCodeTemplate.Option>({
generator: DefaultCodeTemplate.makeApiClient,
option: {
sync: false,
},
});
if (enableValidate) {
codeGenerator.validate({
logger: { displayLogLines: 1 },
});
}
fs.writeFileSync(`test/code/${name}.ts`, code, { encoding: "utf-8" });
console.log(`Generate Code : test/code/${name}.ts`);
};

const genSyncMode = (name: string, enableValidate = true): void => {
const params: CodeGenerator.OpenApiTsCodeGen.Configuration = {
entryPoint: `test/${name}/index.yml`,
codeGenerator: {
templates: {
Default: CodeGenerator.DefaultCodeTemplate.makeApiClient,
},
},
typeDefinitionGenerator: {
additional: {
template: "Default",
option: {
sync: true,
},
},
},
validator: {
openapiSchema: enableValidate,
logger: {
displayLogLines: 1,
},
},
};
const codeGenerator = new CodeGenerator(`test/${name}/index.yml`);
fs.mkdirSync("test/code", { recursive: true });
const code = CodeGenerator.make(params);
fs.writeFileSync(`test/code/sync-${name}.ts`, code.typeDefinition.value, { encoding: "utf-8" });
if (enableValidate) {
codeGenerator.validate({
logger: { displayLogLines: 1 },
});
}
const code = codeGenerator.generateTypeDefinition<DefaultCodeTemplate.Option>({
generator: DefaultCodeTemplate.makeApiClient,
option: {
sync: true,
},
});
fs.writeFileSync(`test/code/sync-${name}.ts`, code, { encoding: "utf-8" });
console.log(`Generate Code : test/code/sync-${name}.ts`);
};

Expand Down
2 changes: 1 addition & 1 deletion scripts/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ const validator = new OpenAPISchemaValidator({
},
});

const message = validator.validate(yaml.safeLoad(data) as any);
const message = validator.validate(yaml.load(data) as any);

fs.writeFileSync(path.join(__dirname, "../debug/validate.json"), JSON.stringify(message, null, 2), { encoding: "utf-8" });
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/BinaryExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export interface Factory {
create: (params: Params) => ts.BinaryExpression;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params): ts.BinaryExpression => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params): ts.BinaryExpression => {
const node = factory.createBinaryExpression(params.left, operators[params.operator], params.right);
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export interface Factory {
create: (params: Params$Create) => ts.Block;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.Block => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.Block => {
const node = factory.createBlock(params.statements, params.multiLine);
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/CallExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export interface Factory {
create: (params: Params$Create) => ts.CallExpression;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.CallExpression => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.CallExpression => {
const node = factory.createCallExpression(params.expression, params.typeArguments, params.argumentsArray);
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/ClassDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Factory {
create: (params: Params$Create) => ts.ClassDeclaration;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.ClassDeclaration => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.ClassDeclaration => {
const node = factory.createClassDeclaration(
undefined,
params.export && [factory.createModifier(ts.SyntaxKind.ExportKeyword)],
Expand All @@ -23,7 +23,7 @@ export const create = ({ factory }: ts.TransformationContext): Factory["create"]
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/ConstructorDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export interface Factory {
create: (params: Params$Create) => ts.ConstructorDeclaration;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.ConstructorDeclaration => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.ConstructorDeclaration => {
const node = factory.createConstructorDeclaration(undefined, undefined, params.parameters || [], params.body);
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/ElementAccessExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export interface Factory {
create: (params: Params$Create) => ts.ElementAccessExpression;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.ElementAccessExpression => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.ElementAccessExpression => {
const index = typeof params.index === "string" ? factory.createStringLiteral(params.index) : params.index;
const node = factory.createElementAccessExpression(params.expression, index);
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/FunctionTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export interface Factory {
create: (params: Params$Create) => ts.FunctionTypeNode;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.FunctionTypeNode => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.FunctionTypeNode => {
const node = factory.createFunctionTypeNode(params.typeParameters, params.parameters, params.type);
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/Identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export interface Factory {
create: (params: Params) => ts.Identifier;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params): ts.Identifier => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params): ts.Identifier => {
const node = factory.createIdentifier(params.name);
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/IndexSignatureDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Factory {
create: (params: Params$Create) => ts.IndexSignatureDeclaration;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.IndexSignatureDeclaration => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.IndexSignatureDeclaration => {
const node = factory.createIndexSignature(
undefined,
undefined,
Expand All @@ -30,7 +30,7 @@ export const create = ({ factory }: ts.TransformationContext): Factory["create"]
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/IndexedAccessTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export interface Factory {
create: (params: Params$Create) => ts.IndexedAccessTypeNode;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.IndexedAccessTypeNode => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.IndexedAccessTypeNode => {
const node = factory.createIndexedAccessTypeNode(params.objectType, params.indexType);
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/InterfaceDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface Factory {
create: (params: Params$Create) => ts.InterfaceDeclaration;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.InterfaceDeclaration => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.InterfaceDeclaration => {
const node = factory.createInterfaceDeclaration(
undefined,
params.export && [factory.createModifier(ts.SyntaxKind.ExportKeyword)],
Expand All @@ -31,7 +31,7 @@ export const create = ({ factory }: ts.TransformationContext): Factory["create"]
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/IntersectionTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export interface Factory {
create: (params: Params) => ts.IntersectionTypeNode;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params): ts.IntersectionTypeNode => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params): ts.IntersectionTypeNode => {
const node = factory.createIntersectionTypeNode(params.typeNodes);
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/LiteralTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Factory {
create: (params: Params) => ts.LiteralTypeNode;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params): ts.LiteralTypeNode => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params): ts.LiteralTypeNode => {
const createNode = () => {
if (typeof params.value === "string") {
const literal = ts.setEmitFlags(factory.createStringLiteral(params.value), ts.EmitFlags.NoAsciiEscaping);
Expand All @@ -31,7 +31,7 @@ export const create = ({ factory }: ts.TransformationContext): Factory["create"]
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/MethodDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Factory {
create: (params: Params$Create) => ts.MethodDeclaration;
}

export const create = ({ factory }: ts.TransformationContext): Factory["create"] => (params: Params$Create): ts.MethodDeclaration => {
export const create = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] => (params: Params$Create): ts.MethodDeclaration => {
const modifiers: ts.Modifier[] = [];
if (params.private) {
modifiers.push(factory.createModifier(ts.SyntaxKind.PrivateKeyword));
Expand Down Expand Up @@ -46,7 +46,7 @@ export const create = ({ factory }: ts.TransformationContext): Factory["create"]
return node;
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
create: create(context),
};
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenerator/factory/ModuleBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export interface Factory {
update: (params: UpdateParams) => ts.ModuleBlock;
}

export const update = ({ factory }: ts.TransformationContext): Factory["update"] => (params: UpdateParams): ts.ModuleBlock => {
export const update = ({ factory }: Pick<ts.TransformationContext, "factory">): Factory["update"] => (params: UpdateParams): ts.ModuleBlock => {
const { node, statements } = params;
return factory.updateModuleBlock(node, statements);
};

export const make = (context: ts.TransformationContext): Factory => {
export const make = (context: Pick<ts.TransformationContext, "factory">): Factory => {
return {
update: update(context),
};
Expand Down
Loading

0 comments on commit 09f8509

Please sign in to comment.