@@ -133,6 +133,14 @@ export class SpawnError extends Error {
133133 }
134134}
135135
136+ export interface SpawnOptions {
137+ outputLabel ?: string ;
138+ outputChannel : LogOutputChannel ;
139+ cancellationToken ?: CancellationToken ;
140+ environment ?: Record < string , string | undefined > | undefined ;
141+ onStderr ?: ( data : Buffer , context : { abort : ( ) => void } ) => void ;
142+ }
143+
136144/**
137145 * Spawns a new process using the given `command`, with command-line arguments in `args`.
138146 * - All output is appended to the `options.outputChannel`, optionally prefixed by `options.outputLabel`.
@@ -143,12 +151,7 @@ export class SpawnError extends Error {
143151export const spawn = (
144152 command : string ,
145153 args : string [ ] ,
146- options : {
147- outputLabel ?: string ;
148- outputChannel : LogOutputChannel ;
149- cancellationToken ?: CancellationToken ;
150- environment ?: Record < string , string | undefined > | undefined ;
151- } ,
154+ options : SpawnOptions ,
152155) => {
153156 return new Promise < { code : number | null ; signal : NodeJS . Signals | null } > (
154157 ( resolve , reject ) => {
@@ -169,24 +172,38 @@ export const spawn = (
169172
170173 const child = childProcess . spawn ( command , args , spawnOptions ) ;
171174
175+ const killChild = ( ) => {
176+ // Use SIGINT on Unix, 'SIGTERM' on Windows
177+ const isWindows = os . platform ( ) === "win32" ;
178+ if ( isWindows ) {
179+ child . kill ( "SIGTERM" ) ;
180+ } else {
181+ child . kill ( "SIGINT" ) ;
182+ }
183+ } ;
184+
172185 const disposeCancel = options . cancellationToken ?. onCancellationRequested (
173186 ( ) => {
174187 outputChannel . appendLine (
175188 `${ outputLabel } Command cancelled: ${ commandLine } ` ,
176189 ) ;
177- // Use SIGINT on Unix, 'SIGTERM' on Windows
178- const isWindows = os . platform ( ) === "win32" ;
179- if ( isWindows ) {
180- child . kill ( "SIGTERM" ) ;
181- } else {
182- child . kill ( "SIGINT" ) ;
183- }
190+ killChild ( ) ;
184191 reject ( new Error ( "Command cancelled" ) ) ;
185192 } ,
186193 ) ;
187194
188195 pipeToLogOutputChannel ( child , outputChannel , outputLabel ) ;
189196
197+ if ( options . onStderr ) {
198+ child . stderr ?. on ( "data" , ( data : Buffer ) =>
199+ options . onStderr ?.( data , {
200+ abort ( ) {
201+ killChild ( ) ;
202+ } ,
203+ } ) ,
204+ ) ;
205+ }
206+
190207 child . on ( "close" , ( code , signal ) => {
191208 disposeCancel ?. dispose ( ) ;
192209
0 commit comments