From 5b4ae6763fd847e445e745c8ac3f91ee2d31e865 Mon Sep 17 00:00:00 2001 From: fofolee Date: Sun, 14 Jun 2020 15:39:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=AE=9E=E6=97=B6=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/preload.js | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/src/preload.js b/src/preload.js index ddc0b7c..345bca4 100644 --- a/src/preload.js +++ b/src/preload.js @@ -51,6 +51,7 @@ const QuickCommandActions = [ simulatePaste(); }, + // setTimout 不能在 vm2 中使用,同时在 electron 中有 bug sleep = ms => { var start = new Date().getTime() var cmd, tempFilePath @@ -130,37 +131,18 @@ runCodeInVm = (cmd, cb) => { return item.toString() } - var chunks = [], err_chunks = [] - - var callBackResult = () => { - cb(chunks.join("\n"), err_chunks.join("\n")) - } - - vm.on('console.log', chunk => { - console.log(chunk); - chunks.push(parseItem(chunk)) + vm.on('console.log', stdout => { + cb(parseItem(stdout), null) }); - vm.on('console.error', err_chunk => { - err_chunks.push(err_chunk.toString()) - }); - - var timer = setInterval(() => { - if (process.exitcode == 1) { - console.log('done'); - clearInterval(timer) - callBackResult() - } - }, 100); - - vm.run(` - try { + try { + vm.run(` ${cmd} - } catch(e) { - console.error(e) - } - process.exitcode = 1 - `, path.join(__dirname, 'preload.js')); + process.exitcode = 1 + `, path.join(__dirname, 'preload.js')); + } catch (error) { + cb(null, error.toString()) + } } // shell 以环境变量下命令作为代码提示