关闭窗口自动杀进程

This commit is contained in:
fofolee 2022-04-22 22:28:24 +08:00
parent 76dd321ff6
commit 2b84ebdda3
5 changed files with 48 additions and 23 deletions

View File

@ -8,7 +8,8 @@
"axios": "^0.24.0", "axios": "^0.24.0",
"iconv-lite": "^0.6.3", "iconv-lite": "^0.6.3",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"ses": "^0.15.15" "ses": "^0.15.15",
"tree-kill": "^1.2.2"
} }
}, },
"node_modules/axios": { "node_modules/axios": {
@ -63,6 +64,14 @@
"version": "0.15.15", "version": "0.15.15",
"resolved": "https://registry.npmmirror.com/ses/-/ses-0.15.15.tgz", "resolved": "https://registry.npmmirror.com/ses/-/ses-0.15.15.tgz",
"integrity": "sha512-sJM4HRlM3VouA3RhRmS7wG5MRQPqZZnc6O4BvAefU7yeM+qp8EUfGAWQ9iB/X5cNh3+m5N9lC7DEpyxQ+E4D+w==" "integrity": "sha512-sJM4HRlM3VouA3RhRmS7wG5MRQPqZZnc6O4BvAefU7yeM+qp8EUfGAWQ9iB/X5cNh3+m5N9lC7DEpyxQ+E4D+w=="
},
"node_modules/tree-kill": {
"version": "1.2.2",
"resolved": "https://registry.npmmirror.com/tree-kill/-/tree-kill-1.2.2.tgz",
"integrity": "sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==",
"bin": {
"tree-kill": "cli.js"
}
} }
}, },
"dependencies": { "dependencies": {
@ -101,6 +110,11 @@
"version": "0.15.15", "version": "0.15.15",
"resolved": "https://registry.npmmirror.com/ses/-/ses-0.15.15.tgz", "resolved": "https://registry.npmmirror.com/ses/-/ses-0.15.15.tgz",
"integrity": "sha512-sJM4HRlM3VouA3RhRmS7wG5MRQPqZZnc6O4BvAefU7yeM+qp8EUfGAWQ9iB/X5cNh3+m5N9lC7DEpyxQ+E4D+w==" "integrity": "sha512-sJM4HRlM3VouA3RhRmS7wG5MRQPqZZnc6O4BvAefU7yeM+qp8EUfGAWQ9iB/X5cNh3+m5N9lC7DEpyxQ+E4D+w=="
},
"tree-kill": {
"version": "1.2.2",
"resolved": "https://registry.npmmirror.com/tree-kill/-/tree-kill-1.2.2.tgz",
"integrity": "sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A=="
} }
} }
} }

View File

@ -3,6 +3,7 @@
"axios": "^0.24.0", "axios": "^0.24.0",
"iconv-lite": "^0.6.3", "iconv-lite": "^0.6.3",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"ses": "^0.15.15" "ses": "^0.15.15",
"tree-kill": "^1.2.2"
} }
} }

View File

