From ece10895893508fc1ee337b222f5552a881254b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <git@minelli.swiss>
Date: Tue, 25 Mar 2025 19:30:56 +0100
Subject: [PATCH] Add setting for interactive mode by default

---
 NodeApp/src/commander/CommanderApp.ts | 16 ++++++++++++++--
 NodeApp/src/config/Config.ts          | 24 +++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/NodeApp/src/commander/CommanderApp.ts b/NodeApp/src/commander/CommanderApp.ts
index 685da94..9c46506 100644
--- a/NodeApp/src/commander/CommanderApp.ts
+++ b/NodeApp/src/commander/CommanderApp.ts
@@ -46,7 +46,6 @@ class CommanderApp {
                                sortSubcommands  : true
                            })
             .option('-H, --host <string>', 'override the Dojo API endpoint', ClientsSharedConfig.apiURL)
-            .option('-I, --interactive', 'show interactive interface when available', Config.interactiveMode)
             .addOption(new Option('--debug').hideHelp())
             .hook('preAction', (_thisCommand: Command, actionCommand: Command) => {
                 if ( this.hasToExecuteHook(actionCommand) ) {
@@ -59,12 +58,25 @@ class CommanderApp {
                 }
             });
 
+        const interactiveModeOption = new Option('-I, --interactive', 'show interactive interface when available');
+        const noInteractiveModeOption = new Option('-N, --no_interactive', 'hide interactive interface when available').conflicts('interactive'); // WARNING: Due to a bug in commander.js, the conflicts method doesn't work as expected if the command is named no-interactive
+        if ( Config.interactiveMode ) {
+            interactiveModeOption.hideHelp();
+        } else {
+            noInteractiveModeOption.hideHelp();
+        }
+        this.program.addOption(interactiveModeOption).addOption(noInteractiveModeOption);
+
         this.program.on('option:host', () => {
             ClientsSharedConfig.apiURL = this.program.opts().host;
         });
 
+        this.program.on('option:no_interactive', () => {
+            Config.interactiveMode = false;
+        });
+
         this.program.on('option:interactive', () => {
-            Config.interactiveMode = this.program.opts().interactive;
+            Config.interactiveMode = true;
         });
 
         this.program.on('option:debug', () => {
diff --git a/NodeApp/src/config/Config.ts b/NodeApp/src/config/Config.ts
index ec06f03..0d4a441 100644
--- a/NodeApp/src/config/Config.ts
+++ b/NodeApp/src/config/Config.ts
@@ -1,8 +1,12 @@
 import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig';
 import GitlabManager       from '../managers/GitlabManager';
+import ConfigFiles         from './ConfigFiles';
+import inquirer            from 'inquirer';
 
 
 class Config {
+    readonly INTERACTIVE_MODE_CONFIG_KEY = 'interactiveMode';
+
     public gitlabManager!: GitlabManager;
 
     public versionUpdateInformationPeriodHours!: number;
@@ -67,7 +71,25 @@ class Config {
             neededFiles: JSON.parse(getEnvVar('EXERCISE_NEEDED_FILES', '[]'))
         };
 
-        this.interactiveMode = getEnvVar('INTERACTIVE_MODE', 'false') === 'true';
+        const interactiveMode: boolean | null = ConfigFiles.stateConfigFile.getParam(this.INTERACTIVE_MODE_CONFIG_KEY) as boolean | null;
+        this.interactiveMode = interactiveMode == null ? await this.askInteractiveMode() : interactiveMode;
+    }
+
+    setInteractiveMode(state: boolean) {
+        ConfigFiles.stateConfigFile.setParam(this.INTERACTIVE_MODE_CONFIG_KEY, state);
+    }
+
+    async askInteractiveMode(): Promise<boolean> {
+        const state: boolean = (await inquirer.prompt({
+                                                          name   : 'state',
+                                                          message: 'An interactive mode is available. Do you want to enable it by default ?',
+                                                          type   : 'confirm',
+                                                          default: true
+                                                      })).state;
+
+        this.setInteractiveMode(state);
+
+        return state;
     }
 }
 
-- 
GitLab