diff --git a/lib/generator.js b/lib/generator.js index 05ff5643f..28d60061d 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -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; @@ -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. @@ -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 * @@ -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)