@@ -3,6 +3,8 @@ import { Options, execa } from "execa";
33export interface CommandOptions extends Options {
44 reject ?: boolean ;
55 timeout ?: number ;
6+ onStdout ?: ( data : string ) => void ;
7+ onStderr ?: ( data : string ) => void ;
68 env ?: Record < string , string > ;
79 cwd ?: string ;
810}
@@ -20,26 +22,44 @@ export async function executeCommand(
2022 options : CommandOptions = { }
2123) : Promise < CommandResult > {
2224 try {
25+ const streamOutput = options . onStdout || options . onStderr ;
26+
2327 const subprocess = execa ( executable , args , {
2428 reject : options . reject ?? false ,
2529 timeout : options . timeout ,
2630 env : options . env ,
2731 cwd : options . cwd ,
2832 encoding : "utf8" ,
33+ buffer : ! streamOutput ,
2934 } ) ;
3035
31- /*
32- // Pipe the output to the parent process
33- // This is commented out to avoid cluttering the output
34- // Uncomment if you want to see the output in real-time
35- if (subprocess.stdout) {
36- subprocess.stdout.pipe(process.stdout);
37- }
36+ if ( streamOutput ) {
37+ if ( subprocess . stdout ) {
38+ subprocess . stdout . on ( "data" , ( data : Buffer ) => {
39+ const lines = data . toString ( ) . split ( / \r ? \n / ) ;
40+ for ( const line of lines ) {
41+ if ( line . trim ( ) ) {
42+ if ( options . onStdout ) {
43+ options . onStdout ( line ) ;
44+ }
45+ }
46+ }
47+ } ) ;
48+ }
3849
39- if (subprocess.stderr) {
40- subprocess.stderr.pipe(process.stderr);
50+ if ( subprocess . stderr ) {
51+ subprocess . stderr . on ( "data" , ( data : Buffer ) => {
52+ const lines = data . toString ( ) . split ( / \r ? \n / ) ;
53+ for ( const line of lines ) {
54+ if ( line . trim ( ) ) {
55+ if ( options . onStderr ) {
56+ options . onStderr ( line ) ;
57+ }
58+ }
59+ }
60+ } ) ;
61+ }
4162 }
42- */
4363
4464 const { stdout, stderr, exitCode } = await subprocess ;
4565
0 commit comments