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

chore: added ui/ux improvements in optimize commands #1526

Open
wants to merge 5 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
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
Loading