From 0d6836138134be27e8939cc6da05018b5aa428ad Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Sat, 4 Aug 2018 16:43:02 +0200 Subject: [PATCH] fix(PATH): add proper PATH handling for child processes --- ts/smartshell.classes.shellenv.ts | 45 +++++++++++++++++++++++------ ts/smartshell.classes.smartshell.ts | 16 +--------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/ts/smartshell.classes.shellenv.ts b/ts/smartshell.classes.shellenv.ts index 504bb34..7a34387 100644 --- a/ts/smartshell.classes.shellenv.ts +++ b/ts/smartshell.classes.shellenv.ts @@ -1,22 +1,48 @@ -export type TExecutor = 'sh' | 'bash'; +export type TExecutor = "sh" | "bash"; export interface IShellEnvContructorOptions { executor: TExecutor; - sourceFilePaths: string[]; + sourceFilePaths?: string[]; + pathDirectories?: string[]; } export class ShellEnv { executor: TExecutor; sourceFileArray: string[] = []; + pathDirArray: string[] = []; /** * constructor for the shellenv */ constructor(optionsArg: IShellEnvContructorOptions) { this.executor = optionsArg.executor; - for (let sourceFilePath of optionsArg.sourceFilePaths) { - this.sourceFileArray.push(sourceFilePath); + + // add sourcefiles + if (optionsArg.sourceFilePaths) { + this.sourceFileArray = this.sourceFileArray.concat( + optionsArg.sourceFilePaths + ); } + + // add pathDirectories + if (optionsArg.pathDirectories) { + this.pathDirArray = this.pathDirArray.concat(optionsArg.pathDirectories); + } + } + + /** + * imports path into the shell from env if available and returns it with + */ + private _setPath(commandStringArg): string { + let commandResult = commandStringArg; + let commandPath = process.env.PATH; + if (process.env.SMARTSHELL_PATH) { + commandPath = `${commandPath}:${process.env.SMARTSHELL_PATH}`; + } + commandResult = `PATH=${ + commandPath + } && ${commandStringArg}`; + return commandResult; } /** @@ -37,14 +63,15 @@ export class ShellEnv { } createEnvExecString(commandArg): string { - if (this.executor === 'bash') { - let sourceString = ''; + let commandResult = '' + if (this.executor === "bash") { + let sourceString = ""; for (let sourceFilePath of this.sourceFileArray) { sourceString = sourceString + `source ${sourceFilePath} && `; } - return `bash -c '${sourceString} ${commandArg}'`; - } else { - return commandArg; + commandResult = `bash -c '${sourceString} ${commandArg}'`; } + commandResult = this._setPath(commandResult); + return commandResult; } } diff --git a/ts/smartshell.classes.smartshell.ts b/ts/smartshell.classes.smartshell.ts index 8092ed1..071ee5a 100644 --- a/ts/smartshell.classes.smartshell.ts +++ b/ts/smartshell.classes.smartshell.ts @@ -29,20 +29,7 @@ export class Smartshell { constructor(optionsArg: IShellEnvContructorOptions) { this.shellEnv = new ShellEnv(optionsArg); - } - - /** - * imports path into the shell from env if available and returns it with - */ - private _importEnvVarPath(stringArg): string { - if (process.env.SMARTSHELL_PATH) { - let commandResult = `PATH=${process.env.SMARTSHELL_PATH} && ${stringArg}`; - // console.log(commandResult) - return commandResult; - } else { - return stringArg; - } - } + }; /** * executes a given command async @@ -60,7 +47,6 @@ export class Smartshell { // build commandToExecute let commandToExecute = commandStringArg; commandToExecute = this.shellEnv.createEnvExecString(commandStringArg); - commandToExecute = this._importEnvVarPath(commandToExecute); const spawnlogInstance = new ShellLog(); const execChildProcess = cp.spawn(commandToExecute, [], { shell: true,