From e6c25faede3fcf89b5be0bdf7295150e67de2423 Mon Sep 17 00:00:00 2001 From: Pedro Ramos Date: Mon, 5 Feb 2024 19:49:00 +0100 Subject: [PATCH] Reduce cognitive complexity --- src/commands/optimize.ts | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/commands/optimize.ts b/src/commands/optimize.ts index 2056e45c6fb..dd81bb3cfc2 100644 --- a/src/commands/optimize.ts +++ b/src/commands/optimize.ts @@ -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; } @@ -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}`); + } + } + } }