smartshell/ts/smartshell.classes.smartshell.ts

65 lines
1.6 KiB
TypeScript
Raw Normal View History

import * as plugins from './smartshell.plugins';
import * as smartshellWrap from './smartshell.wrap';
2017-03-08 15:51:02 +00:00
export type TExecutor = 'sh' | 'bash';
2017-03-08 15:51:02 +00:00
export interface ISmartshellContructorOptions {
executor: TExecutor;
sourceFilePaths: string[];
2017-03-08 15:51:02 +00:00
}
export class Smartshell {
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) {
this.sourceFileArray.push(sourceFilePath);
2017-03-08 15:51:02 +00:00
}
2017-03-10 19:14:40 +00:00
}
addSourceFiles(sourceFilePathsArray: string[]) {
2017-06-27 08:28:11 +00:00
for (let sourceFilePath of sourceFilePathsArray) {
this.sourceFileArray.push(sourceFilePath);
2017-03-10 19:14:40 +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
*/
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
*/
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
*/
private createExecString(commandArg): string {
2017-03-10 21:08:04 +00:00
if (this.executor === 'bash') {
let sourceString = '';
2017-03-10 21:08:04 +00:00
for (let sourceFilePath of this.sourceFileArray) {
sourceString = sourceString + `source ${sourceFilePath} && `;
2017-03-10 21:08:04 +00:00
}
return `bash -c '${sourceString} ${commandArg}'`;
2017-03-10 21:08:04 +00:00
} else {
return commandArg;
2017-03-10 21:08:04 +00:00
}
}
}