Skip to content

Commit

Permalink
refactor: generate function refactored to reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
swastiksuvam55 committed Oct 8, 2023
1 parent 6d42118 commit bd023ef
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 5 deletions.
104 changes: 103 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class Generator {
* @return {Promise}
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
async generate(asyncapiDocument, parseOptions = {}) {
/* async generate(asyncapiDocument, parseOptions = {}) {
const isAlreadyParsedDocument = isAsyncAPIDocument(asyncapiDocument);
const isParsableCompatible = asyncapiDocument && typeof asyncapiDocument === 'string';
if (!isAlreadyParsedDocument && !isParsableCompatible) {
Expand Down Expand Up @@ -207,6 +207,108 @@ class Generator {
await this.generateDirectoryStructure(this.asyncapi);
await this.launchHook('generate:after');
}
} */

// Main method for generating code based on AsyncAPI document
async generate(asyncapiDocument, parseOptions = {}) {
this.validateAsyncAPIDocument(asyncapiDocument);
this.setupOutput();
this.setLogLevel();

const templateInfo = await this.installAndSetupTemplate();
await this.configureTemplateWorkflow(parseOptions);
await this.handleEntrypoint();
await this.executeAfterHook();
}

// Validate the AsyncAPI document
validateAsyncAPIDocument(asyncapiDocument) {
const isAlreadyParsedDocument = isAsyncAPIDocument(asyncapiDocument);
const isParsableCompatible = asyncapiDocument && typeof asyncapiDocument === 'string';

if (!isAlreadyParsedDocument && !isParsableCompatible) {
throw new Error('Parameter "asyncapiDocument" must be a non-empty string or an already parsed AsyncAPI document.');
}

this.asyncapi = this.originalAsyncAPI = asyncapiDocument;
}

// Setup the output based on the configured options
setupOutput() {
if (this.output === 'fs') {
this.setupFSOutput();
} else if (this.output === 'string' && this.entrypoint === undefined) {
throw new Error('Parameter entrypoint is required when using output = "string"');
}
}

// Setup file system output
async setupFSOutput() {
// Create directory if not exists
xfs.mkdirpSync(this.targetDir);

// Verify target directory if forceWrite is not enabled
if (!this.forceWrite) {
await this.verifyTargetDir(this.targetDir);
}
}

// Set log level based on debug option
setLogLevel() {
if (this.debug) log.setLevel('debug');
}

// Install and setup the template
async installAndSetupTemplate() {
const { name: templatePkgName, path: templatePkgPath } = await this.installTemplate(this.install);

this.templateDir = templatePkgPath;
this.templateName = templatePkgName;
this.templateContentDir = path.resolve(this.templateDir, TEMPLATE_CONTENT_DIRNAME);

await this.loadTemplateConfig();

return { templatePkgName, templatePkgPath };
}

// Configure the template workflow
async configureTemplateWorkflow(parseOptions) {
// Parse input and validate template configuration
await this.parseInput(this.asyncapi, parseOptions);
validateTemplateConfig(this.templateConfig, this.templateParams, this.asyncapi);
await this.configureTemplate();

if (!isReactTemplate(this.templateConfig)) {
await registerFilters(this.nunjucks, this.templateConfig, this.templateDir, FILTERS_DIRNAME);
}

await registerHooks(this.hooks, this.templateConfig, this.templateDir, HOOKS_DIRNAME);
await this.launchHook('generate:before');
}

// Handle the entrypoint logic
async handleEntrypoint() {
if (this.entrypoint) {
const entrypointPath = path.resolve(this.templateContentDir, this.entrypoint);

if (!(await exists(entrypointPath))) {
throw new Error(`Template entrypoint "${entrypointPath}" couldn't be found.`);
}

if (this.output === 'fs') {
await this.generateFile(this.asyncapi, path.basename(entrypointPath), path.dirname(entrypointPath));
await this.launchHook('generate:after');
} else if (this.output === 'string') {
return this.renderFile(this.asyncapi, entrypointPath);
}
} else {
await this.generateDirectoryStructure(this.asyncapi);
}
}

// Execute the after-hook
async executeAfterHook() {
await this.launchHook('generate:after');
}

/**
Expand Down
11 changes: 7 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bd023ef

Please sign in to comment.