Skip to content

Commit

Permalink
chore: add no-interactive flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurtu-gal committed Apr 15, 2024
1 parent b9d8446 commit f2ad75c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
30 changes: 22 additions & 8 deletions src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -318,7 +332,7 @@ export default class Template extends Command {
});
}

private watcherHandler(asyncapi: string, template: string, output: string, options: Record<string, any>, genOption: any): (changedFiles: Record<string, any>) => Promise<void> {
private watcherHandler(asyncapi: string, template: string, output: string, options: Record<string, any>, genOption: any, interactive: boolean): (changedFiles: Record<string, any>) => Promise<void> {
return async (changedFiles: Record<string, any>): Promise<void> => {
console.clear();
console.log('[WATCHER] Change detected');
Expand All @@ -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);
}
Expand Down
20 changes: 17 additions & 3 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.',
Expand Down Expand Up @@ -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()) {
Expand Down
7 changes: 5 additions & 2 deletions test/integration/generate/fromTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f2ad75c

Please sign in to comment.