改为动态获取输出内容

This commit is contained in:
fofolee 2020-06-15 18:01:26 +08:00
parent 9c7302c455
commit 55a46fda0a

View File

@ -5,9 +5,12 @@ const iconv = require('iconv-lite')
const electron = require('electron') const electron = require('electron')
const { NodeVM } = require('vm2') const { NodeVM } = require('vm2')
const path = require("path") const path = require("path")
const util = require("util")
if (!utools.isWindows()) process.env.PATH += ':/usr/local/bin:/usr/local/sbin' if (!utools.isWindows()) process.env.PATH += ':/usr/local/bin:/usr/local/sbin'
// window.startTime = new Date().getTime()
const QuickCommandActions = [ const QuickCommandActions = [
open = path => { open = path => {
@ -82,7 +85,9 @@ var getSandboxFuns = () => {
path: path, path: path,
os: os, os: os,
child_process: child_process, child_process: child_process,
util: util,
alert: alert, alert: alert,
// Swal: Swal,
$: { $: {
get: $.get, get: $.get,
post: $.post, post: $.post,
@ -135,6 +140,10 @@ runCodeInVm = (cmd, cb) => {
cb(parseItem(stdout), null) cb(parseItem(stdout), null)
}); });
vm.on('console.error', stderr => {
cb(null, stderr.toString())
});
try { try {
vm.run(` vm.run(`
${cmd} ${cmd}
@ -187,6 +196,10 @@ getNodeJsCommand = () => {
// tmpdir = os.tmpdir(), // tmpdir = os.tmpdir(),
// exists = fs.existsSync; // exists = fs.existsSync;
htmlEncode = (value, raw) => {
return raw ? String(value).replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;") : value
}
getQuickCommandScriptFile = ext => { getQuickCommandScriptFile = ext => {
return path.join(os.tmpdir(), `QuickCommandTempScript.${ext}`) return path.join(os.tmpdir(), `QuickCommandTempScript.${ext}`)
} }
@ -356,7 +369,7 @@ runCodeFile = (cmd, option, terminal, callback) => {
end if`; end if`;
child = child_process.spawn('osascript', ['-e', appleScript], { encoding: 'buffer' }) child = child_process.spawn('osascript', ['-e', appleScript], { encoding: 'buffer' })
} else { } else {
return utools.showNotification('Linux 不支持在终端输出') return message('Linux 不支持在终端输出')
} }
} else { } else {
child = child_process.spawn(bin, argvs, { encoding: 'buffer' }) child = child_process.spawn(bin, argvs, { encoding: 'buffer' })
@ -368,19 +381,21 @@ runCodeFile = (cmd, option, terminal, callback) => {
child = child_process.spawn(script, { encoding: 'buffer' }) child = child_process.spawn(script, { encoding: 'buffer' })
} }
} }
var chunks = [], // var chunks = [],
err_chunks = []; // err_chunks = [];
child.stdout.on('data', chunk => { child.stdout.on('data', chunk => {
if (option.codec) chunk = iconv.decode(chunk, option.codec) if (option.codec) chunk = iconv.decode(chunk, option.codec)
chunks.push(chunk) callback(chunk, null)
// chunks.push(chunk)
}) })
child.stderr.on('data', err_chunk => { child.stderr.on('data', stderr => {
if (option.codec) err_chunk = iconv.decode(err_chunk, option.codec) if (option.codec) stderr = iconv.decode(stderr, option.codec)
err_chunks.push(err_chunk) callback(null, stderr)
}) // err_chunks.push(err_chunk)
child.on('close', code => {
let stdout = chunks.join("");
let stderr = err_chunks.join("");
callback(stdout, stderr)
}) })
// child.on('close', code => {
// let stdout = chunks.join("");
// let stderr = err_chunks.join("");
// callback(stdout, stderr)
// })
} }