Skip to content

Commit

Permalink
Added UI Changes to Optimize Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishalk91-4 committed Sep 23, 2024
1 parent 9b790c0 commit 3ebe407
Show file tree
Hide file tree
Showing 3 changed files with 11 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
9 changes: 4 additions & 5 deletions src/core/errors/validation-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ export class ValidationError extends Error {
this.buildError(error.err);
}
if (error.type === 'invalid-file') {
this.message = `There is no file or context with name "${error.filepath}".`;
this.message = `There is no file or context with the name "${error.filepath}". Please check the path and try again.`;
}
if (error.type === 'no-spec-found') {
this.message = 'Unable to perform validation. Specify what AsyncAPI file should be validated.\n\nThese are your options to specify in the CLI what AsyncAPI file should be used:\n- You can provide a path to the AsyncAPI file: asyncapi validate path/to/file/asyncapi.yml\n- You can also pass a saved context that points to your AsyncAPI file: asyncapi validate mycontext\n- In case you did not specify a context that you want to use, the CLI checks if there is a default context and uses it. To set default context run: asyncapi context use mycontext\n- In case you did not provide any reference to AsyncAPI file and there is no default context, the CLI detects if in your current working directory you have files like asyncapi.json, asyncapi.yaml, asyncapi.yml. Just rename your file accordingly.';
this.message = 'Unable to perform validation. Please specify which AsyncAPI file should be validated.';
}
this.name = 'ValidationError';
}

private buildError(err: any) {
const errorsInfo: Array<string> = [];

Expand All @@ -37,8 +36,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

0 comments on commit 3ebe407

Please sign in to comment.