mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Split exec
This commit is contained in:
@@ -77,7 +77,7 @@ module.exports = class EvalCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
makeResultMessages(result, hrDiff, input = null) {
|
makeResultMessages(result, hrDiff, input = null, lang = 'javascript') {
|
||||||
const inspected = util.inspect(result, { depth: 0 })
|
const inspected = util.inspect(result, { depth: 0 })
|
||||||
.replace(nlPattern, '\n')
|
.replace(nlPattern, '\n')
|
||||||
.replace(this.sensitivePattern, '--snip--');
|
.replace(this.sensitivePattern, '--snip--');
|
||||||
@@ -87,19 +87,19 @@ module.exports = class EvalCommand extends Command {
|
|||||||
const appendPart = inspected[last] !== '}' && inspected[last] !== ']' && inspected[last] !== '\''
|
const appendPart = inspected[last] !== '}' && inspected[last] !== ']' && inspected[last] !== '\''
|
||||||
? split[split.length - 1]
|
? split[split.length - 1]
|
||||||
: inspected[last];
|
: inspected[last];
|
||||||
const prepend = `\`\`\`javascript\n${prependPart}\n`;
|
const prepend = `\`\`\`${lang}\n${prependPart}\n`;
|
||||||
const append = `\n${appendPart}\n\`\`\``;
|
const append = `\n${appendPart}\n\`\`\``;
|
||||||
if (input) {
|
if (input) {
|
||||||
return Util.splitMessage(tags.stripIndents`
|
return Util.splitMessage(tags.stripIndents`
|
||||||
*Executed in ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
|
*Executed in ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
|
||||||
\`\`\`javascript
|
\`\`\`${lang}
|
||||||
${inspected}
|
${inspected}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
`, { maxLength: 1900, prepend, append });
|
`, { maxLength: 1900, prepend, append });
|
||||||
} else {
|
} else {
|
||||||
return Util.splitMessage(tags.stripIndents`
|
return Util.splitMessage(tags.stripIndents`
|
||||||
*Callback executed after ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
|
*Callback executed after ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
|
||||||
\`\`\`javascript
|
\`\`\`${lang}
|
||||||
${inspected}
|
${inspected}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
`, { maxLength: 1900, prepend, append });
|
`, { maxLength: 1900, prepend, append });
|
||||||
|
|||||||
+12
-9
@@ -2,7 +2,6 @@ const Command = require('../../framework/Command');
|
|||||||
const { exec } = require('child_process');
|
const { exec } = require('child_process');
|
||||||
const { promisify } = require('util');
|
const { promisify } = require('util');
|
||||||
const execAsync = promisify(exec);
|
const execAsync = promisify(exec);
|
||||||
const { stripIndents } = require('common-tags');
|
|
||||||
|
|
||||||
module.exports = class ExecCommand extends Command {
|
module.exports = class ExecCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
@@ -25,20 +24,24 @@ module.exports = class ExecCommand extends Command {
|
|||||||
|
|
||||||
async run(msg, { command }) {
|
async run(msg, { command }) {
|
||||||
const results = await this.exec(command);
|
const results = await this.exec(command);
|
||||||
return msg.reply(stripIndents`
|
const msgs = this.client.registry.commands.get('eval')
|
||||||
_${results.err ? 'An error occurred:' : 'Successfully executed.'}_
|
.makeResultMessages(results.std, results.hrDiff, command, 'sh');
|
||||||
\`\`\`sh
|
if (Array.isArray(msgs)) {
|
||||||
${results.std}
|
return msgs.map(item => msg.reply(item));
|
||||||
\`\`\`
|
} else {
|
||||||
`);
|
return msg.reply(msgs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async exec(command) {
|
async exec(command) {
|
||||||
|
let hrDiff;
|
||||||
try {
|
try {
|
||||||
|
const hrStart = process.hrtime();
|
||||||
const { stdout } = await execAsync(command, { timeout: 30000, encoding: 'utf8' });
|
const { stdout } = await execAsync(command, { timeout: 30000, encoding: 'utf8' });
|
||||||
return { err: false, std: stdout.trim() };
|
hrDiff = process.hrtime(hrStart);
|
||||||
|
return { err: false, std: stdout.trim(), hrDiff };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return { err: true, std: err.stderr.trim() };
|
return { err: true, std: err.stderr.trim(), hrDiff: null };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user