Skip to content

Commit

Permalink
code updated to support new generator & html template
Browse files Browse the repository at this point in the history
  • Loading branch information
flamewow committed Jan 27, 2024
1 parent 6c0afef commit bf30465
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/asyncapi.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class AsyncApiModule {
templateOptions?: AsyncApiTemplateOptions,
) {
const generator = new AsyncapiGenerator(templateOptions);
return await generator.generate(contract).catch((err) => {
return generator.generate(contract).catch((err) => {
this.logger.error(err);
throw err;
});
Expand Down
2 changes: 1 addition & 1 deletion lib/interface/generator-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface GeneratorOptions {
templateConfig: Record<string, any>;
hooks: Record<string, any>;
templateParams: AsyncApiTemplateOptions;
generate: (document: any) => Promise<void>;
generate: (document: any, args?: any) => Promise<void>;
generateFromURL: (url: string) => Promise<void>;
generateFromFile: (path: string) => Promise<void>;
generateFromString: (yaml: string, args?: any) => Promise<string>;
Expand Down
16 changes: 11 additions & 5 deletions lib/services/asyncapi.generator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import Generator from '@asyncapi/generator';
import fs from 'fs/promises';
import jsyaml from 'js-yaml';
import os from 'os';
import * as os from 'os';
import { AsyncApiTemplateOptions, GeneratorOptions } from '../interface';

export class AsyncapiGenerator {
private readonly generator: GeneratorOptions;
private readonly tmpDir: string = os.tmpdir();
private readonly fileName: string = 'index.html';
private readonly fullFilePath = `${this.tmpDir}/${this.fileName}`;

constructor(readonly templateOptions?: AsyncApiTemplateOptions) {
this.generator = new Generator('@asyncapi/html-template', os.tmpdir(), {
this.generator = new Generator('@asyncapi/html-template', this.tmpDir, {
forceWrite: true,
entrypoint: 'index.html',
output: 'string',
entrypoint: 'index.html.js',
output: 'fs',
templateParams: {
singleFile: true,
...templateOptions,
Expand All @@ -20,10 +24,12 @@ export class AsyncapiGenerator {

public async generate(contract: any): Promise<string> {
const yaml = jsyaml.dump(contract);
return await this.generator.generateFromString(yaml, {
await this.generator.generate(yaml, {
resolve: {
file: false,
},
});

return fs.readFile(this.fullFilePath, 'utf8');
}
}

0 comments on commit bf30465

Please sign in to comment.