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

feat: add the flag for disabling optimizations #1447

Merged
merged 7 commits into from
Jul 31, 2024
Merged
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
28 changes: 27 additions & 1 deletion src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export enum Optimizations {
MOVE_ALL_TO_COMPONENTS='move-all-to-components',
}

export enum DisableOptimizations {
SCHEMA='schema'
}

export enum Outputs {
TERMINAL='terminal',
NEW_FILE='new-file',
Expand All @@ -27,13 +31,15 @@ export default class Optimize extends Command {
static description = 'optimize asyncapi specification file';
isInteractive = false;
selectedOptimizations?: Optimizations[];
disableOptimizations?: DisableOptimizations[];
outputMethod?: Outputs;

static examples = [
'asyncapi optimize ./asyncapi.yaml',
'asyncapi optimize ./asyncapi.yaml --no-tty',
'asyncapi optimize ./asyncapi.yaml --optimization=remove-components --optimization=reuse-components --optimization=move-all-to-components --no-tty',
'asyncapi optimize ./asyncapi.yaml --optimization=remove-components --output=terminal --no-tty',
'asyncapi optimize ./asyncapi.yaml --ignore=schema'
];

static flags = optimizeFlags();
Expand Down Expand Up @@ -74,6 +80,7 @@ export default class Optimize extends Command {
}
this.isInteractive = !flags['no-tty'];
this.selectedOptimizations = flags.optimization as Optimizations[];
this.disableOptimizations = flags.ignore as DisableOptimizations[];
this.outputMethod = flags.output as Outputs;
this.metricsMetadata.optimized = false;

Expand All @@ -93,7 +100,11 @@ export default class Optimize extends Command {
moveAllToComponents: this.selectedOptimizations.includes(Optimizations.MOVE_ALL_TO_COMPONENTS),
removeComponents: this.selectedOptimizations.includes(Optimizations.REMOVE_COMPONENTS),
reuseComponents: this.selectedOptimizations.includes(Optimizations.REUSE_COMPONENTS)
}, output: Output.YAML});
},
disableOptimizationFor: {
schema: this.disableOptimizations.includes(DisableOptimizations.SCHEMA)
},
output: Output.YAML});

this.collectMetricsData(report);

Expand Down Expand Up @@ -178,6 +189,13 @@ export default class Optimize extends Command {
this.showOptimizations(report.reuseComponents);
choices.push({name: 'reuse components', value: Optimizations.REUSE_COMPONENTS});
}

if (this.disableOptimizations?.includes(DisableOptimizations.SCHEMA)) {
choices.push({name: 'Do not ignore schema', value: DisableOptimizations.SCHEMA});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we use the same option as flag. I mean the name should be Ignore schemas. Also it should be checked by default if --ignore=schema is passed.

} else {
choices.push({name: 'Ignore schema', value: DisableOptimizations.SCHEMA});
}

const optimizationRes = await inquirer.prompt([{
name: 'optimization',
message: 'select the type of optimization that you want to apply:',
Expand All @@ -186,6 +204,14 @@ export default class Optimize extends Command {
choices
}]);

if (optimizationRes.optimization.includes('schema')) {
if (this.disableOptimizations?.includes(DisableOptimizations.SCHEMA)) {
this.disableOptimizations = this.disableOptimizations?.filter(opt => opt !== DisableOptimizations.SCHEMA);
} else {
this.disableOptimizations = [...(this.disableOptimizations || []), DisableOptimizations.SCHEMA];
}
}

this.selectedOptimizations = optimizationRes.optimization;

const outputRes = await inquirer.prompt([{
Expand Down
5 changes: 5 additions & 0 deletions src/core/flags/optimize.flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export enum Optimizations {
MOVE_ALL_TO_COMPONENTS='move-all-to-components',
}

export enum DisableOptimizations {
SCHEMA='schema',
}

export enum Outputs {
TERMINAL='terminal',
NEW_FILE='new-file',
Expand All @@ -17,6 +21,7 @@ export const optimizeFlags = () => {
return {
help: Flags.help({ char: 'h' }),
optimization: Flags.string({char: 'p', default: Object.values(Optimizations), options: Object.values(Optimizations), multiple: true, description: 'select the type of optimizations that you want to apply.'}),
ignore: Flags.string({char: 'i', default: [], options: Object.values(DisableOptimizations), multiple: true, description: 'list of components to be ignored from the optimization process'}),
output: Flags.string({char: 'o', default: Outputs.TERMINAL, options: Object.values(Outputs), description: 'select where you want the output.'}),
'no-tty': Flags.boolean({ description: 'do not use an interactive terminal', default: false }),
};
Expand Down
Loading