Skip to content

Commit 311da18

Browse files
committed
use execa
1 parent cbd26a6 commit 311da18

File tree

3 files changed

+45
-38
lines changed

3 files changed

+45
-38
lines changed

packages/utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"vitest": "catalog:"
4141
},
4242
"dependencies": {
43+
"execa": "9.6.0",
4344
"ms": "2.1.3",
4445
"pid-port": "2.0.0"
4546
}

packages/utils/src/get-port.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
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
}

pnpm-lock.yaml

Lines changed: 23 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)