2018-07-30 14:03:48 +00:00
|
|
|
import * as plugins from './smartshell.plugins';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* a log handler for spawned logs
|
|
|
|
* making sure the process doesn't run out of memory
|
|
|
|
*/
|
|
|
|
export class ShellLog {
|
|
|
|
logStore = Buffer.from('');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* log data to console
|
2018-07-30 14:08:14 +00:00
|
|
|
* @param dataArg
|
2018-07-30 14:03:48 +00:00
|
|
|
*/
|
|
|
|
logToConsole(dataArg: string | Buffer): void {
|
|
|
|
// make sure we have the data as string
|
|
|
|
const dataString: string = (() => {
|
|
|
|
if (Buffer.isBuffer(dataArg)) {
|
|
|
|
return dataArg.toString();
|
|
|
|
}
|
|
|
|
return dataArg;
|
|
|
|
})();
|
2018-11-26 16:45:58 +00:00
|
|
|
process.stdout.write(dataString);
|
2018-07-30 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add data to Buffer for later consumption
|
|
|
|
* @param dataArg
|
|
|
|
*/
|
|
|
|
addToBuffer(dataArg: string | Buffer): void {
|
|
|
|
// make sure we have the data as Buffer
|
|
|
|
const dataBuffer: Buffer = (() => {
|
|
|
|
if (!Buffer.isBuffer(dataArg)) {
|
|
|
|
return Buffer.from(dataArg);
|
|
|
|
}
|
|
|
|
return dataArg;
|
|
|
|
})();
|
2018-07-30 14:08:14 +00:00
|
|
|
this.logStore = Buffer.concat([this.logStore, dataBuffer]);
|
2018-07-30 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logAndAdd(dataArg: string | Buffer): void {
|
|
|
|
this.logToConsole(dataArg);
|
|
|
|
this.addToBuffer(dataArg);
|
|
|
|
}
|
2018-07-30 14:08:14 +00:00
|
|
|
}
|