Skip to content

Commit

Permalink
Fix: testCases
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushRedHat committed Dec 19, 2023
1 parent 4af7923 commit 812205a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Examples outputs:

A template is an independent Node.js project unrelated to the `generator` repository. AsyncAPI templates are managed, released, and published separately. You can also create templates and manage templates on your own.

The generator uses the official [Arborist](https://www.npmjs.com/package/@npmcli/arborist) NPM library. (This means templates do not have to be published to package managers to use them.) Arborist helps the generator fetch the template's source code and use it for the generation process. By default, this library pulls data from the default NPM registry, which is https://registry.npmjs.org. You can also configure the generator to fetch templates that are private or hosted in different NPM registrie
The generator uses the official [Arborist](https://www.npmjs.com/package/@npmcli/arborist) NPM library. (This means templates do not have to be published to package managers to use them.) Arborist helps the generator fetch the template's source code and use it for the generation process. By default, this library pulls data from the default NPM registry, which is https://registry.npmjs.org. You can also configure the generator to fetch templates that are private or hosted in different NPM registry

You can store template projects on a local drive or as a `git` repository during the development process.

Expand Down
34 changes: 15 additions & 19 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const DEFAULT_TEMPLATES_DIR = path.resolve(ROOT_DIR, 'node_modules');

const TRANSPILED_TEMPLATE_LOCATION = '__transpiled';
const TEMPLATE_CONTENT_DIRNAME = 'template';
const GENERATOR_OPTIONS = ['debug', 'disabledHooks', 'entrypoint', 'forceWrite', 'install', 'noOverwriteGlobs', 'output', 'templateParams', 'mapBaseUrlToFolder'];
const GENERATOR_OPTIONS = ['debug', 'disabledHooks', 'entrypoint', 'forceWrite', 'install', 'noOverwriteGlobs', 'output', 'templateParams', 'mapBaseUrlToFolder','url','username','password','token','registry'];
const REGISTRY_OPTIONS = ['url', 'username', 'password', 'token'];

const logMessage = require('./logMessages');
Expand Down Expand Up @@ -94,25 +94,20 @@ 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 = {url: '', username: '', password: '', token: '' }} = {}) {
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 || []);
invalidRegOptions(invalidOptions)
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.');
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;
/** @type {RegistryOptions} Npm registry information. */
this.registry = registry;

/** @type {String} Name of the template to generate. */
this.templateName = templateName;
/** @type {String} Path to the directory where the files will be generated. */
Expand Down Expand Up @@ -156,19 +151,21 @@ class Generator {
});
});
}
/**
* Check if the Registery Options are valid or not.

/**
* Check if the Registry Options are valid or not.
*
* @private
* @public
* @param {Object} invalidRegOptions Invalid Registery 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 @@ -529,7 +526,7 @@ class Generator {
}

/**
* @private
* @public
* @param {Object} arbOptions ArbOptions to intialise the Registry details.
*/
initialiseArbOptions(arbOptions){
Expand Down Expand Up @@ -576,10 +573,9 @@ class Generator {
const arbOptions = {
path: ROOT_DIR,
};



if (this.registry) {
initialiseArbOptions(arbOptions)
this.initialiseArbOptions(arbOptions)
}
const arb = new Arborist(arbOptions)

Expand Down Expand Up @@ -1071,4 +1067,4 @@ class Generator {
Generator.DEFAULT_TEMPLATES_DIR = DEFAULT_TEMPLATES_DIR;
Generator.TRANSPILED_TEMPLATE_LOCATION = TRANSPILED_TEMPLATE_LOCATION;

module.exports = Generator;
module.exports = Generator;
6 changes: 4 additions & 2 deletions test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ 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 @@ -332,12 +334,12 @@ describe('Generator', () => {

it('works with a path to registry', async () => {
log.debug = jest.fn();
utils.__getTemplateDetails = undefined;
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', () => {
Expand All @@ -347,7 +349,7 @@ describe('Generator', () => {
privateKey: 'some.key'
}
});
expect(t).toThrow('There invalid parameters were specified to configure private registry: privateKey');
expect(t).toThrow('These options are not supported by the generator to configure private registry: privateKey');
});
});

Expand Down

0 comments on commit 812205a

Please sign in to comment.