diff --git a/lib/helpers/generateTemplate.ts b/lib/helpers/generateTemplate.ts index b1d7701..46b5df7 100644 --- a/lib/helpers/generateTemplate.ts +++ b/lib/helpers/generateTemplate.ts @@ -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, @@ -59,7 +60,7 @@ export async function generateClientTemplate(options: { if (options.logger) { options.logger.info('Copied -> ' + file.name); } - } + } // Function to Determine new project files. const project = new Project({ @@ -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); diff --git a/lib/templates/backendInterface.handlebars b/lib/templates/backendInterface.handlebars new file mode 100644 index 0000000..01f4229 --- /dev/null +++ b/lib/templates/backendInterface.handlebars @@ -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}} + } +} \ No newline at end of file diff --git a/nopeconfig.json b/nopeconfig.json index 02ea428..72082ee 100644 --- a/nopeconfig.json +++ b/nopeconfig.json @@ -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": {