smartshell/ts/smartshell.classes.shellenv.ts

104 lines
2.8 KiB
TypeScript
Raw Permalink Normal View History

2018-11-26 16:55:15 +00:00
export type TExecutor = 'sh' | 'bash';
export interface IShellEnvContructorOptions {
executor: TExecutor;
sourceFilePaths?: string[];
pathDirectories?: string[];
}
export class ShellEnv {
executor: TExecutor;
sourceFileArray: string[] = [];
pathDirArray: string[] = [];
2018-07-30 14:08:14 +00:00
/**
* constructor for the shellenv
*/
constructor(optionsArg: IShellEnvContructorOptions) {
this.executor = optionsArg.executor;
// add sourcefiles
if (optionsArg.sourceFilePaths) {
2018-11-26 16:55:15 +00:00
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
*/
2021-07-26 19:24:13 +00:00
private _setPath(commandStringArg: string): string {
let commandResult = commandStringArg;
2018-12-13 15:50:32 +00:00
let commandPaths: string[] = [];
commandPaths = commandPaths.concat(process.env.PATH.split(':'));
if (process.env.SMARTSHELL_PATH) {
2018-12-13 15:50:32 +00:00
commandPaths = commandPaths.concat(process.env.SMARTSHELL_PATH.split(':'));
}
2018-12-13 15:50:32 +00:00
// lets filter for unwanted paths
// Windows WSL
2021-07-26 19:24:13 +00:00
commandPaths = commandPaths.filter((commandPathArg) => {
2018-12-13 15:50:32 +00:00
const filterResult =
!commandPathArg.startsWith('/mnt/c/') &&
!commandPathArg.startsWith('Files/1E') &&
!commandPathArg.includes(' ');
if (!filterResult) {
// console.log(`${commandPathArg} will be filtered!`);
}
return filterResult;
});
commandResult = `PATH=${commandPaths.join(':')} && ${commandStringArg}`;
return commandResult;
2018-07-30 14:08:14 +00:00
}
/**
* add files that are going to be sourced when running a command
2018-07-30 14:08:14 +00:00
* @param sourceFilePathsArray
*/
addSourceFiles(sourceFilePathsArray: string[]) {
for (let sourceFilePath of sourceFilePathsArray) {
this.sourceFileArray.push(sourceFilePath);
}
}
/**
* cleans the source files array
*/
cleanSourceFiles() {
this.sourceFileArray = [];
}
2019-05-19 20:41:20 +00:00
public createEnvExecString(commandArg: string): string {
2018-11-26 16:55:15 +00:00
let commandResult = '';
2018-12-13 15:50:32 +00:00
let sourceString = '';
2019-05-19 20:41:20 +00:00
// deal with sourcestring
for (const sourceFilePath of this.sourceFileArray) {
sourceString = sourceString + `source ${sourceFilePath} && `;
}
2021-11-26 14:17:52 +00:00
// deal with available path
2019-05-19 20:41:20 +00:00
let pathString = 'PATH=$PATH';
for (const pathDir of this.pathDirArray) {
pathString += `:${pathDir}`;
}
pathString += ` && `;
2018-12-13 15:50:32 +00:00
switch (this.executor) {
case 'bash':
2019-05-19 20:41:20 +00:00
commandResult = `bash -c '${pathString}${sourceString}${commandArg}'`;
2018-12-13 15:50:32 +00:00
break;
case 'sh':
2019-05-19 20:41:20 +00:00
commandResult = `${pathString}${sourceString}${commandArg}`;
2018-12-13 15:50:32 +00:00
break;
}
commandResult = this._setPath(commandResult);
return commandResult;
}
2018-07-30 14:08:14 +00:00
}