Skip to content

Commit

Permalink
changed calls to use consistent then syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
lucsomers101 committed Feb 16, 2024
1 parent 1620269 commit 7721049
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 52 deletions.
65 changes: 35 additions & 30 deletions src/commands/generatePrototypeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string, string>(
[
['{{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}`)])
}
}
}
28 changes: 6 additions & 22 deletions src/utils/terminalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down

0 comments on commit 7721049

Please sign in to comment.