2018-07-30 14:03:48 +00:00
|
|
|
// -- imports --
|
2018-07-18 18:58:12 +00:00
|
|
|
import * as plugins from './smartshell.plugins';
|
2018-07-30 14:08:14 +00:00
|
|
|
import { ShellEnv, IShellEnvContructorOptions, TExecutor } from './smartshell.classes.shellenv';
|
2018-07-30 14:03:48 +00:00
|
|
|
import { ShellLog } from './smartshell.classes.shelllog';
|
2017-03-08 15:51:02 +00:00
|
|
|
|
2018-07-30 14:08:14 +00:00
|
|
|
import * as cp from 'child_process';
|
|
|
|
import { Deferred } from '@pushrocks/smartpromise';
|
2017-03-08 15:51:02 +00:00
|
|
|
|
2018-07-30 14:03:48 +00:00
|
|
|
// -- interfaces --
|
|
|
|
/**
|
|
|
|
* interface for ExecResult
|
|
|
|
*/
|
|
|
|
export interface IExecResult {
|
|
|
|
exitCode: number;
|
|
|
|
stdout: string;
|
2017-03-08 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 14:03:48 +00:00
|
|
|
/**
|
|
|
|
* interface for streaming ExecResult
|
|
|
|
*/
|
|
|
|
export interface IExecResultStreaming {
|
|
|
|
childProcess: cp.ChildProcess;
|
|
|
|
finalPromise: Promise<IExecResult>;
|
2018-10-28 18:18:42 +00:00
|
|
|
kill: () => void;
|
2018-07-30 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- SmartShell --
|
2017-03-08 15:51:02 +00:00
|
|
|
export class Smartshell {
|
2019-05-19 20:41:20 +00:00
|
|
|
public shellEnv: ShellEnv;
|
|
|
|
public smartexit = new plugins.smartexit.SmartExit();
|
2018-07-30 14:03:48 +00:00
|
|
|
|
|
|
|
constructor(optionsArg: IShellEnvContructorOptions) {
|
|
|
|
this.shellEnv = new ShellEnv(optionsArg);
|
2018-11-26 16:55:15 +00:00
|
|
|
}
|
2017-03-10 19:14:40 +00:00
|
|
|
|
2018-07-30 14:03:48 +00:00
|
|
|
/**
|
|
|
|
* executes a given command async
|
|
|
|
* @param commandStringArg
|
|
|
|
*/
|
2018-07-30 14:08:14 +00:00
|
|
|
private async _exec(
|
2018-07-30 14:03:48 +00:00
|
|
|
commandStringArg: string,
|
|
|
|
silentArg: boolean = false,
|
|
|
|
strictArg = false,
|
|
|
|
streamingArg = false
|
|
|
|
): Promise<IExecResult | IExecResultStreaming> {
|
|
|
|
// flow control promises
|
|
|
|
const done = plugins.smartpromise.defer<IExecResult | IExecResultStreaming>();
|
|
|
|
const childProcessEnded = plugins.smartpromise.defer<IExecResult>();
|
|
|
|
// build commandToExecute
|
2018-07-30 14:08:14 +00:00
|
|
|
let commandToExecute = commandStringArg;
|
2018-07-30 14:03:48 +00:00
|
|
|
commandToExecute = this.shellEnv.createEnvExecString(commandStringArg);
|
|
|
|
const spawnlogInstance = new ShellLog();
|
|
|
|
const execChildProcess = cp.spawn(commandToExecute, [], {
|
|
|
|
shell: true,
|
2018-10-28 18:12:15 +00:00
|
|
|
env: process.env,
|
2019-05-27 13:30:52 +00:00
|
|
|
detached: false
|
2018-07-30 14:03:48 +00:00
|
|
|
});
|
|
|
|
|
2019-05-19 20:41:20 +00:00
|
|
|
this.smartexit.addProcess(execChildProcess);
|
|
|
|
|
2018-07-30 14:08:14 +00:00
|
|
|
execChildProcess.stdout.on('data', data => {
|
2018-07-30 14:03:48 +00:00
|
|
|
if (!silentArg) {
|
2019-05-28 08:43:54 +00:00
|
|
|
spawnlogInstance.writeToConsole(data);
|
2018-07-30 14:03:48 +00:00
|
|
|
}
|
|
|
|
spawnlogInstance.addToBuffer(data);
|
|
|
|
});
|
2018-07-30 14:08:14 +00:00
|
|
|
execChildProcess.stderr.on('data', data => {
|
2018-07-30 14:03:48 +00:00
|
|
|
if (!silentArg) {
|
2019-05-28 08:43:54 +00:00
|
|
|
spawnlogInstance.writeToConsole(data);
|
2018-07-30 14:03:48 +00:00
|
|
|
}
|
|
|
|
spawnlogInstance.addToBuffer(data);
|
|
|
|
});
|
|
|
|
|
2018-07-30 14:08:14 +00:00
|
|
|
if (streamingArg) {
|
2018-07-30 14:03:48 +00:00
|
|
|
done.resolve({
|
|
|
|
childProcess: execChildProcess,
|
2018-10-28 18:18:42 +00:00
|
|
|
finalPromise: childProcessEnded.promise,
|
|
|
|
kill: () => {
|
|
|
|
// this notation with the - kills the whole process group
|
|
|
|
process.kill(-execChildProcess.pid);
|
|
|
|
}
|
2018-07-30 14:03:48 +00:00
|
|
|
});
|
|
|
|
}
|
2017-03-10 21:08:04 +00:00
|
|
|
|
2018-07-30 14:08:14 +00:00
|
|
|
execChildProcess.on('exit', (code, signal) => {
|
2019-05-19 20:41:20 +00:00
|
|
|
this.smartexit.removeProcess(execChildProcess);
|
2018-07-30 14:08:14 +00:00
|
|
|
if (strictArg && code === 1) {
|
2018-07-30 14:03:48 +00:00
|
|
|
done.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
const execResult = {
|
|
|
|
exitCode: code,
|
|
|
|
stdout: spawnlogInstance.logStore.toString()
|
2018-07-30 14:08:14 +00:00
|
|
|
};
|
2018-07-30 14:03:48 +00:00
|
|
|
|
2018-07-30 14:08:14 +00:00
|
|
|
if (!streamingArg) {
|
2018-07-30 14:03:48 +00:00
|
|
|
done.resolve(execResult);
|
|
|
|
}
|
|
|
|
childProcessEnded.resolve(execResult);
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = await done.promise;
|
|
|
|
return result;
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|
2018-07-30 14:03:48 +00:00
|
|
|
|
2019-05-28 08:43:54 +00:00
|
|
|
public async exec(commandStringArg: string): Promise<IExecResult> {
|
2018-07-30 14:03:48 +00:00
|
|
|
return (await this._exec(commandStringArg, false)) as IExecResult;
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 21:08:04 +00:00
|
|
|
/**
|
2018-07-30 14:03:48 +00:00
|
|
|
* executes a given command async and silent
|
|
|
|
* @param commandStringArg
|
2017-03-10 21:08:04 +00:00
|
|
|
*/
|
2019-05-28 08:43:54 +00:00
|
|
|
public async execSilent(commandStringArg: string): Promise<IExecResult> {
|
2018-07-30 14:03:48 +00:00
|
|
|
return (await this._exec(commandStringArg, true)) as IExecResult;
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|
2017-03-10 21:08:04 +00:00
|
|
|
|
2017-03-11 01:36:27 +00:00
|
|
|
/**
|
2018-07-30 14:03:48 +00:00
|
|
|
* executes a command async and strict, meaning it rejects the promise if something happens
|
2017-03-11 01:36:27 +00:00
|
|
|
*/
|
2019-05-28 08:43:54 +00:00
|
|
|
public async execStrict(commandStringArg: string): Promise<IExecResult> {
|
2018-07-30 14:08:14 +00:00
|
|
|
return (await this._exec(commandStringArg, true, true)) as IExecResult;
|
|
|
|
}
|
2017-03-11 01:36:27 +00:00
|
|
|
|
2017-03-10 21:08:04 +00:00
|
|
|
/**
|
2018-07-30 14:03:48 +00:00
|
|
|
* executes a command and allows you to stream output
|
2017-03-10 21:08:04 +00:00
|
|
|
*/
|
2019-05-28 08:43:54 +00:00
|
|
|
public async execStreaming(
|
2018-07-30 14:03:48 +00:00
|
|
|
commandStringArg: string,
|
|
|
|
silentArg: boolean = false
|
|
|
|
): Promise<IExecResultStreaming> {
|
|
|
|
return (await this._exec(commandStringArg, silentArg, false, true)) as IExecResultStreaming;
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|
2018-07-30 14:03:48 +00:00
|
|
|
|
2019-05-28 08:43:54 +00:00
|
|
|
public async execStreamingSilent(commandStringArg: string) {
|
2018-07-30 14:03:48 +00:00
|
|
|
return (await this.execStreaming(commandStringArg, true)) as IExecResultStreaming;
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|
2018-07-30 14:03:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* executes a command and returns promise that will be fullfilled once an putput line matches RegexArg
|
|
|
|
* @param commandStringArg
|
|
|
|
* @param regexArg
|
|
|
|
*/
|
2019-05-28 08:43:54 +00:00
|
|
|
public async execAndWaitForLine(
|
|
|
|
commandStringArg: string,
|
|
|
|
regexArg: RegExp,
|
|
|
|
silentArg: boolean = false
|
|
|
|
) {
|
2018-07-30 14:03:48 +00:00
|
|
|
let done = plugins.smartpromise.defer();
|
|
|
|
let execStreamingResult = await this.execStreaming(commandStringArg, silentArg);
|
2018-07-30 14:08:14 +00:00
|
|
|
execStreamingResult.childProcess.stdout.on('data', (stdOutChunk: string) => {
|
2018-07-30 14:03:48 +00:00
|
|
|
if (regexArg.test(stdOutChunk)) {
|
|
|
|
done.resolve();
|
2017-03-10 21:08:04 +00:00
|
|
|
}
|
2018-07-30 14:03:48 +00:00
|
|
|
});
|
|
|
|
return done.promise;
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|
2018-07-30 14:03:48 +00:00
|
|
|
|
2019-05-28 08:43:54 +00:00
|
|
|
public async execAndWaitForLineSilent(commandStringArg: string, regexArg: RegExp) {
|
2018-07-30 14:03:48 +00:00
|
|
|
this.execAndWaitForLine(commandStringArg, regexArg, true);
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|
2019-05-28 08:43:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* execs an command and then enters interactive CLI
|
|
|
|
* @param commandStringArg
|
|
|
|
* @param regexArg
|
|
|
|
*/
|
|
|
|
public async execInteractive(commandStringArg: string) {
|
2019-05-28 09:14:32 +00:00
|
|
|
if (process.env.CI) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-28 08:43:54 +00:00
|
|
|
const done = plugins.smartpromise.defer();
|
|
|
|
const shell = cp.spawn('sh', [], { stdio: 'pipe' });
|
|
|
|
this.smartexit.addProcess(shell);
|
|
|
|
const shellLog = new ShellLog();
|
2019-05-28 09:14:32 +00:00
|
|
|
const stdInStream = process.stdin.pipe(shell.stdin);
|
|
|
|
const stdOutStream = shell.stdout.pipe(process.stdout);
|
2019-05-28 08:43:54 +00:00
|
|
|
shell.on('close', code => {
|
|
|
|
console.log(`interactive shell terminated with code ${code}`);
|
2019-05-28 09:14:32 +00:00
|
|
|
stdInStream.removeAllListeners();
|
|
|
|
stdInStream.uncork();
|
|
|
|
stdOutStream.removeAllListeners();
|
|
|
|
stdOutStream.unpipe();
|
|
|
|
shell.kill('SIGTERM');
|
2019-05-28 08:43:54 +00:00
|
|
|
done.resolve();
|
|
|
|
});
|
2019-05-28 09:14:32 +00:00
|
|
|
let commandString = commandStringArg;
|
|
|
|
if (process.env.CI) {
|
|
|
|
commandString += ' && exit';
|
|
|
|
}
|
|
|
|
commandString += '\n';
|
|
|
|
|
|
|
|
shell.stdin.write(commandString);
|
2019-05-28 08:43:54 +00:00
|
|
|
await done.promise;
|
|
|
|
}
|
2017-03-10 21:08:04 +00:00
|
|
|
}
|