diff --git a/lib/generator.js b/lib/generator.js index 97e684685..92f835945 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -94,13 +94,13 @@ class Generator { * @param {String} [options.registry.token] Optional parameter to pass npm registry auth token */ -constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwriteGlobs, disabledHooks, output = 'fs', forceWrite = false, install = false, debug = false, mapBaseUrlToFolder = {}, registry = {}} = {}) { + constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwriteGlobs, disabledHooks, output = 'fs', forceWrite = false, install = false, debug = false, mapBaseUrlToFolder = {}, registry = {}} = {}) { 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?.registry) { - const invalidRegOptions = getInvalidOptions(REGISTRY_OPTIONS, options.registry || []); - this.isInvalidRegOptions(invalidRegOptions) + if (options && options.registry) { + const invalidRegOptions = getInvalidOptions(REGISTRY_OPTIONS, options.registry || []); + this.isInvalidRegOptions(invalidRegOptions); } if (!templateName) throw new Error('No template name has been specified.'); if (!entrypoint && !targetDir) throw new Error('No target directory has been specified.'); @@ -132,8 +132,6 @@ constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwr this.hooks = {}; /** @type {Object} Maps schema URL to folder. */ this.mapBaseUrlToFolder = mapBaseUrlToFolder; - - // Load template configuration /** @type {Object} The template parameters. The structure for this object is based on each individual template. */ @@ -155,16 +153,14 @@ constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwr /** * Check if the Registry Options are valid or not. * - * @public + * @private * @param {Object} invalidRegOptions Invalid Registry Options. * @return {boolean} */ - isInvalidRegOptions(invalidRegOptions){ + 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. @@ -317,11 +313,9 @@ constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwr 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'); } @@ -343,11 +337,9 @@ constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwr 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'); @@ -526,15 +518,14 @@ constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwr } /** - * @public + * @private * @param {Object} arbOptions ArbOptions to intialise the Registry details. */ - initialiseArbOptions(arbOptions){ + 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 @@ -575,9 +566,9 @@ constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwr }; if (this.registry) { - this.initialiseArbOptions(arbOptions) + this.initialiseArbOptions(arbOptions); } - const arb = new Arborist(arbOptions) + const arb = new Arborist(arbOptions); try { const installResult = await arb.reify({ diff --git a/test/generator.test.js b/test/generator.test.js index 1c21b28d8..db9dcf785 100644 --- a/test/generator.test.js +++ b/test/generator.test.js @@ -4,7 +4,6 @@ const path = require('path'); const Generator = require('../lib/generator'); const log = require('loglevel'); const unixify = require('unixify'); - const dummyYAML = fs.readFileSync(path.resolve(__dirname, './docs/dummy.yml'), 'utf8'); const logMessage = require('./../lib/logMessages.js'); @@ -316,8 +315,6 @@ describe('Generator', () => { }); describe('#generateFromURL', () => { - let utils; - it('calls fetch and generateFromString with the right params', async () => { const utils = require('../lib/utils'); const asyncapiURL = 'http://example.com/fake-asyncapi.yml'; @@ -331,26 +328,6 @@ describe('Generator', () => { expect(utils.fetchSpec.mock.calls[0][0]).toBe(asyncapiURL); expect(generateMock.mock.calls[0][0]).toBe('fake text'); }); - - it('works with a path to registry', async () => { - log.debug = jest.fn(); - const gen = new Generator('nameOfTestTemplate', __dirname, {debug: true, registry: {url: 'some.registry.com', username: 'user', password: 'password', token: 'token'}}); - await gen.installTemplate(); - setTimeout(() => { // This puts the call at the end of the Node.js event loop queue. - expect(arboristMock.reify).toHaveBeenCalledTimes(1); - }, 0); - - }); - - it('throws an error indicating an unexpected param was given for registry configuration', () => { - const t = () => new Generator('testTemplate', __dirname, { - registry: { - url: 'some.url.com', - privateKey: 'some.key' - } - }); - expect(t).toThrow('These options are not supported by the generator to configure private registry: privateKey'); - }); }); describe('#installTemplate', () => { @@ -437,6 +414,25 @@ describe('Generator', () => { expect(arboristMock.reify).toHaveBeenCalledTimes(1); }, 0); }); + + it('works with a path to registry', async () => { + log.debug = jest.fn(); + const gen = new Generator('nameOfTestTemplate', __dirname, {debug: true, registry: {url: 'some.registry.com', username: 'user', password: 'password', token: 'token'}}); + await gen.installTemplate(); + setTimeout(() => { // This puts the call at the end of the Node.js event loop queue. + expect(arboristMock.reify).toHaveBeenCalledTimes(1); + }); + }); + + it('throws an error indicating an unexpected param was given for registry configuration', () => { + const t = () => new Generator('testTemplate', __dirname, { + registry: { + url: 'some.url.com', + privateKey: 'some.key' + } + }); + expect(t).toThrow('These options are not supported by the generator to configure private registry: privateKey'); + }); }); describe('.getTemplateFile', () => {