diff --git a/src/Environment.ts b/src/Environment.ts index a489dc87f..271b553a6 100644 --- a/src/Environment.ts +++ b/src/Environment.ts @@ -2,6 +2,7 @@ import {delimiter} from "path"; import {executeCommandWithShellConfig} from "./PTY"; import {clone} from "lodash"; import {homeDirectory} from "./utils/Common"; +import * as Path from "path"; const env: Dictionary = {}; export async function loadEnvironment(): Promise { @@ -48,8 +49,8 @@ export class Environment { return key in this.storage; } - get path(): string { - return this.get("PATH"); + get path(): EnvironmentPath { + return new EnvironmentPath(this); } cdpath(pwd: string): string[] { @@ -68,3 +69,40 @@ export class Environment { this.set("PWD", value); } } + +export class EnvironmentPath { + constructor(private environment: Environment) { + } + + append(path: string) { + if (!this.has(path)) { + this.environment.set("PATH", this.raw + Path.delimiter + path); + } + } + + prepend(path: string) { + if (!this.has(path)) { + this.environment.set("PATH", path + Path.delimiter + this.raw); + } + } + + get split() { + return this.raw.split(Path.delimiter); + } + + remove(toRemove: string) { + this.removeWhere(existing => existing !== toRemove); + } + + removeWhere(remover: (existing: string) => boolean) { + this.environment.set("PATH", this.split.filter(remover).join(Path.delimiter)); + } + + private has(path: string) { + return this.split.includes(path); + } + + private get raw() { + return this.environment.get("PATH"); + } +} diff --git a/src/main/Menu.ts b/src/main/Menu.ts index 4f09fc40b..b81ba8549 100644 --- a/src/main/Menu.ts +++ b/src/main/Menu.ts @@ -98,8 +98,7 @@ export const menu = { }, ]; - let menu = Menu.buildFromTemplate(template); - Menu.setApplicationMenu(menu); + Menu.setApplicationMenu(Menu.buildFromTemplate(template)); } else { const template = [ { @@ -141,8 +140,7 @@ export const menu = { }, ]; - let menu = Menu.buildFromTemplate(template); - browserWindow.setMenu(menu); + browserWindow.setMenu(Menu.buildFromTemplate(template)); } }, }; diff --git a/src/plugins/NVM.ts b/src/plugins/NVM.ts index 10d405ff1..721476b31 100644 --- a/src/plugins/NVM.ts +++ b/src/plugins/NVM.ts @@ -10,12 +10,9 @@ PluginManager.registerEnvironmentObserver({ if (await exists(rcPath)) { const version = (await readFile(rcPath)).trim(); - const newPath = Path.join(homeDirectory, ".nvm", "versions", "node", version, "bin") + Path.delimiter + session.environment.path; - - session.environment.set("PATH", newPath); + session.environment.path.prepend(Path.join(homeDirectory, ".nvm", "versions", "node", version, "bin")); } else { - const path = session.environment.path.split(Path.delimiter).filter(path => !path.includes(".nvm")).join(Path.delimiter); - session.environment.setMany({PATH: path}); + session.environment.path.removeWhere(path => !path.includes(".nvm")); } }, }); diff --git a/src/plugins/RVM.ts b/src/plugins/RVM.ts index fc9100471..78d017e50 100644 --- a/src/plugins/RVM.ts +++ b/src/plugins/RVM.ts @@ -48,16 +48,15 @@ PluginManager.registerEnvironmentObserver({ const rubyVersion = await getRubyVersion(session.directory); const gemSetName = await getGemSetName(session.directory); const gemPaths = getGemSetPaths(rubyVersion, gemSetName); - const path = binPaths(rubyVersion, gemSetName) + Path.delimiter + session.environment.path; + session.environment.path.prepend(binPaths(rubyVersion, gemSetName)); session.environment.setMany({ - PATH: path, GEM_PATH: gemPaths.join(Path.delimiter), GEM_HOME: gemPaths[0], }); } else { - const path = session.environment.path.split(Path.delimiter).filter(path => !path.includes(".rvm")).join(Path.delimiter); - session.environment.setMany({PATH: path, GEM_HOME: "", GEM_PATH: ""}); + session.environment.path.removeWhere(path => !path.includes(".rvm")); + session.environment.setMany({GEM_HOME: "", GEM_PATH: ""}); } }, }); diff --git a/src/utils/Common.ts b/src/utils/Common.ts index 63c9aa34e..db9873b9f 100644 --- a/src/utils/Common.ts +++ b/src/utils/Common.ts @@ -5,6 +5,7 @@ import * as e from "./../Enums"; import * as _ from "lodash"; import * as fs from "fs"; import {KeyCode} from "./../Enums"; +import {EnvironmentPath} from "../Environment"; interface FSExtraWalkObject { path: string; @@ -160,12 +161,12 @@ export function baseName(path: string): string { export const {executablesInPaths} = new class { private executables: Array = []; - executablesInPaths = async (paths: string): Promise => { + executablesInPaths = async (path: EnvironmentPath): Promise => { if (this.executables.length) { return this.executables; } - const validPaths = await filterAsync(paths.split(Path.delimiter), isDirectory); + const validPaths = await filterAsync(path.split, isDirectory); const allFiles: string[][] = await Promise.all(validPaths.map(filesIn)); return _.uniq(_.flatten(allFiles)); diff --git a/src/views/3_JobComponent.tsx b/src/views/3_JobComponent.tsx index 1bbec60a5..0ebc77b0d 100644 --- a/src/views/3_JobComponent.tsx +++ b/src/views/3_JobComponent.tsx @@ -33,7 +33,7 @@ export class JobComponent extends React.Component implements KeyDo componentDidMount() { this.props.job .on("data", () => this.forceUpdate()) - .on("status", () => this.forceUpdate()) + .on("status", () => this.forceUpdate()); } componentDidUpdate() {