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

chore: support cli to fetch private templates #1399

Merged
merged 16 commits into from
May 13, 2024
Merged
3 changes: 3 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,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
39 changes: 36 additions & 3 deletions src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,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'
}),
' ': Flags.string({
Shurtu-gal marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -126,7 +135,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 @@ -135,6 +144,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 @@ -213,14 +227,33 @@ 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;
}

// Parsing the url
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.');
}
}
// No we need at least one parameter to be passed registryAuth and registryToken
Shurtu-gal marked this conversation as resolved.
Show resolved Hide resolved

private registryValidation(registryUrl?:string, registryAuth?:string, registryToken?:string) {
if (registryUrl!=='https://registry.npmjs.org'&&registryAuth&&!registryToken) {
Shurtu-gal marked this conversation as resolved.
Show resolved Hide resolved
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
Loading