smartshell/ts/smartshell.wrap.ts

205 lines
4.6 KiB
TypeScript
Raw Normal View History

import * as plugins from "./smartshell.plugins";
2017-03-08 15:51:02 +00:00
2017-03-10 19:14:40 +00:00
// interfaces
import * as cp from "child_process";
import { Deferred } from "@pushrocks/smartpromise";
2017-03-10 19:14:40 +00:00
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-08 15:51:02 +00:00
}
2017-03-11 12:46:08 +00:00
/**
* interface for streaming ExecResult
*/
2017-03-10 19:14:40 +00:00
export interface IExecResultStreaming {
childProcess: cp.ChildProcess;
finalPromise: Promise<IExecResult>;
2017-03-10 19:14:40 +00:00
}
2017-07-18 13:17:05 +00:00
/**
* imports path into the shell from env if available and returns it with
2017-07-18 13:17:05 +00:00
*/
let importEnvVarPath = (stringArg): string => {
2017-07-18 13:17:05 +00:00
if (process.env.SMARTSHELL_PATH) {
let commandResult = `PATH=${process.env.SMARTSHELL_PATH} && ${stringArg}`;
2017-07-18 13:17:05 +00:00
// console.log(commandResult)
return commandResult;
2017-07-18 13:17:05 +00:00
} else {
return stringArg;
2017-07-18 13:17:05 +00:00
}
};
2017-07-18 13:17:05 +00:00
2017-03-10 19:14:40 +00:00
/**
* executes a given command async
* @param commandStringArg
*/
export let exec = (
commandStringArg: string,
silentArg: boolean = false,
strictArg = false
): Promise<IExecResult> => {
let done = plugins.smartpromise.defer<IExecResult>();
const commandToExecute = importEnvVarPath(commandStringArg);
try {
const execChildProcess = cp.exec(commandToExecute, {
timeout: null,
maxBuffer: 1000000,
env: process.env
});
let logStore = "";
execChildProcess.stdout.on("data", (data: string) => {
if (!silentArg) {
console.log(data);
}
logStore += data;
});
execChildProcess.stderr.on("data", data => {
if (!silentArg) {
console.log(data);
}
logStore += data;
});
execChildProcess.on("exit", (code, signal) => {
done.resolve({
exitCode: code,
stdout: logStore
});
});
} catch (e) {
const error = e;
}
/*plugins.shelljs.exec(importEnvVarPath(commandStringArg), { async: true, silent: silentArg }, (code, stdout, stderr) => {
2017-09-09 11:20:24 +00:00
if (
stderr
&& (stderr !== '')
&& (!silentArg || strictArg)
&& (process.env.DEBUG === 'true')
) {
console.log('StdErr found.')
2017-07-18 13:17:05 +00:00
console.log(stderr)
2017-07-19 12:56:33 +00:00
}
if (strictArg) {
done.reject(new Error(stderr))
2017-07-18 13:17:05 +00:00
return
}
2017-03-08 15:51:02 +00:00
done.resolve({
exitCode: code,
stdout: stdout
})
})*/
return done.promise;
};
2017-03-08 15:51:02 +00:00
2017-03-10 19:14:40 +00:00
/**
* executes a given command async and silent
* @param commandStringArg
*/
export let execSilent = async (
commandStringArg: string
): Promise<IExecResult> => {
return await exec(commandStringArg, true);
};
2017-07-19 12:56:33 +00:00
/**
* executes strict, meaning it rejects the promise if something happens
*/
export let execStrict = async (
commandStringArg: string
): Promise<IExecResult> => {
return await exec(commandStringArg, true, true);
};
2017-03-10 19:14:40 +00:00
/**
* executes a command and allws you to stream output
*/
export let execStreaming = (
commandStringArg: string,
silentArg: boolean = false
) => {
let childProcessEnded = plugins.smartpromise.defer<IExecResult>();
const commandToExecute = importEnvVarPath(commandStringArg);
let execChildProcess = cp.exec(commandToExecute, {
timeout: null,
maxBuffer: 1000000,
env: process.env
});
let logStore = "";
execChildProcess.stdout.on("data", (data: string) => {
if (!silentArg) {
console.log(data);
}
logStore += data;
});
execChildProcess.stderr.on("data", data => {
if (!silentArg) {
console.log(data);
}
logStore += data;
});
execChildProcess.on("exit", (code, signal) => {
2017-03-10 19:14:40 +00:00
childProcessEnded.resolve({
exitCode: code,
stdout: logStore
});
});
2017-03-10 19:14:40 +00:00
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-27 09:18:37 +00:00
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
*/
export let execAndWaitForLine = (
commandStringArg: string,
regexArg: RegExp,
silentArg: boolean = false
) => {
let done = plugins.smartpromise.defer();
let execStreamingResult = execStreaming(commandStringArg, silentArg);
execStreamingResult.childProcess.stdout.on("data", (stdOutChunk: string) => {
2017-06-22 16:17:45 +00:00
if (regexArg.test(stdOutChunk)) {
done.resolve();
2017-06-22 16:17:45 +00:00
}
});
return done.promise;
};
2017-06-22 16:17:45 +00:00
export let execAndWaitForLineSilent = (
commandStringArg: string,
regexArg: RegExp
) => {
execAndWaitForLine(commandStringArg, regexArg, true);
};
2017-06-27 09:18:37 +00:00
2017-03-11 12:46:08 +00:00
/**
* get a path
*/
export let which = (cmd: string): Promise<string> => {
let done = plugins.smartpromise.defer<string>();
2017-03-11 12:46:08 +00:00
plugins.which(cmd, (err, path: string) => {
if (err) {
done.reject(err);
2017-03-11 12:46:08 +00:00
}
done.resolve(path);
});
return done.promise;
};