Adding a General Backend-Interface

This commit is contained in:
Martin Karkowski 2020-08-25 12:50:17 +02:00
parent 5612ef0460
commit f2e9f9b657
3 changed files with 47 additions and 6 deletions

View File

@ -11,7 +11,8 @@ import { createFile, createPath } from "./fileHelpers";
* @param options
*/
export async function generateClientTemplate(options: {
pathToTemplate: string,
pathToClientTemplate: string,
pathToInterfaceTemplate: string,
outputDir: string,
inputDir: string,
tsConfigFilePath: string,
@ -76,12 +77,12 @@ export async function generateClientTemplate(options: {
const result = analyzeFiles(sourceFiles);
// load the File.
const template = await readFile(options.pathToTemplate, {
const clientTemplate = await readFile(options.pathToClientTemplate, {
encoding: 'utf-8'
})
// Renderfuncting
const render = handlebars.compile(template);
const renderClient = handlebars.compile(clientTemplate);
// Generate the Files.
const files: {
@ -92,6 +93,7 @@ export async function generateClientTemplate(options: {
// Extract the Class uri
const classUri = item.classDecorator.decoratorSettings.exportsElementsToDispatcher ? item.classDecorator.decoratorSettings.exportsElementsToDispatcher.uri || item.className : item.className;
(item as any).orginalName = item.className;
item.className = item.className + 'ClientInterface';
item.methods = item.methods.map(m => {
@ -113,10 +115,28 @@ export async function generateClientTemplate(options: {
return {
name: item.className + '.ts',
content: render(item)
content: renderClient(item)
}
});
// load the File.
const interfaceTemplate = await readFile(options.pathToInterfaceTemplate, {
encoding: 'utf-8'
})
// Renderfuncting
const renderInterface = handlebars.compile(interfaceTemplate);
await createFile(
// Generate the Path.
join(options.outputDir, 'BackendInterface.ts'),
renderInterface({ classes: result })
);
if (options.logger) {
options.logger.info('Generated -> BackendInterface.ts');
}
for (const file of files) {
// Define the File Name:
const fileName = join(options.outputDir, 'clients', file.name);

View File

@ -0,0 +1,20 @@
// Automatic Genearted Backend-Interface
// To update run `npm run build:backend`
import { nopeDispatcher } from "./dispatcher/nopeDispatcher"
{{#each classes}}
import { {{className}} } from "./clients/{{className}}";
{{/each}}
export class BackendInterface {
{{#each classes}}
public {{orginalName}}: {{className}};
{{/each}}
constructor(protected _dispatcher: nopeDispatcher){
{{#each classes}}
this.{{orginalName}} = new {{className}}(_dispatcher);
{{/each}}
}
}

View File

@ -2,7 +2,8 @@
"dispatcher": {
"inputDir": "./test/*.ts",
"outputDir": "./backend-api",
"pathToTemplate": "./lib/templates/clientInterface.handlebars",
"pathToClientTemplate": "./lib/templates/clientInterface.handlebars",
"pathToInterfaceTemplate": "./lib/templates/backendInterface.handlebars",
"tsConfigFilePath": "./tsconfigBackend.json"
},
"openapi": {