|
1 | 1 | const Shell = require('../shell') |
| 2 | +const sudo = require('../sudo') |
2 | 3 |
|
3 | 4 | const execute = Shell.execute |
4 | 5 |
|
5 | 6 | const executor = { |
6 | 7 | async windows (exec, { port }) { |
7 | | - const cmds = [`for /f "tokens=5" %a in ('netstat -aon ^| find ":${port}" ^| find "LISTENING"') do (taskkill /f /pid %a & exit /B) `] |
8 | | - await exec(cmds, { type: 'cmd' }) |
9 | | - return true |
| 8 | + const killCmd = `for /f "tokens=5" %a in ('netstat -aon ^| find ":${port}" ^| find "LISTENING"') do (taskkill /f /pid %a & exit /B)` |
| 9 | + try { |
| 10 | + await exec([killCmd], { type: 'cmd' }) |
| 11 | + return true |
| 12 | + } catch (e) { |
| 13 | + const msg = String(e || '') |
| 14 | + if (/Access is denied|not permitted|permission denied/i.test(msg)) { |
| 15 | + // Re-run via sudo to trigger UAC elevation on Windows |
| 16 | + await sudo(killCmd, { name: `KillByPort ${port}` }) |
| 17 | + return true |
| 18 | + } |
| 19 | + throw e |
| 20 | + } |
10 | 21 | }, |
11 | 22 | async linux (exec, { port }) { |
12 | | - await exec(`kill \`lsof -i:${port} |grep 'dev-sidecar\\|electron\\|@docmirro' |awk '{print $2}'\``) |
13 | | - return true |
| 23 | + const pidCmd = `lsof -i:${port} |grep 'dev-sidecar\\|electron\\|@docmirro' |awk '{print $2}'` |
| 24 | + try { |
| 25 | + await exec(`kill \`${pidCmd}\``) |
| 26 | + return true |
| 27 | + } catch (e) { |
| 28 | + const msg = String(e || '') |
| 29 | + if (/not permitted|Operation not permitted|permission denied/i.test(msg)) { |
| 30 | + await sudo(`kill \`${pidCmd}\``, { name: `KillByPort ${port}` }) |
| 31 | + return true |
| 32 | + } |
| 33 | + throw e |
| 34 | + } |
14 | 35 | }, |
15 | 36 | async mac (exec, { port }) { |
16 | | - await exec(`kill \`lsof -i:${port} |grep 'dev-side\\|Elect' |awk '{print $2}'\``) |
17 | | - return true |
| 37 | + const pidCmd = `lsof -i:${port} |grep 'dev-side\\|Elect' |awk '{print $2}'` |
| 38 | + try { |
| 39 | + await exec(`kill \`${pidCmd}\``) |
| 40 | + return true |
| 41 | + } catch (e) { |
| 42 | + const msg = String(e || '') |
| 43 | + if (/not permitted|Operation not permitted|permission denied/i.test(msg)) { |
| 44 | + await sudo(`kill \`${pidCmd}\``, { name: `KillByPort ${port}` }) |
| 45 | + return true |
| 46 | + } |
| 47 | + throw e |
| 48 | + } |
18 | 49 | }, |
19 | 50 | } |
20 | 51 |
|
|
0 commit comments