From 04e9ad7c9f53fe47ffb2ef6f2c0266e28c1c963b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Sun, 14 Jan 2024 22:21:37 +0100
Subject: [PATCH] ExerciseDockerCompose => Refactor code

---
 helpers/Dojo/ExerciseDockerCompose.ts | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/helpers/Dojo/ExerciseDockerCompose.ts b/helpers/Dojo/ExerciseDockerCompose.ts
index dafa4bd..9dd619c 100644
--- a/helpers/Dojo/ExerciseDockerCompose.ts
+++ b/helpers/Dojo/ExerciseDockerCompose.ts
@@ -44,6 +44,10 @@ class ExerciseDockerCompose {
         this.events.emit('logs', message, error, displayable, this.currentStep);
     }
 
+    private finished(success: boolean, code: number) {
+        this.events.emit('finished', success, code);
+    }
+
     private registerChildProcess(childProcess: ChildProcessWithoutNullStreams, resolve: (value: (number | PromiseLike<number>)) => void, reject: (reason?: unknown) => void, displayable: boolean) {
         childProcess.stdout.on('data', (data) => {
             this.log(data.toString(), false, displayable);
@@ -86,7 +90,7 @@ class ExerciseDockerCompose {
                     });
                 } catch ( error ) {
                     this.endStep(`Error while running the docker compose file`, true);
-                    this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_RUN_ERROR);
+                    this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_RUN_ERROR);
                     return;
                 }
                 this.endStep(`Docker Compose file run successfully`, false);
@@ -110,7 +114,7 @@ class ExerciseDockerCompose {
                     });
                 } catch ( error ) {
                     this.endStep(`Error while getting the linked services logs`, true);
-                    this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_LOGS_ERROR);
+                    this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_LOGS_ERROR);
                     return;
                 }
                 this.endStep(`Linked services logs acquired`, false);
@@ -135,10 +139,10 @@ class ExerciseDockerCompose {
                         });
                     } catch ( error ) {
                         this.endStep(`Error stop and remove containers`, true);
-                        this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_DOWN_ERROR);
+                        this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_DOWN_ERROR);
                         return;
                     }
-                    this.events.emit('endStep', this.currentStep, `Containers stopped and removed`, false);
+                    this.endStep(`Containers stopped and removed`, false);
                 }
             }
 
@@ -161,14 +165,14 @@ class ExerciseDockerCompose {
                         });
                     } catch ( error ) {
                         this.endStep(`Error while removing dangling images`, true);
-                        this.events.emit('finished', false, ExerciseCheckerError.DOCKER_COMPOSE_REMOVE_DANGLING_ERROR);
+                        this.finished(false, ExerciseCheckerError.DOCKER_COMPOSE_REMOVE_DANGLING_ERROR);
                         return;
                     }
-                    this.events.emit('endStep', this.currentStep, `Dangling images removed`, false);
+                    this.endStep(`Dangling images removed`, false);
                 }
             }
 
-            this.events.emit('finished', true, containerExitCode);
+            this.finished(true, containerExitCode);
         })();
     }
 }
-- 
GitLab