From 81c75907328e444ae68722befecbc2b485d146ce Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Tue, 21 Apr 2020 19:49:23 +0300 Subject: [PATCH] Cache terminall shell env (#274) Signed-off-by: Jari Kolehmainen --- src/main/node-shell-session.ts | 2 +- src/main/shell-session.ts | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/node-shell-session.ts b/src/main/node-shell-session.ts index 2593db6f99..99a1867967 100644 --- a/src/main/node-shell-session.ts +++ b/src/main/node-shell-session.ts @@ -30,7 +30,7 @@ export class NodeShellSession extends ShellSession { } args = ["exec", "-i", "-t", "-n", "kube-system", this.podId, "--", "sh", "-c", "((clear && bash) || (clear && ash) || (clear && sh))"] - const shellEnv = this.getShellEnv() + const shellEnv = await this.getCachedShellEnv() this.shellProcess = pty.spawn(shell, args, { cols: 80, cwd: this.cwd() || shellEnv["HOME"], diff --git a/src/main/shell-session.ts b/src/main/shell-session.ts index 0257382cb1..270a8aa82f 100644 --- a/src/main/shell-session.ts +++ b/src/main/shell-session.ts @@ -10,6 +10,8 @@ import { Cluster, ClusterPreferences } from "./cluster" import { helmCli } from "./helm-cli" export class ShellSession extends EventEmitter { + static shellEnv: any + protected websocket: WebSocket protected shellProcess: pty.IPty protected kubeconfigPath: string @@ -30,8 +32,7 @@ export class ShellSession extends EventEmitter { public async open() { this.kubectlBinDir = await this.kubectl.binDir() this.helmBinDir = helmCli.getBinaryDir() - await helmCli.binaryPath() - const env = this.getShellEnv() + const env = await this.getCachedShellEnv() const shell = env.PTYSHELL const args = await this.getShellArgs(shell) this.shellProcess = pty.spawn(shell, args, { @@ -75,8 +76,25 @@ export class ShellSession extends EventEmitter { } } - protected getShellEnv() { - const env = JSON.parse(JSON.stringify(shellEnv.sync())) + protected async getCachedShellEnv() { + let env: any + if (!ShellSession.shellEnv) { + env = await this.getShellEnv() + ShellSession.shellEnv = env + } else { + env = ShellSession.shellEnv + + // refresh env in the background + this.getShellEnv().then((shellEnv: any) => { + ShellSession.shellEnv = shellEnv + }) + } + + return env + } + + protected async getShellEnv() { + const env = JSON.parse(JSON.stringify(await shellEnv())) const pathStr = [this.kubectlBinDir, this.helmBinDir, process.env.PATH].join(path.delimiter) if(process.platform === "win32") {