smartshell/ts/smartshell.wrap.ts

132 lines
3.3 KiB
TypeScript
Raw Normal View History

2017-03-08 15:51:02 +00:00
import * as plugins from './smartshell.plugins'
2017-03-10 19:14:40 +00:00
// interfaces
import { ChildProcess } from 'child_process'
import { Deferred } from 'smartq'
2017-03-11 12:46:08 +00:00
/**
* interface for ExecResult
*/
2017-03-08 15:51:02 +00:00
export interface IExecResult {
exitCode: number,
stdout: string
}
2017-03-11 12:46:08 +00:00
/**
* interface for streaming ExecResult
*/
2017-03-10 19:14:40 +00:00
export interface IExecResultStreaming {
childProcess: ChildProcess,
finalPromise: Promise<IExecResult>
}
2017-07-18 13:17:05 +00:00
/**
* import path
*/
let importPath = (stringArg): string => {
if (process.env.SMARTSHELL_PATH) {
let commandResult = `PATH=${process.env.SMARTSHELL_PATH} && ${stringArg}`
// console.log(commandResult)
return commandResult
} else {
return stringArg
}
}
2017-03-10 19:14:40 +00:00
/**
* executes a given command async
* @param commandStringArg
*/
2017-03-08 15:51:02 +00:00
export let exec = (commandStringArg: string): Promise<IExecResult> => {
let done = plugins.smartq.defer<IExecResult>()
2017-07-18 13:17:05 +00:00
plugins.shelljs.exec(importPath(commandStringArg), { async: true }, (code, stdout, stderr) => {
if (stderr) {
console.log('StdErr found:')
console.log(stderr)
done.reject(stderr)
return
}
2017-03-08 15:51:02 +00:00
done.resolve({
exitCode: code,
stdout: stdout
})
})
return done.promise
}
2017-03-10 19:14:40 +00:00
/**
* executes a given command async and silent
* @param commandStringArg
*/
2017-03-08 15:51:02 +00:00
export let execSilent = (commandStringArg: string) => {
let done = plugins.smartq.defer<IExecResult>()
2017-07-18 13:17:05 +00:00
plugins.shelljs.exec(importPath(commandStringArg), { async: true, silent: true }, (code, stdout, stderr) => {
if (stderr && stderr !== '') {
console.log('StdErr found:')
console.log(stderr)
done.reject(stderr)
return
}
2017-03-08 15:51:02 +00:00
done.resolve({
exitCode: code,
stdout: stdout
})
})
return done.promise
}
2017-03-10 19:14:40 +00:00
/**
* executes a command and allws you to stream output
*/
2017-06-27 09:18:37 +00:00
export let execStreaming = (commandStringArg: string, silentArg: boolean = false) => {
2017-03-10 19:14:40 +00:00
let childProcessEnded = plugins.smartq.defer<IExecResult>()
2017-07-18 13:17:05 +00:00
let execChildProcess = plugins.shelljs.exec(importPath(commandStringArg), {async: true, silent: silentArg}, (code, stdout, stderr) => {
2017-03-10 19:14:40 +00:00
childProcessEnded.resolve({
exitCode: code,
stdout: stdout
})
})
return {
childProcess: execChildProcess,
finalPromise: childProcessEnded.promise
}
}
2017-03-11 12:46:08 +00:00
2017-06-27 09:18:37 +00:00
export let execStreamingSilent = (commandStringArg: string) => {
return execStreaming(commandStringArg, true)
}
2017-06-22 16:17:45 +00:00
/**
* executes a command and returns promise that will be fullfilled once an putput line matches RegexArg
* @param commandStringArg
2017-06-27 08:28:11 +00:00
* @param regexArg
2017-06-22 16:17:45 +00:00
*/
2017-06-27 09:18:37 +00:00
export let execAndWaitForLine = (commandStringArg: string, regexArg: RegExp, silentArg: boolean = false) => {
2017-06-22 16:17:45 +00:00
let done = plugins.smartq.defer()
2017-06-27 09:18:37 +00:00
let execStreamingResult = execStreaming(commandStringArg, silentArg)
2017-06-27 08:28:11 +00:00
execStreamingResult.childProcess.stdout.on('data', (stdOutChunk: string) => {
2017-06-22 16:17:45 +00:00
if (regexArg.test(stdOutChunk)) {
done.resolve()
}
})
return done.promise
}
2017-06-27 09:18:37 +00:00
export let execAndWaitForLineSilent = (commandStringArg: string, regexArg: RegExp) => {
execAndWaitForLine(commandStringArg, regexArg, true)
}
2017-03-11 12:46:08 +00:00
/**
* get a path
*/
export let which = (cmd: string): Promise<string> => {
2017-07-16 14:19:48 +00:00
let done = plugins.smartq.defer<string>()
2017-03-11 12:46:08 +00:00
plugins.which(cmd, (err, path: string) => {
if (err) {
done.reject(err)
}
done.resolve(path)
})
return done.promise
}