|
| 1 | +import { Args } from '@oclif/core'; |
| 2 | +import { BaseGeneratorCommand } from '@cli/internal/base/BaseGeneratorCommand'; |
| 3 | +// eslint-disable-next-line |
| 4 | +// @ts-ignore |
| 5 | +import { listBakedInTemplates } from 'generator-v2'; |
| 6 | +import { intro, note } from '@clack/prompts'; |
| 7 | +import { inverse, yellow } from 'picocolors'; |
| 8 | +import { clientsFlags } from '@cli/internal/flags/generate/clients.flags'; |
| 9 | +import { parseGeneratorFlags } from '@utils/generate/flags'; |
| 10 | +import { promptForLanguage } from '@utils/generate/prompts'; |
| 11 | +import { availableLanguages, AvailableLanguageType, getDefaultLanguage } from '@models/generate/ClientLanguages'; |
| 12 | +import { GeneratorError } from '@errors/generator-error'; |
| 13 | + |
| 14 | +export default class Client extends BaseGeneratorCommand { |
| 15 | + static description = `Generates clients baked-in AsyncAPI Generator. Available for: ${availableLanguages.join(', ')}. If some language is not supported or you want to improve existing client, join us at https://github.com/asyncapi/generator`; |
| 16 | + |
| 17 | + static examples = [ |
| 18 | + 'asyncapi generate client javascript asyncapi.yaml --param version=1.0.0 singleFile=true --output ./docs --force-write' |
| 19 | + ]; |
| 20 | + |
| 21 | + static readonly flags = { |
| 22 | + ...clientsFlags(), |
| 23 | + ...BaseGeneratorCommand.flags |
| 24 | + }; |
| 25 | + |
| 26 | + static args = { |
| 27 | + language: Args.string({ description: `The language you want the client generated for. Available target languages: ${availableLanguages.join(', ')}`, required: true }), |
| 28 | + ...BaseGeneratorCommand.args |
| 29 | + }; |
| 30 | + |
| 31 | + async run() { |
| 32 | + const { args, flags } = await this.parse(Client); // NOSONAR |
| 33 | + const interactive = !flags['no-interactive']; |
| 34 | + let asyncapi = args['asyncapi'] ?? ''; |
| 35 | + let language = args['language'] as AvailableLanguageType; |
| 36 | + let output = flags.output as string; |
| 37 | + const { proxyPort, proxyHost } = flags; |
| 38 | + |
| 39 | + if (interactive) { |
| 40 | + intro(inverse('Client generation with AsyncAPI Generator')); |
| 41 | + note(yellow('This feature is in the experimental phase. Please provide feedback at: https://github.com/asyncapi/generator/issues')); |
| 42 | + |
| 43 | + const parsedArgs = await this.parseArgs(args, output); |
| 44 | + asyncapi = parsedArgs.asyncapi; |
| 45 | + language = parsedArgs.language as AvailableLanguageType; |
| 46 | + output = parsedArgs.output; |
| 47 | + } |
| 48 | + |
| 49 | + const template = this.getTemplateName(language); |
| 50 | + |
| 51 | + const parsedFlags = parseGeneratorFlags( |
| 52 | + flags['disable-hook'], |
| 53 | + flags['param'], |
| 54 | + flags['map-base-url'], |
| 55 | + flags['registry-url'], |
| 56 | + flags['registry-auth'], |
| 57 | + flags['registry-token'] |
| 58 | + ); |
| 59 | + |
| 60 | + const options = await this.buildGeneratorOptions(flags, parsedFlags); |
| 61 | + |
| 62 | + // Apply proxy configuration using base class method |
| 63 | + asyncapi = this.applyProxyConfiguration(asyncapi, proxyHost, proxyPort); |
| 64 | + |
| 65 | + const asyncapiInput = await this.loadAsyncAPIInput(asyncapi); |
| 66 | + |
| 67 | + this.specFile = asyncapiInput; |
| 68 | + this.metricsMetadata.language = language; |
| 69 | + |
| 70 | + const watchTemplate = flags['watch']; |
| 71 | + const genOption = this.buildGenOption(flags, parsedFlags); |
| 72 | + |
| 73 | + // Use GeneratorService with new generator (v2) for client generation |
| 74 | + const specification = await this.loadSpecificationSafely(asyncapi); |
| 75 | + const result = await this.generatorService.generateUsingNewGenerator( |
| 76 | + specification, |
| 77 | + template, |
| 78 | + output, |
| 79 | + options as any, // GeneratorService expects different options interface |
| 80 | + genOption, |
| 81 | + ); |
| 82 | + |
| 83 | + if (!result.success) { |
| 84 | + throw new GeneratorError(new Error(result.error)); |
| 85 | + } |
| 86 | + |
| 87 | + this.log(result.data?.logs?.join('\n')); |
| 88 | + |
| 89 | + if (watchTemplate) { |
| 90 | + await this.handleWatchMode(asyncapi, template, output, options, genOption, interactive); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private async parseArgs(args: Record<string, any>, output?: string): Promise<{ asyncapi: string; language: string; output: string; }> { |
| 95 | + // Use base class method for common args |
| 96 | + const commonArgs = await this.parseCommonArgs(args, output); |
| 97 | + |
| 98 | + let language = args['language'] as AvailableLanguageType; |
| 99 | + |
| 100 | + if (!language) { |
| 101 | + const defaultLanguage = getDefaultLanguage(); |
| 102 | + language = await promptForLanguage(defaultLanguage) as AvailableLanguageType; |
| 103 | + } |
| 104 | + |
| 105 | + this.handleCancellation(language); |
| 106 | + |
| 107 | + return { |
| 108 | + asyncapi: commonArgs.asyncapi, |
| 109 | + language, |
| 110 | + output: commonArgs.output |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + private getTemplateName(language: AvailableLanguageType): string { |
| 115 | + const template = listBakedInTemplates({ type: 'client' }).find((template: any) => { |
| 116 | + return template.target === language; |
| 117 | + })?.name; |
| 118 | + |
| 119 | + if (!template) { |
| 120 | + this.log(`❌ Client generation for "${language}" is not yet available.`); |
| 121 | + this.log(`✅ Available languages: ${availableLanguages.join(', ')}`); |
| 122 | + this.log('🙏 Help us create the missing one. Start discussion at: https://github.com/asyncapi/generator/issues.'); |
| 123 | + this.exit(1); |
| 124 | + } |
| 125 | + |
| 126 | + return template; |
| 127 | + } |
| 128 | +} |
0 commit comments