Skip to content

Commit 6a9092b

Browse files
authored
chore(cli): fix CI linux tests (#727)
1 parent 7b671c3 commit 6a9092b

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
lines changed

packages/cli/lib/testing.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import process from 'node:process';
44
import degit from 'degit';
55
import { x, exec } from 'tinyexec';
66
import { create } from '@sveltejs/create';
7+
import pstree, { type PS } from 'ps-tree';
78

89
export { addPnpmBuildDependencies } from '../utils/package-manager.ts';
910
export type ProjectVariant = 'kit-js' | 'kit-ts' | 'vite-js' | 'vite-ts';
@@ -121,17 +122,36 @@ export async function startPreview({
121122
});
122123
}
123124

125+
async function getProcessTree(pid: number) {
126+
return new Promise<readonly PS[]>((res, rej) => {
127+
pstree(pid, (err, children) => {
128+
if (err) rej(err);
129+
res(children);
130+
});
131+
});
132+
}
133+
124134
async function terminate(pid: number) {
135+
if (process.platform === 'win32') {
136+
// on windows, use taskkill to terminate the process tree
137+
await x('taskkill', ['/PID', `${pid}`, '/T', '/F']);
138+
return;
139+
}
140+
const children = await getProcessTree(pid);
141+
// the process tree is ordered from parents -> children,
142+
// so we'll iterate in the reverse order to terminate the children first
143+
for (let i = children.length - 1; i >= 0; i--) {
144+
const child = children[i];
145+
const pid = Number(child.PID);
146+
kill(pid);
147+
}
148+
kill(pid);
149+
}
150+
151+
function kill(pid: number) {
125152
try {
126-
if (process.platform === 'win32') {
127-
// on windows, use taskkill to terminate the process tree
128-
await x('taskkill', ['/PID', `${pid}`, '/T', '/F']);
129-
} else {
130-
process.kill(-pid, 'SIGTERM'); // Kill the process group
131-
}
153+
process.kill(pid);
132154
} catch {
133-
try {
134-
process.kill(pid, 'SIGTERM'); // Kill just the process
135-
} catch {}
155+
// this can happen if a process has been automatically terminated.
136156
}
137157
}

packages/cli/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
"@sveltejs/cli-core": "workspace:*",
3636
"@sveltejs/create": "workspace:*",
3737
"@types/degit": "^2.8.6",
38+
"@types/ps-tree": "^1.1.6",
3839
"commander": "^13.1.0",
3940
"degit": "^2.8.4",
4041
"empathic": "^1.1.0",
4142
"package-manager-detector": "^0.2.11",
4243
"picocolors": "^1.1.1",
44+
"ps-tree": "^1.2.0",
4345
"tinyexec": "^0.3.2",
4446
"valibot": "^0.41.0"
4547
},

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)