@ -8,6 +8,7 @@ const axios = require('axios');
const http = require('http'); const http = require('http');
const url = require('url') const url = require('url')
const nodeFns = require("./lib/nodeFns") const nodeFns = require("./lib/nodeFns")
const kill = require('tree-kill')
require('ses') require('ses')
window._ = require("lodash") window._ = require("lodash")
@ -97,8 +98,8 @@ window.quickcommand = {
}, },
// 关闭进程 // 关闭进程
kill: function(pid, signal = 'SIGTERM') { kill: function(pid, signal = 'SIGTERM', cb) {
process.kill(pid, signal) kill(pid, signal, cb)
}, },
// dom 解析 // dom 解析
@ -211,22 +212,22 @@ if (process.platform !== 'linux') quickcommand.runInTerminal = function(cmdline,
} }
let getCommandToLaunchTerminal = (cmdline, dir) => { let getCommandToLaunchTerminal = (cmdline, dir) => {
let cd = '' let cd = ''
if (utools.isWindows()) { if (utools.isWindows()) {
let appPath = path.join(utools.getPath('home'), '/AppData/Local/Microsoft/WindowsApps/') let appPath = path.join(utools.getPath('home'), '/AppData/Local/Microsoft/WindowsApps/')
// 直接 existsSync wt.exe 无效 // 直接 existsSync wt.exe 无效
if (fs.existsSync(appPath) && fs.readdirSync(appPath).includes('wt.exe')) { if (fs.existsSync(appPath) && fs.readdirSync(appPath).includes('wt.exe')) {
cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `-d "${dir.replace(/\\/g, '/')}"`
command = `${appPath}wt.exe ${cd} cmd /k "${cmdline}"`
} else {
cmdline = cmdline.replace(/"/g, `^"`)
if (dir) cd = `cd /d "${dir.replace(/\\/g, '/')}" &&`
command = `${cd} start "" cmd /k "${cmdline}"`
}
} else {
cmdline = cmdline.replace(/"/g, `\\"`) cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `cd ${dir.replace(/ /g, `\\\\ `)} &&` if (dir) cd = `-d "${dir.replace(/\\/g, '/')}"`
command = `${appPath}wt.exe ${cd} cmd /k "${cmdline}"`
} else {
cmdline = cmdline.replace(/"/g, `^"`)
if (dir) cd = `cd /d "${dir.replace(/\\/g, '/')}" &&`
command = `${cd} start "" cmd /k "${cmdline}"`
}
} else {
cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `cd ${dir.replace(/ /g, `\\\\ `)} &&`
if (fs.existsSync('/Applications/iTerm.app')) { if (fs.existsSync('/Applications/iTerm.app')) {
command = `osascript -e 'tell application "iTerm" command = `osascript -e 'tell application "iTerm"
create window with default profile create window with default profile
@ -606,6 +607,7 @@ window.runCodeFile = (cmd, option, terminal, callback) => {
// let stderr = err_chunks.join(""); // let stderr = err_chunks.join("");
// callback(stdout, stderr) // callback(stdout, stderr)
// }) // })
return child
} }
let httpServer let httpServer

View File

@ -59,6 +59,7 @@ export default {
history: [], history: [],
historyIdx: null, historyIdx: null,
enableHtml: false, enableHtml: false,
child: null,
}; };
}, },
props: { props: {
@ -122,7 +123,7 @@ export default {
: this.$root.programs[currentCommand.program]; : this.$root.programs[currentCommand.program];
option.scptarg = currentCommand.scptarg || ""; option.scptarg = currentCommand.scptarg || "";
option.charset = currentCommand.charset || {}; option.charset = currentCommand.charset || {};
window.runCodeFile( this.child = window.runCodeFile(
currentCommand.cmd, currentCommand.cmd,
option, option,
currentCommand.output === "terminal", currentCommand.output === "terminal",
@ -237,6 +238,9 @@ export default {
}, },
stopRun() { stopRun() {
this.runResult = ""; this.runResult = "";
if (!!this.child) {
quickcommand.kill(this.child.pid);
}
if (!!this.listener) { if (!!this.listener) {
this.subInputValue = ""; this.subInputValue = "";
utools.removeSubInput(); utools.removeSubInput();

View File

@ -256,11 +256,15 @@ interface quickcommandApi {
loadRemoteScript(url: string): Promise<object>; loadRemoteScript(url: string): Promise<object>;
/** /**
* signal pid , process.kill * signal pid ,
*
* process.kill
*
* @param pid ID * @param pid ID
* @param signal SIGTERM * @param signal SIGTERM
* @param callback
*/ */
kill(pid: number, signal?: number | string): void; kill(pid: number, signal?: string | number, callback?: (error?: Error) => void): void;
/** /**
* windows VBS * windows VBS