smartshell/ts/smartshell.classes.smartshell.ts

66 lines
1.6 KiB
TypeScript
Raw Normal View History

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