Improve exec system
parent
c690a5425b
commit
58989a9b7e
|
@ -1,6 +1,5 @@
|
||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
import { promisify } from 'util';
|
import { exec, ExecOptions } from 'child_process';
|
||||||
import childProcess from 'child_process';
|
|
||||||
import nodemailer from 'nodemailer';
|
import nodemailer from 'nodemailer';
|
||||||
import { Message, PrivateChannel, GroupChannel, Member, User } from 'eris';
|
import { Message, PrivateChannel, GroupChannel, Member, User } from 'eris';
|
||||||
import uuid from 'uuid/v4';
|
import uuid from 'uuid/v4';
|
||||||
|
@ -31,16 +30,27 @@ export default class Util {
|
||||||
* @param command The command to execute
|
* @param command The command to execute
|
||||||
* @param options childProcess.ExecOptions
|
* @param options childProcess.ExecOptions
|
||||||
*/
|
*/
|
||||||
public async exec(command: string, options: childProcess.ExecOptions = {}): Promise<string> {
|
public async exec(command: string, options: ExecOptions = {}): Promise<string> {
|
||||||
const ex = promisify(childProcess.exec);
|
return new Promise((res, rej) => {
|
||||||
let result: string;
|
let error = false;
|
||||||
try {
|
let output = '';
|
||||||
const res = await ex(command, options);
|
const writeFunction = (data: string|Buffer|Error) => {
|
||||||
result = `${res.stdout}${res.stderr}`;
|
if (data instanceof Error) error = true;
|
||||||
} catch (err) {
|
output += `${data}`;
|
||||||
return Promise.reject(new Error(`Command failed: ${err.cmd}\n${err.stderr}${err.stdout}`));
|
};
|
||||||
}
|
const cmd = exec(command, options);
|
||||||
return result;
|
cmd.stdout.on('data', writeFunction);
|
||||||
|
cmd.stderr.on('data', writeFunction);
|
||||||
|
cmd.on('error', writeFunction);
|
||||||
|
cmd.once('close', (code, signal) => {
|
||||||
|
output += `Command exited with code ${code}${signal ? `/${signal}` : ''}`;
|
||||||
|
cmd.stdout.removeListener('data', writeFunction);
|
||||||
|
cmd.stderr.removeListener('data', writeFunction);
|
||||||
|
cmd.removeListener('error', writeFunction);
|
||||||
|
if (error) rej(output);
|
||||||
|
res(output);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue