diff --git a/src/commands/generate/fromTemplate.ts b/src/commands/generate/fromTemplate.ts index 82b1179f668..1b398e1d91d 100644 --- a/src/commands/generate/fromTemplate.ts +++ b/src/commands/generate/fromTemplate.ts @@ -65,6 +65,11 @@ export default class Template extends Command { description: 'Disable a specific hook type or hooks from a given hook type', multiple: true }), + 'no-interactive': Flags.boolean({ + description: 'Disable interactive mode and run with the provided flags.', + required: false, + default: false, + }), install: Flags.boolean({ char: 'i', default: false, @@ -108,9 +113,18 @@ export default class Template extends Command { async run() { const { args, flags } = await this.parse(Template); // NOSONAR + const interactive = !flags['no-interactive']; + + let { asyncapi, template, output } = args; + if (interactive) { + intro(inverse('AsyncAPI Generator')); + + const parsedArgs = await this.parseArgs(args, output); + asyncapi = parsedArgs.asyncapi; + template = parsedArgs.template; + output = parsedArgs.output; + } - intro(inverse('AsyncAPI Generator')); - const { asyncapi, template, output } = await this.parseArgs(args, flags.output); const parsedFlags = this.parseFlags(flags['disable-hook'], flags['param'], flags['map-base-url']); const options = { forceWrite: flags['force-write'], @@ -138,9 +152,9 @@ export default class Template extends Command { this.error(`${template} template does not support AsyncAPI v3 documents, please checkout ${v3IssueLink}`); } } - await this.generate(asyncapi, template, output, options, genOption); + await this.generate(asyncapi, template, output, options, genOption, interactive); if (watchTemplate) { - const watcherHandler = this.watcherHandler(asyncapi, template, output, options, genOption); + const watcherHandler = this.watcherHandler(asyncapi, template, output, options, genOption, interactive); await this.runWatchMode(asyncapi, template, output, watcherHandler); } } @@ -253,7 +267,7 @@ export default class Template extends Command { return mapBaseURLToFolder; } - private async generate(asyncapi: string | undefined, template: string, output: string, options: any, genOption: any) { + private async generate(asyncapi: string | undefined, template: string, output: string, options: any, genOption: any, interactive = true) { let specification: Specification; try { specification = await load(asyncapi); @@ -267,7 +281,7 @@ export default class Template extends Command { ); } const generator = new AsyncAPIGenerator(template, output || path.resolve(os.tmpdir(), 'asyncapi-generator'), options); - const s = spinner(); + const s = interactive ? spinner() : { start: () => null, stop: () => null}; s.start('Generation in progress. Keep calm and wait a bit'); try { await generator.generateFromString(specification.text(), genOption); @@ -318,7 +332,7 @@ export default class Template extends Command { }); } - private watcherHandler(asyncapi: string, template: string, output: string, options: Record, genOption: any): (changedFiles: Record) => Promise { + private watcherHandler(asyncapi: string, template: string, output: string, options: Record, genOption: any, interactive: boolean): (changedFiles: Record) => Promise { return async (changedFiles: Record): Promise => { console.clear(); console.log('[WATCHER] Change detected'); @@ -340,7 +354,7 @@ export default class Template extends Command { this.log(`\t${magenta(value.path)} was ${eventText}`); } try { - await this.generate(asyncapi, template, output, options, genOption); + await this.generate(asyncapi, template, output, options, genOption, interactive); } catch (err: any) { throw new GeneratorError(err); } diff --git a/src/commands/generate/models.ts b/src/commands/generate/models.ts index b0b2b4060e8..5cb69860676 100644 --- a/src/commands/generate/models.ts +++ b/src/commands/generate/models.ts @@ -38,6 +38,11 @@ export default class Models extends Command { static flags = { help: Flags.help({ char: 'h' }), + 'no-interactive': Flags.boolean({ + description: 'Disable interactive mode and run with the provided flags.', + required: false, + default: false, + }), output: Flags.string({ char: 'o', description: 'The output directory where the models should be written to. Omitting this flag will write the models to `stdout`.', @@ -169,11 +174,20 @@ export default class Models extends Command { /* eslint-disable sonarjs/cognitive-complexity */ async run() { - intro(inverse('AsyncAPI Generate Models')); - const { args, flags } = await this.parse(Models); + const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, tsMarshalling, tsExampleInstance, namespace, csharpAutoImplement, csharpArrayType, csharpNewtonsoft, csharpHashcode, csharpEqual, csharpSystemJson, packageName, javaIncludeComments, javaJackson, javaConstraints } = flags; - const { language, file, output } = await this.parseArgs(args, flags.output); + let { language, file, output } = args; + const interactive = !flags['no-interactive']; + + if (!interactive) { + intro(inverse('AsyncAPI Generate Models')); + + const parsedArgs = await this.parseArgs(args, output); + language = parsedArgs.language; + file = parsedArgs.file; + output = parsedArgs.output; + } const inputFile = (await load(file)) || (await load()); if (inputFile.isAsyncAPI3()) { diff --git a/test/integration/generate/fromTemplate.test.ts b/test/integration/generate/fromTemplate.test.ts index 746653e1422..f3d8cbd9517 100644 --- a/test/integration/generate/fromTemplate.test.ts +++ b/test/integration/generate/fromTemplate.test.ts @@ -4,10 +4,13 @@ import { test } from '@oclif/test'; import rimraf from 'rimraf'; import { expect } from '@oclif/test'; +const nonInteractive = '--no-interactive'; + const generalOptions = [ 'generate:fromTemplate', './test/fixtures/specification.yml', '@asyncapi/minimaltemplate', + nonInteractive, ]; const asyncapiv3 = './test/fixtures/specification-v3.yml'; @@ -38,11 +41,11 @@ describe('template', () => { 'generate:fromTemplate', asyncapiv3, '@asyncapi/minimaltemplate', - '--output=./test/docs/error', + nonInteractive, ]) .it('give error on disabled template', (ctx, done) => { expect(ctx.stderr).to.equal('Error: @asyncapi/minimaltemplate template does not support AsyncAPI v3 documents, please checkout some link\n'); - expect(ctx.stdout).to.equal('┌ AsyncAPI Generator\n'); + expect(ctx.stdout).to.equal(''); done(); }); }).timeout(200000);