Skip to content

Commit

Permalink
changed the logic for updating the progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
ItshMoh committed Oct 21, 2024
1 parent 2e78e9e commit 192aad4
Showing 1 changed file with 54 additions and 67 deletions.
121 changes: 54 additions & 67 deletions src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,103 +257,90 @@ 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;
try {
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);
console.log('\n');
const progressBar = new cliProgress.SingleBar({
format: colors.cyan('{bar}') + ' | {percentage}% | ETA: {eta}s',
const progressBar = interactive ? new cliProgress.SingleBar({
format: colors.cyan('{bar}') + ' | {percentage}% | {status}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});

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

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

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 AsyncAPIGenerator(template, output || path.resolve(os.tmpdir(), 'asyncapi-generator'), options);
progressBar?.update(50, { status: 'Generating files...' });

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

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

} catch (err: any) {
if (interactive) {
progressBar.stop();
progressBar?.stop();
if (err instanceof ValidationError) {
return this.error(
new ValidationError({
type: 'invalid-file',
filepath: asyncapi,
}),
{ exit: 1 },
);
}
throw new GeneratorError(err);
}
console.log('\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',
const progressBar = interactive ? new cliProgress.SingleBar({
format: colors.cyan('{bar}') + ' | {percentage}% | {status}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});

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

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

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

} catch (err: any) {
if (interactive) {
progressBar.stop();
progressBar?.stop();
if (err instanceof ValidationError) {
return this.error(
new ValidationError({
type: 'invalid-file',
filepath: asyncapi,
}),
{ exit: 1 },
);
}
throw new GeneratorError(err);
}
console.log('\n');

console.log(`${yellow('Check out your shiny new generated files at ') + magenta(output) + yellow('.')}\n`);
}

Expand Down

0 comments on commit 192aad4

Please sign in to comment.