Keep terminal open after debug ends

- Grab active terminal if its a daffodil-debugger named terminal, otherwise create a new one.
- Send command to run debugger to terminal.
- This allows for the terminal to stay open after the debugger exists.
- Allow for option to always create new terminal. This is mostly to support testing purposes.

Closes #614
diff --git a/src/daffodilDebuggerUtils.ts b/src/daffodilDebuggerUtils.ts
index 9eafff3..9e7bf68 100644
--- a/src/daffodilDebuggerUtils.ts
+++ b/src/daffodilDebuggerUtils.ts
@@ -49,20 +49,17 @@
 export const stopDebugger = (id: number | undefined = undefined) =>
   child_process.exec(osCheck(`taskkill /F /PID ${id}`, `kill -9 ${id}`))
 
-export const shellPath = (scriptName: string) =>
-  osCheck(scriptName, '/bin/bash')
-
-export const shellArgs = (scriptName: string, port: number) =>
-  osCheck(
-    ['--listenPort', `${port}`],
-    ['--login', '-c', `${scriptName} --listenPort ${port}`]
-  )
+export const shellArgs = (scriptName: string, port: number) => [
+  '--listenPort',
+  `${port}`,
+]
 
 export async function runDebugger(
   rootPath: string,
   daffodilDebugClasspath: string,
   filePath: string,
-  serverPort: number
+  serverPort: number,
+  createTerminal: boolean = false
 ): Promise<vscode.Terminal> {
   const dfdlVersion = daffodilVersion(filePath)
   const artifact = daffodilArtifact(dfdlVersion)
@@ -80,7 +77,7 @@
   return await runScript(
     scriptPath,
     artifact.scriptName,
-    shellPath(artifact.scriptName),
+    createTerminal,
     shellArgs(artifact.scriptName, serverPort),
     {
       DAFFODIL_DEBUG_CLASSPATH: daffodilDebugClasspath,
diff --git a/src/tests/suite/daffodilDebugger.test.ts b/src/tests/suite/daffodilDebugger.test.ts
index f935759..b9cd8bf 100644
--- a/src/tests/suite/daffodilDebugger.test.ts
+++ b/src/tests/suite/daffodilDebugger.test.ts
@@ -50,8 +50,12 @@
 
   before(async () => {
     await unzipFile(SCALA_PATH, PROJECT_ROOT)
-    debuggers.push(await runDebugger(PROJECT_ROOT, '', PACKAGE_PATH, 4711))
-    debuggers.push(await runDebugger(PROJECT_ROOT, '', PACKAGE_PATH, 4712))
+    debuggers.push(
+      await runDebugger(PROJECT_ROOT, '', PACKAGE_PATH, 4711, true)
+    )
+    debuggers.push(
+      await runDebugger(PROJECT_ROOT, '', PACKAGE_PATH, 4712, true)
+    )
   })
 
   after(async () => {
diff --git a/src/utils.ts b/src/utils.ts
index 50b9ee7..df9744b 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -26,6 +26,8 @@
 const defaultConf = vscode.workspace.getConfiguration()
 let currentConfig: vscode.DebugConfiguration
 
+const terminalName = 'daffodil-debugger'
+
 export const regexp = {
   comma: new RegExp(',', 'g'),
   slash: new RegExp('/', 'g'),
@@ -169,6 +171,7 @@
 export async function displayTerminalExitStatus(terminal: vscode.Terminal) {
   vscode.window.onDidCloseTerminal((t) => {
     if (t.name === terminal.name && t.processId === terminal.processId) {
+      t.show()
       vscode.window.showInformationMessage(
         `Terminal exited with status code: ${t.exitStatus?.code}`
       )
@@ -196,10 +199,37 @@
 
 export const delay = (ms: number) => new Promise((res) => setTimeout(res, ms))
 
+// Grab active terminal if available and it can run a new command, else create new one
+export const getTerminal = (
+  hideTerminal: boolean,
+  env:
+    | {
+        [key: string]: string | null | undefined
+      }
+    | undefined,
+  createTerminal: boolean
+) => {
+  if (!createTerminal) {
+    if (vscode.window.activeTerminal) {
+      const activeTerminal = vscode.window.activeTerminal
+
+      // check allows for shell name or full path to shell in terminal name
+      if (activeTerminal.name.includes(terminalName)) return activeTerminal
+    }
+  }
+
+  // If no good active terminal available create new one
+  return vscode.window.createTerminal({
+    name: terminalName,
+    hideFromUser: hideTerminal,
+    env: env,
+  })
+}
+
 export async function runScript(
   scriptPath: string,
   scriptName: string,
-  shellPath: string | null = null,
+  createTerminal: boolean,
   shellArgs: string[] = [],
   env:
     | {
@@ -225,15 +255,17 @@
     }
   }
 
-  // Start server in terminal based on scriptName
-  const terminal = vscode.window.createTerminal({
-    name: scriptName,
-    cwd: path.join(scriptPath, 'bin'),
-    hideFromUser: hideTerminal,
-    shellPath: shellPath !== null ? shellPath : scriptName,
-    shellArgs: shellArgs,
-    env: env,
-  })
+  const terminal = getTerminal(hideTerminal, env, createTerminal)
+
+  // Create debugger run command
+  const fullPathToScript = path
+    .join(scriptPath, 'bin', scriptName)
+    // fix pathing as emtpy space needs a \ before it to not cause errors
+    .replace(' ', '\\ ')
+  const debuggerRunCommand = `${fullPathToScript} ${shellArgs.join(' ')}`
+
+  // Send debugger run command to terminal, when exists terminal will stay open
+  terminal.sendText(debuggerRunCommand)
 
   if (!hideTerminal) {
     terminal.show()