@@ -2,9 +2,8 @@ import fs from 'node:fs';
22import path from 'node:path' ;
33import process from 'node:process' ;
44import degit from 'degit' ;
5- import { exec } from 'tinyexec' ;
5+ import { x , exec } from 'tinyexec' ;
66import { create } from '@sveltejs/create' ;
7- import pstree , { type PS } from 'ps-tree' ;
87
98export { addPnpmBuildDependencies } from '../utils/package-manager.ts' ;
109export type ProjectVariant = 'kit-js' | 'kit-ts' | 'vite-js' | 'vite-ts' ;
@@ -122,31 +121,17 @@ export async function startPreview({
122121 } ) ;
123122}
124123
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-
134124async function terminate ( pid : number ) {
135- const children = await getProcessTree ( pid ) ;
136- // the process tree is ordered from parents -> children,
137- // so we'll iterate in the reverse order to terminate the children first
138- for ( let i = children . length - 1 ; i >= 0 ; i -- ) {
139- const child = children [ i ] ;
140- const pid = Number ( child . PID ) ;
141- kill ( pid ) ;
142- }
143- kill ( pid ) ;
144- }
145-
146- function kill ( pid : number ) {
147125 try {
148- process . kill ( pid ) ;
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+ }
149132 } catch {
150- // this can happen if a process has been automatically terminated.
133+ try {
134+ process . kill ( pid , 'SIGTERM' ) ; // Kill just the process
135+ } catch { }
151136 }
152137}
0 commit comments