-
-
Notifications
You must be signed in to change notification settings - Fork 239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: generate function refactored to reduce cognitive complexity #1047
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,7 +160,7 @@ | |
* @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) { | ||
|
@@ -207,6 +207,108 @@ | |
await this.generateDirectoryStructure(this.asyncapi); | ||
await this.launchHook('generate:after'); | ||
} | ||
} */ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No new lines between the jsdoc and function 🙂 |
||
// Main method for generating code based on AsyncAPI document | ||
swastiksuvam55 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for this. |
||
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'); | ||
} | ||
|
||
/** | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove any commented out code 🙂