Skip to content

Commit

Permalink
chore: support cli to fetch private templates (#1399)
Browse files Browse the repository at this point in the history
Co-authored-by: asyncapi-bot <[email protected]>
  • Loading branch information
AayushSaini101 and asyncapi-bot authored May 13, 2024
1 parent 8db8aa1 commit 5c99587
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ FLAGS
unstaged files or not empty dir (defaults to false)
--map-base-url=<value> Maps all schema references from base url to local folder
--no-interactive Disable interactive mode and run with the provided flags.
--registry-url Specifies the URL of the private registry for fetching templates and dependencies
--registry-auth The registry username and password encoded with base64, formatted as username:password
--registry-token The npm registry authentication token, that can be passed instead of base64 encoded username and password
DESCRIPTION
Generates whatever you want using templates compatible with AsyncAPI Generator.
Expand Down
36 changes: 35 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 34 additions & 3 deletions src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Parser } from '@asyncapi/parser';
import type { Example } from '@oclif/core/lib/interfaces';
import { intro, isCancel, spinner, text } from '@clack/prompts';
import { inverse, yellow, magenta, green, red } from 'picocolors';
import fetch from 'node-fetch';

interface IMapBaseUrlToFlag {
url: string,
Expand Down Expand Up @@ -100,8 +101,17 @@ 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'
})
};

static args = [
{ name: 'asyncapi', description: '- Local path, url or context-name pointing to AsyncAPI file' },
{ name: 'template', description: '- Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template' },
Expand All @@ -124,7 +134,7 @@ export default class Template extends Command {
output = parsedArgs.output;
}

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'],flags['registry.auth'],flags['registry.token']);
const options = {
forceWrite: flags['force-write'],
install: flags.install,
Expand All @@ -133,6 +143,11 @@ export default class Template extends Command {
noOverwriteGlobs: flags['no-overwrite'],
mapBaseUrlToFolder: parsedFlags.mapBaseUrlToFolder,
disabledHooks: parsedFlags.disableHooks,
registry: {
url: flags['registry-url'],
auth: flags['registry-auth'],
token: flags['registry-token']
}
};
const asyncapiInput = (await load(asyncapi)) || (await load());

Expand Down Expand Up @@ -211,14 +226,30 @@ export default class Template extends Command {
return { asyncapi, template, output };
}

private parseFlags(disableHooks?: string[], params?: string[], mapBaseUrl?: string): ParsedFlags {
private parseFlags(disableHooks?: string[], params?: string[], mapBaseUrl?: string, registryUrl?: string, registryAuth?:string, registryToken?:string): ParsedFlags {
return {
params: this.paramParser(params),
disableHooks: this.disableHooksParser(disableHooks),
mapBaseUrlToFolder: this.mapBaseURLParser(mapBaseUrl),
registryURLValidation: this.registryURLParser(registryUrl),
registryAuthentication: this.registryValidation(registryUrl, registryAuth, registryToken)

} as ParsedFlags;
}

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.');
}
}
private async registryValidation(registryUrl?:string, registryAuth?:string, registryToken?:string) {
const response= await fetch(registryUrl as string);
if (response.status === 401&&!registryAuth&&!registryToken) {
throw new Error('Need to pass either registryAuth in username:password encoded in Base64 or need to pass registryToken');
}
}
private paramParser(inputs?: string[]) {
if (!inputs) { return {}; }
const params: Record<string, any> = {};
Expand Down

0 comments on commit 5c99587

Please sign in to comment.