Skip to content

Commit

Permalink
Reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-rr committed Feb 5, 2024
1 parent 8d227f3 commit e6c25fa
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,12 @@ export default class Optimize extends Command {
this.optimizations = flags.optimization as Optimizations[];
this.outputMethod = flags.output as Outputs;

this.metricsMetadata.optimized = true;

if (report.moveToComponents?.length) {
this.metricsMetadata.optimization_moveToComponents = true;
}
if (report.removeComponents?.length) {
this.metricsMetadata.optimization_removeComponents = true;
}
if (report.reuseComponents?.length) {
this.metricsMetadata.optimization_reuseComponents = true;
}

if (!(report.moveToComponents?.length || report.removeComponents?.length || report.reuseComponents?.length)) {
this.log(`No optimization has been applied since ${this.specFile.getFilePath() ?? this.specFile.getFileURL()} looks optimized!`);
if (report.moveToComponents?.length || report.removeComponents?.length || report.reuseComponents?.length) {
this.metricsMetadata.optimized = true;
await this.collectMetricsData(report);
} else {
this.metricsMetadata.optimized = false;
this.log(`No optimization has been applied since ${this.specFile.getFilePath() ?? this.specFile.getFileURL()} looks optimized!`);
return;
}

Expand Down Expand Up @@ -202,4 +193,22 @@ export default class Optimize extends Command {
}]);
this.outputMethod = outputRes.output;
}

private async collectMetricsData(report: Report) {
try {
if (report.moveToComponents?.length) {
this.metricsMetadata.optimization_moveToComponents = true;
}
if (report.removeComponents?.length) {
this.metricsMetadata.optimization_removeComponents = true;
}
if (report.reuseComponents?.length) {
this.metricsMetadata.optimization_reuseComponents = true;
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}
}

0 comments on commit e6c25fa

Please sign in to comment.