diff --git a/src/commands/generatePrototypeCommand.ts b/src/commands/generatePrototypeCommand.ts index b2476bb..1a8bb14 100644 --- a/src/commands/generatePrototypeCommand.ts +++ b/src/commands/generatePrototypeCommand.ts @@ -13,7 +13,7 @@ export class generatePrototypeCommand { const extensionPath: string = context.extensionPath; - const encodedZipContent = zipUtils.zipFolder(extensionPath); + const encodedZipContent:string|undefined = zipUtils.zipFolder(extensionPath); if(encodedZipContent === undefined) return; @@ -26,46 +26,51 @@ export class generatePrototypeCommand { tryKillPortForwardedProcessAndTerminal(this.portForwardTerminalPID); - vscode.workspace.fs.readFile(templateFileUri).then((data: Uint8Array) =>{ + vscode.workspace.fs.readFile(templateFileUri).then((data: Uint8Array) => replaceMarkers(data,encodedZipContent)); + + function tryKillPortForwardedProcessAndTerminal(terminalToKill : vscode.Terminal | undefined) + { + if(terminalToKill === undefined) + return; + + //get the processID from the terminal that needs to be killed + terminalToKill.processId.then((terminalToKillPID: number | undefined) => { + let killerTerminal = terminalUtils.RunCommandsInNewTerminal("Kill processes", + [`PID=$(ps -ef | grep 'kubectl port-forward' | grep -v grep | awk '{print $2}')`, + `kill $PID`, + (`kill -9 ${terminalToKillPID}`)], + false); + + //Get own terminal PID to kill it later + killerTerminal.processId.then((killerTerminalPID: number|undefined) => { + + //Kill self to cleanup + terminalUtils.RunCommandsInExistingTerminal(killerTerminal,[(`kill -9 ${killerTerminalPID}`)]); + }); + }); + } + + function replaceMarkers(data: Uint8Array, encodedZipContent:string) + { const newData: Uint8Array = fileUtils.replaceMarkers(data, new Map( [ ['{{zipFileContent}}', encodedZipContent], ['{{mainScript}}', encodedMainScript] ] )); - vscode.workspace.fs.writeFile(manifestFileUri, newData).then(() => { - - vscode.window.showInformationMessage(`Manifest saved, running in minikube.`); + + vscode.workspace.fs.writeFile(manifestFileUri, newData).then(runPrototypeCommand) + } - const deployment: string = 'prototype'; - const service: string = 'prototype'; + function runPrototypeCommand() + { + const deployment: string = 'prototype'; + const service: string = 'prototype'; - this.portForwardTerminalPID = terminalUtils.RunCommandsInNewTerminal("Run prototype in minikube", + generatePrototypeCommand.portForwardTerminalPID = terminalUtils.RunCommandsInNewTerminal("Run prototype in minikube", [`kubectl apply -f ${manifestFileUri.fsPath}`, `kubectl rollout status deployment/${deployment} --timeout=300s`, `kubectl port-forward svc/${service} -n default 8000:80`,]); - }); - }); - - async function tryKillPortForwardedProcessAndTerminal(terminalToKill : vscode.Terminal | undefined) - { - if(terminalToKill === undefined) - return; - - //get the processID from the terminal that needs to be killed - let terminalPID = await terminalToKill.processId.then(); - - //kill the portforward process and then kill the terminal that was hosting it. - let killerTerminal = terminalUtils.RunCommandsInNewTerminal("Kill processes", - [`PID=$(ps -ef | grep 'kubectl port-forward' | grep -v grep | awk '{print $2}')`, - `kill $PID`, - (`kill -9 ${terminalPID}`)]); - - //Get own terminal PID to kill it later - let killerTerminalPID = await killerTerminal.processId.then(); - - //Kill self to cleanup - terminalUtils.RunCommandsInExistingTerminal(killerTerminal,[(`kill -9 ${killerTerminalPID}`)]) } } } \ No newline at end of file diff --git a/src/utils/terminalUtils.ts b/src/utils/terminalUtils.ts index 6638136..03c4d5b 100644 --- a/src/utils/terminalUtils.ts +++ b/src/utils/terminalUtils.ts @@ -2,39 +2,23 @@ import vscode, { Terminal } from 'vscode'; import { fileUtils } from './fileUtils'; export class terminalUtils{ - static RunCommandInNewTerminal(terminalName : string, runAmpersandCommand : string, workingDir? : string[]) + static RunCommandsInNewTerminal(terminalName : string, runAmpersandCommands : string[], showTerminal: boolean = true, workingDir? : string[]) : Terminal { if(workingDir === undefined) - { workingDir = ['']; - } - - let terminal = vscode.window.createTerminal({name:terminalName,cwd:fileUtils.generateWorkspacePath(workingDir)}); - this.RunCommandsInExistingTerminal(terminal,[runAmpersandCommand]) - } - - static RunCommandsInNewTerminal(terminalName : string, runAmpersandCommands : string[], workingDir? : string[]) : Terminal - { - if(workingDir === undefined) - { - workingDir = ['']; - } - + let terminal = vscode.window.createTerminal({name:terminalName,cwd:fileUtils.generateWorkspacePath(workingDir)}); this.RunCommandsInExistingTerminal(terminal,runAmpersandCommands); + if(showTerminal) + terminal.show(); + return terminal; } - static RunCommandsInExistingTerminal(terminal : Terminal, runAmpersandCommands : string[], workingDir? : string[]) + static RunCommandsInExistingTerminal(terminal : Terminal, runAmpersandCommands : string[]) { - if(workingDir === undefined) - { - workingDir = ['']; - } - - terminal.show(); runAmpersandCommands.forEach(command => { terminal.sendText(command) });