1- import { exec } from 'node:child_process' ;
1+ import { exec , execFileSync } from 'node:child_process' ;
22import { promisify } from 'node:util' ;
33
44const execAsync = promisify ( exec ) ;
@@ -17,17 +17,44 @@ export async function getPort(): Promise<number | undefined> {
1717 switch ( platform ) {
1818 case 'linux' :
1919 case 'darwin' : {
20- // Grab the first port entry reported
21- const result = await execAsync (
22- `lsof -a -i -P -n -p ${ pid } | awk '/LISTEN/ {split($9,a,":"); print a[length(a)]; exit}'`
20+ const lsofResult = execFileSync (
21+ 'lsof' ,
22+ [ '-a' , '-i' , '-P' , '-n' , '-p' , pid . toString ( ) ] ,
23+ {
24+ encoding : 'utf-8' ,
25+ }
2326 ) ;
27+ const awkResult = execFileSync (
28+ 'awk' ,
29+ [ '/LISTEN/ {split($9,a,":"); print a[length(a)]; exit}' ] ,
30+ {
31+ encoding : 'utf-8' ,
32+ input : lsofResult ,
33+ }
34+ ) ;
35+ const result = { stdout : awkResult } ;
2436 port = parseInt ( result . stdout . trim ( ) , 10 ) ;
2537 break ;
2638 }
2739 case 'win32' : {
28- const result = await execAsync (
29- `netstat -ano | awk "/LISTENING/ && /${ pid } / {split($2,a,\":\"); print a[length(a)]; exit}"`
40+ const lsofResult = execFileSync (
41+ 'netstat' ,
42+ [ '-a' , '-n' , '-o' , pid . toString ( ) ] ,
43+ {
44+ encoding : 'utf-8' ,
45+ }
46+ ) ;
47+ const awkResult = execFileSync (
48+ 'awk' ,
49+ [
50+ '/LISTENING/ && /${pid}/ {split($2,a,\":\"); print a[length(a)]; exit}' ,
51+ ] ,
52+ {
53+ encoding : 'utf-8' ,
54+ input : lsofResult ,
55+ }
3056 ) ;
57+ const result = { stdout : awkResult } ;
3158 port = parseInt ( result . stdout . trim ( ) , 10 ) ;
3259 break ;
3360 }
0 commit comments