Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support private registry options in asyncapi generate fromTemplate command #1091

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,11 @@ FLAGS
--debug Enable more specific errors in the console
--force-write Force writing of the generated files to given directory even if it is a git repo with
unstaged files or not empty dir (defaults to false)
--map-base-url=<value> Maps all schema references from base url to local folder
--map-base-url=<value> Maps all schema references from base url to local folder
--registry-url=<value> Specifies URL of private registry for fetching templates&dependencies
--registry-auth=<value> The registry username and password encoded with base64, formatted as username:password
--registry-token=<value> The npm registry authentication token


DESCRIPTION
Generates whatever you want using templates compatible with AsyncAPI Generator.
Expand Down
30 changes: 27 additions & 3 deletions src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ interface IMapBaseUrlToFlag {
interface ParsedFlags {
params: Record<string, string>,
disableHooks: Record<string, string>,
mapBaseUrlToFolder: IMapBaseUrlToFlag
mapBaseUrlToFolder: IMapBaseUrlToFlag,
registryUrl: string
}

const templatesNotSupportingV3: Record<string, string> = {
Expand Down Expand Up @@ -100,6 +101,16 @@ export default class Template extends Command {
'map-base-url': Flags.string({
description: 'Maps all schema references from base url to local folder'
}),
'registry-url': Flags.string({
default: 'https://registry.npmjs.org',
description: 'Specifies the URL of the private registry for fetching templates and dependencies'
}),
'registry-auth': Flags.string({
description: 'The registry username and password encoded with base64, formatted as username:password'
}),
'registry-token': Flags.string({
description: 'The npm registry authentication token, that can be passed instead of base64 encoded username and password'
Comment on lines +108 to +112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it will make more sense if we take registryUsername and registryPassword as input from the user

Suggested change
'registry-auth': Flags.string({
description: 'The registry username and password encoded with base64, formatted as username:password'
}),
'registry-token': Flags.string({
description: 'The npm registry authentication token, that can be passed instead of base64 encoded username and password'
'registry-username': Flags.string({
description: 'The username for the private registry.',
}),
'registry-password': Flags.string({
description: 'The password for the private registry.',
}),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that was also my initial concern I raised under the issue. Though in the example described here we are directly providing the base64 encoded value, wouldn't it make sense to do the same here too?

})
};

static args = [
Expand All @@ -115,7 +126,7 @@ export default class Template extends Command {
const asyncapi = args['asyncapi'];
const template = args['template'];
const output = flags.output || process.cwd();
const parsedFlags = this.parseFlags(flags['disable-hook'], flags['param'], flags['map-base-url']);
const parsedFlags = this.parseFlags(flags['disable-hook'], flags['param'], flags['map-base-url'], flags['registry-url']);
const options = {
forceWrite: flags['force-write'],
install: flags.install,
Expand All @@ -124,6 +135,9 @@ export default class Template extends Command {
noOverwriteGlobs: flags['no-overwrite'],
mapBaseUrlToFolder: parsedFlags.mapBaseUrlToFolder,
disabledHooks: parsedFlags.disableHooks,
registryUrl: parsedFlags.registryUrl,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default URL of registryUrl should be https://registry.npmjs.org

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

registryAuth: flags['registry-auth'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registry auth should be change with base 64 value as mentioned in this
You can do this in something like this

const registryAuth = registryUsername && registryPassword
    ? Buffer.from(`${registryUsername}:${registryPassword}`).toString('base64')
    : undefined;

registryToken: flags['registry-token']
};
const asyncapiInput = (await load(asyncapi)) || (await load());

Expand All @@ -149,11 +163,12 @@ export default class Template extends Command {
}
}

private parseFlags(disableHooks?: string[], params?: string[], mapBaseUrl?: string): ParsedFlags {
private parseFlags(disableHooks?: string[], params?: string[], mapBaseUrl?: string, registryUrl?: string): ParsedFlags {
return {
params: this.paramParser(params),
disableHooks: this.disableHooksParser(disableHooks),
mapBaseUrlToFolder: this.mapBaseURLParser(mapBaseUrl),
registryUrl: this.registryURLParser(registryUrl),
} as ParsedFlags;
}

Expand Down Expand Up @@ -204,6 +219,15 @@ export default class Template extends Command {
return mapBaseURLToFolder;
}

private registryURLParser(input?: string) {
if (!input) { return; }
const isURL = /^https?:/;
if (!isURL.test(input.toLowerCase())) {
throw new Error('Invalid --registry-url flag. The param requires a valid http/https url.');
}
return input;
}

private async generate(asyncapi: string | undefined, template: string, output: string, options: any, genOption: any) {
let specification: Specification;
try {
Expand Down
Loading