Skip to content

Commit

Permalink
do not rollout template files
Browse files Browse the repository at this point in the history
  • Loading branch information
inkognitro committed Mar 23, 2024
1 parent df8c91b commit a1b130a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/oas3/codegen/ts/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
templateRequestResultType,
templateRequestType,
} from './template';
import {EndpointId} from './template/core';

type EndpointId = {
method: string;
path: string;
};

function applyRequestResultTypeDefinition(
codeGenerator: CodeGenerator,
Expand Down
50 changes: 47 additions & 3 deletions src/oas3/codegen/ts/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
Specification,
} from '@oas3/specification';
import {applyEndpointCallerFunction} from '@oas3/codegen/ts/endpoint';
import {EndpointId} from '@oas3/codegen/ts/template/core';
import {mkdirp} from 'mkdirp';
const fs = require('fs');

Expand Down Expand Up @@ -91,6 +90,9 @@ export class DefaultCodeGenerator implements CodeGenerator {
targetFolderPath: string
) {
const cleanTargetFolderPath = cleanUpFolderPath(targetFolderPath);
fs.cpSync(__dirname + '/template', cleanTargetFolderPath, {
recursive: true,
});
for (const filePath in fileOutputByFilePath) {
const fileOutput = fileOutputByFilePath[filePath];
const fsFilePath = `${cleanTargetFolderPath}${filePath}`;
Expand All @@ -101,7 +103,49 @@ export class DefaultCodeGenerator implements CodeGenerator {
}

private createFileContent(fileOutput: FileOutput): string {
return 'pseudo content...'; // todo: implement
const importContents: string[] = [];
for (const importPath in fileOutput.importNamesByPath) {
const importNames = fileOutput.importNamesByPath[importPath];
if (!importNames.length) {
continue;
}
importContents.push(
`import {${importNames.join(', ')}} from '${importPath}';`
);
}
const definitionContents: string[] = [];
fileOutput.definitions.forEach(o => {
switch (o.definitionType) {
case 'function':
definitionContents.push(
`export function ${o.createName(o.path)} ${o.createCode(o.path)}`
);
break;
case 'const':
definitionContents.push(
`export const ${o.createName(o.path)} = ${o.createCode(o.path)}`
);
break;
case 'enum':
definitionContents.push(
`export enum ${o.createName(o.path)} ${o.createCode(o.path)}`
);
break;
case 'type':
definitionContents.push(
`export type ${o.createName(o.path)} = ${o.createCode(o.path)}`
);
break;
default:
throw new Error(`output type "${o.definitionType}" is not supported`);
}
});
let content = importContents.join('\n');
if (importContents.length && definitionContents.length) {
content += '\n\n';
}
content += definitionContents.join('\n');
return content;
}

private generateRequestRequestByMethodMapOutputs(
Expand All @@ -110,7 +154,7 @@ export class DefaultCodeGenerator implements CodeGenerator {
) {
for (const method in requestByMethodMap) {
const requestSchema = requestByMethodMap[method];
const endpointId: EndpointId = {path, method};
const endpointId = {path, method};
applyEndpointCallerFunction(this, endpointId, requestSchema);
this.operationIdOutputPaths.push(
this.createOperationIdOutputPath(requestSchema.operationId)
Expand Down
2 changes: 1 addition & 1 deletion src/oas3/codegen/ts/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getTemplateResponseStatusCodeEnumEntry,
templateResponseType,
templateStatusCodeEnum,
} from '@oas3/codegen/ts/template';
} from './template';

function applyStatusCodeResponseAndGetTypeDefinitionOutput(
codeGenerator: CodeGenerator,
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
],
"exclude": [
"src/**/*.spec.ts",
"src/**/*.test.ts"
"src/**/*.test.ts",
"src/oas3/codegen/ts/template/**",
]
}

0 comments on commit a1b130a

Please sign in to comment.