2018-07-18 18:58:12 +00:00
|
|
|
import * as plugins from './smartshell.plugins';
|
|
|
|
import * as smartshellWrap from './smartshell.wrap';
|
2017-03-08 15:51:02 +00:00
|
|
|
|
2018-07-18 18:58:12 +00:00
|
|
|
export type TExecutor = 'sh' | 'bash';
|
2017-03-08 15:51:02 +00:00
|
|
|
|
|
|
|
export interface ISmartshellContructorOptions {
|
2018-07-18 18:58:12 +00:00
|
|
|
executor: TExecutor;
|
|
|
|
sourceFilePaths: string[];
|
2017-03-08 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Smartshell {
|
2018-07-18 18:58:12 +00:00
|
|
|
executor: TExecutor;
|
|
|
|
sourceFileArray: string[] = [];
|
|
|
|
constructor(optionsArg: ISmartshellContructorOptions) {
|
|
|
|
this.executor = optionsArg.executor;
|
2017-03-10 19:14:40 +00:00
|
|
|
for (let sourceFilePath of optionsArg.sourceFilePaths) {
|
2018-07-18 18:58:12 +00:00
|
|
|
this.sourceFileArray.push(sourceFilePath);
|
2017-03-08 15:51:02 +00:00
|
|
|
}
|
2017-03-10 19:14:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 18:58:12 +00:00
|
|
|
addSourceFiles(sourceFilePathsArray: string[]) {
|
2017-06-27 08:28:11 +00:00
|
|
|
for (let sourceFilePath of sourceFilePathsArray) {
|
2018-07-18 18:58:12 +00:00
|
|
|
this.sourceFileArray.push(sourceFilePath);
|
2017-03-10 19:14:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 18:58:12 +00:00
|
|
|
cleanSourceFiles() {
|
|
|
|
this.sourceFileArray = [];
|
2017-03-10 21:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* executes silently and returns IExecResult
|
2017-03-11 00:58:09 +00:00
|
|
|
* @param commandArg
|
2017-03-10 21:08:04 +00:00
|
|
|
*/
|
2018-07-18 18:58:12 +00:00
|
|
|
async execSilent(commandArg: string) {
|
|
|
|
let execCommand = this.createExecString(commandArg);
|
|
|
|
return await smartshellWrap.execSilent(execCommand);
|
2017-03-10 19:14:40 +00:00
|
|
|
}
|
2017-03-10 21:08:04 +00:00
|
|
|
|
2017-03-11 01:36:27 +00:00
|
|
|
/**
|
|
|
|
* executes and returns IExecResult
|
|
|
|
* @param commandArg
|
|
|
|
*/
|
2018-07-18 18:58:12 +00:00
|
|
|
async exec(commandArg: string) {
|
|
|
|
let execCommand = this.createExecString(commandArg);
|
|
|
|
return await smartshellWrap.exec(execCommand);
|
2017-03-11 01:36:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 21:08:04 +00:00
|
|
|
/**
|
|
|
|
* creates the final sourcing string
|
|
|
|
* @param commandArg
|
|
|
|
*/
|
2018-07-18 18:58:12 +00:00
|
|
|
private createExecString(commandArg): string {
|
2017-03-10 21:08:04 +00:00
|
|
|
if (this.executor === 'bash') {
|
2018-07-18 18:58:12 +00:00
|
|
|
let sourceString = '';
|
2017-03-10 21:08:04 +00:00
|
|
|
for (let sourceFilePath of this.sourceFileArray) {
|
2018-07-18 18:58:12 +00:00
|
|
|
sourceString = sourceString + `source ${sourceFilePath} && `;
|
2017-03-10 21:08:04 +00:00
|
|
|
}
|
2018-07-18 18:58:12 +00:00
|
|
|
return `bash -c '${sourceString} ${commandArg}'`;
|
2017-03-10 21:08:04 +00:00
|
|
|
} else {
|
2018-07-18 18:58:12 +00:00
|
|
|
return commandArg;
|
2017-03-10 21:08:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|