smartshell/ts/smartshell.wrap.ts

83 lines
1.9 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>
}
/**
* 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-03-10 19:14:40 +00:00
plugins.shelljs.exec(commandStringArg, { async: true }, (code, stdout, stderr) => {
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-03-10 19:14:40 +00:00
plugins.shelljs.exec(commandStringArg, { async: true, silent: true }, (code, stdout, stderr) => {
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
*/
export let execStreaming = (commandStringArg: string) => {
let childProcessEnded = plugins.smartq.defer<IExecResult>()
let execChildProcess = plugins.shelljs.exec(commandStringArg, {async: true, silent: true}, (code, stdout, stderr) => {
childProcessEnded.resolve({
exitCode: code,
stdout: stdout
})
})
return {
childProcess: execChildProcess,
finalPromise: childProcessEnded.promise
}
}
2017-03-11 12:46:08 +00:00
/**
* get a path
*/
export let which = (cmd: string): Promise<string> => {
let done = plugins.smartq.defer()
plugins.which(cmd, (err, path: string) => {
if (err) {
done.reject(err)
}
done.resolve(path)
})
return done.promise
}