Skip to content

Commit 63f0809

Browse files
author
wan
committed
feat: 添加端口占用处理逻辑,优化错误提示信息
1 parent 147dd04 commit 63f0809

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/core/core.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ const osName = os.platform();
1010
var child;
1111
var child_pid;
1212

13+
const colorS = "\x1b[31m";
14+
const colorE = "\x1b[0m";
15+
16+
const PORT_9520_KILL_CMD = {
17+
darwin: 'lsof -i:9520 | awk \'{print $2}\' | tail -n +2 | xargs kill -9',
18+
win32: 'netstat -ano | findstr :9520 查找PID,然后执行 taskkill /F /PID <PID>'
19+
};
20+
1321

1422
/**
1523
* @description 判断是否是uniapp-cli项目
@@ -341,6 +349,9 @@ function runCmd(jest_for_node = 'node', cmd = [], opts = {}, testInfo = {}, isDe
341349
const stderr = readline.createInterface(child.stderr);
342350
stderr.on('line', (data) =>{
343351
let stdoutMsg = (data.toString()).trim();
352+
if (stdoutMsg.includes("Port 9520 is in use, please specify another port")) {
353+
is_port_9520_error = true;
354+
};
344355
if ((stdoutMsg.includes("Module Error") && stdoutMsg.includes("Errors compiling")) || stdoutMsg.includes("语法错误")) {
345356
printTestRunLog(MessagePrefix, stdoutMsg);
346357
stopRunTest();
@@ -360,7 +371,8 @@ function runCmd(jest_for_node = 'node', cmd = [], opts = {}, testInfo = {}, isDe
360371

361372
child.on('close', code => {
362373
if (is_port_9520_error) {
363-
createOutputChannel(`${MessagePrefix} 如果您遇到错误 Port 9520 is in use, please specify another port , 解决方法: 打开终端,输入命令 lsof -i:9520 | awk '{print $2}' | tail -n +2 | xargs kill -9`, "error", "log");
374+
const killCmd = PORT_9520_KILL_CMD[osName] || PORT_9520_KILL_CMD.win32;
375+
createOutputChannel(`${MessagePrefix} 端口被占用,解决方法: ${osName == 'darwin' ? '打开终端' : '打开命令'},输入 ${killCmd}`, "error", "log");
364376
};
365377

366378
child_pid = undefined;
@@ -436,6 +448,9 @@ async function runCmdForHBuilderXCli(jest_for_node = 'node', cmd = [], opts = {}
436448
const stderr = readline.createInterface(child.stderr);
437449
stderr.on('line', (data) =>{
438450
let stdoutMsg = (data.toString()).trim();
451+
if (stdoutMsg.includes("Port 9520 is in use")) {
452+
is_port_9520_error = true;
453+
};
439454
if ((stdoutMsg.includes("Module Error") && stdoutMsg.includes("Errors compiling")) || stdoutMsg.includes("语法错误")) {
440455
printTestRunLogForHBuilderXCli(MsgPrefix, stdoutMsg, logger);
441456
stopRunTest();
@@ -457,8 +472,8 @@ async function runCmdForHBuilderXCli(jest_for_node = 'node', cmd = [], opts = {}
457472
child.on('close', code => {
458473
(async () => {
459474
if (is_port_9520_error) {
460-
let msg_1 = `解决方法: 打开终端,输入命令 lsof -i:9520 | awk '{print $2}' | tail -n +2 | xargs kill -9`;
461-
await logger(`${MsgPrefix} 如果您遇到错误 Port 9520 is in use, please specify another port , ${msg_1}`);
475+
const killCmd = PORT_9520_KILL_CMD[osName] || PORT_9520_KILL_CMD.win32;
476+
await logger(`${colorS}${MsgPrefix} 端口被占用,解决方法: ${osName == 'darwin' ? '打开终端' : '打开命令行'},输入 ${killCmd}${colorE}`);
462477
};
463478

464479
child_pid = undefined;

0 commit comments

Comments
 (0)