Skip to content

Commit

Permalink
Fix lint issue and refractor code
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushRedHat committed Dec 19, 2023
1 parent d4cbe2d commit 3659ddc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 43 deletions.
31 changes: 11 additions & 20 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -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. */
Expand All @@ -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.
Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down
42 changes: 19 additions & 23 deletions test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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';
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 3659ddc

Please sign in to comment.