Skip to content

Commit

Permalink
chore: added ui/ux improvements in optimize commands (asyncapi#1526)
Browse files Browse the repository at this point in the history
* Added UI Changes to Optimize Command

* fix: failing tests

Signed-off-by: Vishal K <korada.vishal.phe22@itbhu.ac.in>

---------

Signed-off-by: Vishal K <korada.vishal.phe22@itbhu.ac.in>
Co-authored-by: souvik <souvikde.ns@gmail.com>
  • Loading branch information
Vishalk91-4 and Souvikns authored Oct 26, 2024
1 parent a9cb291 commit 4627ea5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions .asyncapi-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"current":"eight-context","store":{"seven-context":".","eight-context":"."}}
11 changes: 6 additions & 5 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class Optimize extends Command {
this.metricsMetadata.optimized = false;

if (!(report.moveDuplicatesToComponents?.length || report.removeComponents?.length || report.reuseComponents?.length)) {
this.log(`No optimization has been applied since ${this.specFile.getFilePath() ?? this.specFile.getFileURL()} looks optimized!`);
this.log(`🎉 Great news! Your file at ${this.specFile.getFilePath() ?? this.specFile.getFileURL()} is already optimized.`);
return;
}

Expand Down Expand Up @@ -120,21 +120,22 @@ export default class Optimize extends Command {

switch (this.outputMethod) {
case Outputs.TERMINAL:
this.log('📄 Here is your optimized AsyncAPI document:\n');
this.log(optimizedDocument);
break;
case Outputs.NEW_FILE:
await writeFile(newPath, optimizedDocument, { encoding: 'utf8' });
this.log(`Created file ${newPath}...`);
this.log(`✅ Success! Your optimized file has been created at ${chalk.blue({newPath})}.`);
break;
case Outputs.OVERWRITE:
await writeFile(specPath ?? 'asyncapi.yaml', optimizedDocument, { encoding: 'utf8' });
this.log(`Updated file ${specPath}...`);
this.log(`✅ Success! Your original file at ${specPath} has been updated.`);
break;
}
} catch (error) {
throw new ValidationError({
type: 'parser-error',
err: error
err: error,
});
}
}
Expand Down Expand Up @@ -219,7 +220,7 @@ export default class Optimize extends Command {
message: 'where do you want to save the result:',
type: 'list',
default: 'log to terminal',
choices: [{name: 'log to terminal',value: Outputs.TERMINAL}, {name: 'create new file', value: Outputs.NEW_FILE}, {name: 'update original', value: Outputs.OVERWRITE}]
choices: [{name: 'log to terminal',value: Outputs.TERMINAL}, {name: 'create new file', value: Outputs.NEW_FILE}, {name: 'update original file', value: Outputs.OVERWRITE}]
}]);
this.outputMethod = outputRes.output;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/errors/validation-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export class ValidationError extends Error {
const errorHasTitle = !!e.title;
const errorHasLocation = !!e.location;
/*
* All the conditions below are needed since validationErrors (from ParserError) come from Parser JS library,
* so we cannot assure that all the fields or properties are always provided in the error. There might be cases
* All the conditions below are needed since validationErrors (from ParserError) come from Parser JS library,
* so we cannot assure that all the fields or properties are always provided in the error. There might be cases
* that even title is not provided.
*/
if (errorHasTitle && errorHasLocation) {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/optimize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('optimize', () => {
.stdout()
.command(['optimize', optimizedFilePath])
.it('works when file path is passed', (ctx, done) => {
expect(ctx.stdout).to.contain(`No optimization has been applied since ${optimizedFilePath} looks optimized!`);
expect(ctx.stdout).to.contain(`🎉 Great news! Your file at ${optimizedFilePath} is already optimized.`);
expect(ctx.stderr).to.equal('');
done();
});
Expand All @@ -54,7 +54,7 @@ describe('optimize', () => {
.stdout()
.command(['optimize', 'http://localhost:8080/dummySpecWithoutSecurity.yml'])
.it('works when url is passed', (ctx, done) => {
expect(ctx.stdout).to.contain('No optimization has been applied since http://localhost:8080/dummySpecWithoutSecurity.yml looks optimized!');
expect(ctx.stdout).to.contain('🎉 Great news! Your file at http://localhost:8080/dummySpecWithoutSecurity.yml is already optimized.');
expect(ctx.stderr).to.equal('');
done();
});
Expand All @@ -75,7 +75,7 @@ describe('optimize', () => {
.stdout()
.command(['optimize'])
.it('converts from current context', (ctx, done) => {
expect(ctx.stdout).to.contain(`No optimization has been applied since ${path.resolve(__dirname, '../fixtures/specification.yml')} looks optimized!`);
expect(ctx.stdout).to.contain(`🎉 Great news! Your file at ${path.resolve(__dirname, '../fixtures/specification.yml')} is already optimized.`);
expect(ctx.stderr).to.equal('');
done();
});
Expand Down

0 comments on commit 4627ea5

Please sign in to comment.