forked from engineering/cloudservices
fixes to util/exec
parent
e8cfac48cc
commit
5fffe9f5f2
|
@ -1,5 +1,6 @@
|
||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
import { exec, ExecOptions } from 'child_process';
|
import { promisify } from 'util';
|
||||||
|
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';
|
||||||
|
@ -30,27 +31,16 @@ 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: ExecOptions = {}): Promise<string> {
|
public async exec(command: string, options: childProcess.ExecOptions = {}): Promise<string> {
|
||||||
return new Promise((res, rej) => {
|
const ex = promisify(childProcess.exec);
|
||||||
let error = false;
|
let result: string;
|
||||||
let output = '';
|
try {
|
||||||
const writeFunction = (data: string|Buffer|Error) => {
|
const res = await ex(command, options);
|
||||||
if (data instanceof Error) error = true;
|
result = `${res.stdout}${res.stderr}`;
|
||||||
output += `${data}`;
|
} catch (err) {
|
||||||
};
|
return Promise.reject(new Error(`Command failed: ${err.cmd}\n${err.stderr}${err.stdout}`));
|
||||||
const cmd = exec(command, options);
|
}
|
||||||
cmd.stdout.on('data', writeFunction);
|
return result;
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -163,7 +153,7 @@ export default class Util {
|
||||||
const tier = await this.client.db.Tier.findOne({ id: 1 });
|
const tier = await this.client.db.Tier.findOne({ id: 1 });
|
||||||
|
|
||||||
const account = new this.client.db.Account({
|
const account = new this.client.db.Account({
|
||||||
username, userID, emailAddress, createdBy: moderatorID, ramLimitNotification: tier.resourceLimits.ram - 50, createdAt: new Date(), locked: false, tier: 1, supportKey: code, ssInit: false, homepath: `/home/${username}`,
|
username, userID, emailAddress, createdBy: moderatorID, createdAt: new Date(), locked: false, tier: 1, supportKey: code, ssInit: false, ramLimitNotification: tier.resourceLimits.ram - 50, homepath: `/home/${username}`,
|
||||||
});
|
});
|
||||||
return account.save();
|
return account.save();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue