Skip to content

Commit

Permalink
added the progress bar for generate command.
Browse files Browse the repository at this point in the history
  • Loading branch information
ItshMoh committed Oct 20, 2024
1 parent 9e7c5e6 commit 692ac31
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 47 deletions.
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
106 changes: 80 additions & 26 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 @@ -266,42 +268,94 @@ export default class Template extends Command {
{ 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');
console.log('\n');
const progressBar = new cliProgress.SingleBar({
format: colors.cyan('{bar}') + ' | {percentage}% | ETA: {eta}s',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});

if (interactive) {
progressBar.start(100, 0);
}

try {
let progress = 0;
const progressInterval = setInterval(() => {
if (progress < 95) {
progress += Math.random() * 5;
progressBar.update(progress);
}
}, 300);

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

clearInterval(progressInterval);
progressBar.update(100);
progressBar.stop();
} catch (err: any) {
s.stop('Generation failed');
if (interactive) {
progressBar.stop();
}
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) {
return this.error(
new ValidationError({
type: 'invalid-file',
filepath: asyncapi,
}),
{ exit: 1 },
);
}

const generator = new AsyncAPINewGenerator(template, output || path.resolve(os.tmpdir(), 'asyncapi-generator'), options);
console.log('\n');
const progressBar = new cliProgress.SingleBar({
format: colors.cyan('{bar}') + ' | {percentage}% | ETA: {eta}s',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});

if (interactive) {
progressBar.start(100, 0);
}

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) {
return this.error(
new ValidationError({
type: 'invalid-file',
filepath: asyncapi,
}),
{ 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);
try {
let progress = 0;
const progressInterval = setInterval(() => {
if (progress < 95) {
progress += Math.random() * 5;
progressBar.update(progress);
}
}, 300);

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

clearInterval(progressInterval);
progressBar.update(100);
progressBar.stop();
} catch (err: any) {
if (interactive) {
progressBar.stop();
}
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

0 comments on commit 692ac31

Please sign in to comment.