2018-11-26 16:55:15 +00:00
|
|
|
export type TExecutor = 'sh' | 'bash';
|
2018-07-30 14:03:48 +00:00
|
|
|
|
|
|
|
export interface IShellEnvContructorOptions {
|
|
|
|
executor: TExecutor;
|
2018-08-04 14:43:02 +00:00
|
|
|
sourceFilePaths?: string[];
|
|
|
|
pathDirectories?: string[];
|
2018-07-30 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ShellEnv {
|
|
|
|
executor: TExecutor;
|
|
|
|
sourceFileArray: string[] = [];
|
2018-08-04 14:43:02 +00:00
|
|
|
pathDirArray: string[] = [];
|
2018-07-30 14:08:14 +00:00
|
|
|
|
2018-07-30 14:03:48 +00:00
|
|
|
/**
|
|
|
|
* constructor for the shellenv
|
|
|
|
*/
|
|
|
|
constructor(optionsArg: IShellEnvContructorOptions) {
|
|
|
|
this.executor = optionsArg.executor;
|
2018-08-04 14:43:02 +00:00
|
|
|
|
|
|
|
// add sourcefiles
|
|
|
|
if (optionsArg.sourceFilePaths) {
|
2018-11-26 16:55:15 +00:00
|
|
|
this.sourceFileArray = this.sourceFileArray.concat(optionsArg.sourceFilePaths);
|
2018-08-04 14:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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}`;
|
2018-07-30 14:03:48 +00:00
|
|
|
}
|
2018-11-26 16:55:15 +00:00
|
|
|
commandResult = `PATH=${commandPath} && ${commandStringArg}`;
|
2018-08-04 14:43:02 +00:00
|
|
|
return commandResult;
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|
2018-07-30 14:03:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* add files that are going to be sourced when running a command
|
2018-07-30 14:08:14 +00:00
|
|
|
* @param sourceFilePathsArray
|
2018-07-30 14:03:48 +00:00
|
|
|
*/
|
|
|
|
addSourceFiles(sourceFilePathsArray: string[]) {
|
|
|
|
for (let sourceFilePath of sourceFilePathsArray) {
|
|
|
|
this.sourceFileArray.push(sourceFilePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cleans the source files array
|
|
|
|
*/
|
|
|
|
cleanSourceFiles() {
|
|
|
|
this.sourceFileArray = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
createEnvExecString(commandArg): string {
|
2018-11-26 16:55:15 +00:00
|
|
|
let commandResult = '';
|
|
|
|
if (this.executor === 'bash') {
|
|
|
|
let sourceString = '';
|
2018-07-30 14:03:48 +00:00
|
|
|
for (let sourceFilePath of this.sourceFileArray) {
|
|
|
|
sourceString = sourceString + `source ${sourceFilePath} && `;
|
|
|
|
}
|
2018-08-04 14:43:02 +00:00
|
|
|
commandResult = `bash -c '${sourceString} ${commandArg}'`;
|
2018-07-30 14:03:48 +00:00
|
|
|
}
|
2018-08-04 14:43:02 +00:00
|
|
|
commandResult = this._setPath(commandResult);
|
|
|
|
return commandResult;
|
2018-07-30 14:03:48 +00:00
|
|
|
}
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|