Skip to content

Commit

Permalink
Resolve sonar code issue
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushRedHat committed Dec 14, 2023
1 parent b36c18e commit 4af7923
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ class Generator {
const options = arguments[arguments.length - 1];
const invalidOptions = getInvalidOptions(GENERATOR_OPTIONS, options || []);
if (invalidOptions.length) throw new Error(`These options are not supported by the generator: ${invalidOptions.join(', ')}`);
if (options && options.registry) {
if (options?.registry) {
const invalidRegOptions = getInvalidOptions(REGISTRY_OPTIONS, options.registry || []);
if (invalidRegOptions.length) throw new Error(`These options are not supported by the generator to configure private registry: ${invalidRegOptions.join(', ')}`);
invalidRegOptions(invalidOptions)
}
if (!templateName) throw new Error('No template name has been specified.');
if (!entrypoint && !targetDir) throw new Error('No target directory has been specified.');
if (!['fs', 'string'].includes(output)) throw new Error(`Invalid output type ${output}. Valid values are 'fs' and 'string'.`);

this.hooks = {};
/** @type {Object} Maps schema URL to folder. */
this.mapBaseUrlToFolder = mapBaseUrlToFolder;
Expand Down Expand Up @@ -156,6 +156,18 @@ class Generator {
});
});
}

/**
* Check if the Registery Options are valid or not.
*
* @private
* @param {Object} invalidRegOptions Invalid Registery Options.
* @return {boolean}
*/

isInvalidRegOptions(invalidRegOptions){
if (invalidRegOptions.length) throw new Error(`These options are not supported by the generator to configure private registry: ${invalidRegOptions.join(', ')}`);
}

/**
* Generates files from a given template and an AsyncAPIDocument object.
Expand Down Expand Up @@ -516,6 +528,17 @@ class Generator {
return await readFile(path.resolve(templatesDir, templateName, filePath), 'utf8');
}

/**
* @private
* @param {Object} arbOptions ArbOptions to intialise the Registry details.
*/
initialiseArbOptions(arbOptions){
if (this.registry.url) arbOptions.registry = this.registry.url;
if (this.registry.username) arbOptions.username = this.registry.username;
if (this.registry.password) arbOptions.password = this.registry.password;
if (this.registry.token) arbOptions.token = this.registry.token;

}
/**
* Downloads and installs a template and its dependencies
*
Expand Down Expand Up @@ -553,11 +576,10 @@ class Generator {
const arbOptions = {
path: ROOT_DIR,
};


if (this.registry) {
if (this.registry.url) arbOptions.registry = this.registry.url;
if (this.registry.username) arbOptions.username = this.registry.username;
if (this.registry.password) arbOptions.password = this.registry.password;
if (this.registry.token) arbOptions.token = this.registry.token;
initialiseArbOptions(arbOptions)
}
const arb = new Arborist(arbOptions)

Expand Down

0 comments on commit 4af7923

Please sign in to comment.