1- import { exec , execFileSync } from 'node:child_process' ;
2- import { promisify } from 'node:util' ;
3-
4- const execAsync = promisify ( exec ) ;
1+ import { execa } from 'execa' ;
52
63/**
74 * Gets the port number that the process is listening on.
@@ -17,44 +14,42 @@ export async function getPort(): Promise<number | undefined> {
1714 switch ( platform ) {
1815 case 'linux' :
1916 case 'darwin' : {
20- const lsofResult = execFileSync (
21- 'lsof' ,
22- [ '-a' , '-i' , '-P' , '-n' , '-p' , pid . toString ( ) ] ,
23- {
24- encoding : 'utf-8' ,
25- }
26- ) ;
27- const awkResult = execFileSync (
17+ const lsofResult = await execa ( 'lsof' , [
18+ '-a' ,
19+ '-i' ,
20+ '-P' ,
21+ '-n' ,
22+ '-p' ,
23+ pid . toString ( ) ,
24+ ] ) ;
25+ const awkResult = await execa (
2826 'awk' ,
2927 [ '/LISTEN/ {split($9,a,":"); print a[length(a)]; exit}' ] ,
3028 {
31- encoding : 'utf-8' ,
32- input : lsofResult ,
29+ input : lsofResult . stdout ,
3330 }
3431 ) ;
35- const result = { stdout : awkResult } ;
32+ const result = { stdout : awkResult . stdout } ;
3633 port = parseInt ( result . stdout . trim ( ) , 10 ) ;
3734 break ;
3835 }
3936 case 'win32' : {
40- const lsofResult = execFileSync (
41- 'netstat' ,
42- [ '-a' , '-n' , '-o' , pid . toString ( ) ] ,
43- {
44- encoding : 'utf-8' ,
45- }
46- ) ;
47- const awkResult = execFileSync (
37+ const lsofResult = await execa ( 'netstat' , [
38+ '-a' ,
39+ '-n' ,
40+ '-o' ,
41+ pid . toString ( ) ,
42+ ] ) ;
43+ const awkResult = await execa (
4844 'awk' ,
4945 [
5046 '/LISTENING/ && /${pid}/ {split($2,a,\":\"); print a[length(a)]; exit}' ,
5147 ] ,
5248 {
53- encoding : 'utf-8' ,
54- input : lsofResult ,
49+ input : lsofResult . stdout ,
5550 }
5651 ) ;
57- const result = { stdout : awkResult } ;
52+ const result = { stdout : awkResult . stdout } ;
5853 port = parseInt ( result . stdout . trim ( ) , 10 ) ;
5954 break ;
6055 }
0 commit comments