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

feat: added the progress bar for generate command. #1552

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
"@oclif/core": "^4.0.28",
"@smoya/asyncapi-adoption-metrics": "^2.4.9",
"@stoplight/spectral-cli": "6.9.0",
"ansi-colors": "^4.1.3",
"chalk": "^4.1.0",
"chokidar": "^3.5.2",
"cli-progress": "^3.12.0",
"fast-levenshtein": "^3.0.0",
"fs-extra": "^11.1.0",
"inquirer": "^8.2.0",
Expand Down
104 changes: 73 additions & 31 deletions src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { intro, isCancel, spinner, text } from '@clack/prompts';
import { inverse, yellow, magenta, green, red } from 'picocolors';
import fetch from 'node-fetch';
import { fromTemplateFlags } from '../../core/flags/generate/fromTemplate.flags';
import cliProgress from 'cli-progress';
import colors from 'ansi-colors';

interface IMapBaseUrlToFlag {
url: string,
Expand Down Expand Up @@ -255,34 +257,80 @@ export default class Template extends Command {

private async generate(asyncapi: string | undefined, template: string, output: string, options: any, genOption: any, interactive = true) {
let specification: Specification;

const progressBar = interactive ? new cliProgress.SingleBar({
format: colors.cyan('{bar}') + ' | {percentage}% | {status}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
}) : null;

try {
if (progressBar) {
console.log('\n');
progressBar.start(100, 0, { status: 'Loading specification...' });
}


specification = await load(asyncapi);
} catch (err: any) {
return this.error(
new ValidationError({
type: 'invalid-file',
filepath: asyncapi,
}),
{ exit: 1 },
);
}
const generator = new AsyncAPIGenerator(template, output || path.resolve(os.tmpdir(), 'asyncapi-generator'), options);
const s = interactive ? spinner() : { start: () => null, stop: (string: string) => console.log(string) };
s.start('Generation in progress. Keep calm and wait a bit');
try {

progressBar?.update(30, { status: 'Initializing generator...' });

const generator = new AsyncAPIGenerator(template, output || path.resolve(os.tmpdir(), 'asyncapi-generator'), options);
progressBar?.update(50, { status: 'Generating files...' });

await generator.generateFromString(specification.text(), { ...genOption, path: asyncapi });

progressBar?.update(100, { status: 'Generation complete!' });
progressBar?.stop();

} catch (err: any) {
s.stop('Generation failed');
progressBar?.stop();
if (err instanceof ValidationError) {
return this.error(
new ValidationError({
type: 'invalid-file',
filepath: asyncapi,
}),
{ exit: 1 },
);
}
throw new GeneratorError(err);
}
s.stop(`${yellow('Check out your shiny new generated files at ') + magenta(output) + yellow('.')}\n`);
}
console.log('\n');
console.log(`${yellow('Check out your shiny new generated files at ') + magenta(output) + yellow('.')}\n`);
}

private async generateUsingNewGenerator(asyncapi: string | undefined, template: string, output: string, options: any, genOption: any, interactive = true) {
let specification: Specification;
try {
specification = await load(asyncapi);
} catch (err: any) {
private async generateUsingNewGenerator(asyncapi: string | undefined, template: string, output: string, options: any, genOption: any, interactive = true) {
let specification: Specification;

const progressBar = interactive ? new cliProgress.SingleBar({
format: colors.cyan('{bar}') + ' | {percentage}% | {status}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
}) : null;

try {
if (progressBar) {
console.log('\n');
progressBar.start(100, 0, { status: 'Loading specification...' });
}

specification = await load(asyncapi);
progressBar?.update(30, { status: 'Initializing generator...' });

const generator = new AsyncAPINewGenerator(template, output || path.resolve(os.tmpdir(), 'asyncapi-generator'), options);
progressBar?.update(50, { status: 'Generating files...' });

await generator.generateFromString(specification.text(), { ...genOption, path: asyncapi });

progressBar?.update(100, { status: 'Generation complete!' });
progressBar?.stop();

} catch (err: any) {
progressBar?.stop();
if (err instanceof ValidationError) {
return this.error(
new ValidationError({
type: 'invalid-file',
Expand All @@ -291,17 +339,11 @@ export default class Template extends Command {
{ exit: 1 },
);
}
const generator = new AsyncAPINewGenerator(template, output || path.resolve(os.tmpdir(), 'asyncapi-generator'), options);
const s = interactive ? spinner() : { start: () => null, stop: (string: string) => console.log(string) };
s.start('Generation in progress. Keep calm and wait a bit');
try {
await generator.generateFromString(specification.text(), { ...genOption, path: asyncapi });
} catch (err: any) {
s.stop('Generation failed');
throw new GeneratorError(err);
}
s.stop(`${yellow('Check out your shiny new generated files at ') + magenta(output) + yellow('.')}\n`);
throw new GeneratorError(err);
}
console.log('\n');
console.log(`${yellow('Check out your shiny new generated files at ') + magenta(output) + yellow('.')}\n`);
}

private async runWatchMode(asyncapi: string | undefined, template: string, output: string, watchHandler: ReturnType<typeof this.watcherHandler>) {
const specification = await load(asyncapi);
Expand Down
Loading