@@ -103,6 +103,7 @@ export type ProcessConfig = Omit<
103103 args ?: string [ ] ;
104104 observer ?: ProcessObserver ;
105105 ignoreExitCode ?: boolean ;
106+ silent ?: boolean ;
106107} ;
107108
108109/**
@@ -153,62 +154,65 @@ export type ProcessObserver = {
153154 * @param cfg - see {@link ProcessConfig}
154155 */
155156export function executeProcess ( cfg : ProcessConfig ) : Promise < ProcessResult > {
156- const { command, args, observer, ignoreExitCode = false , ...options } = cfg ;
157+ const { command, args, observer, ignoreExitCode, silent , ...options } = cfg ;
157158 const { onStdout, onStderr, onError, onComplete } = observer ?? { } ;
158159
159160 const bin = [ command , ...( args ?? [ ] ) ] . join ( ' ' ) ;
160161
161- return logger . command (
162- bin ,
163- ( ) =>
164- new Promise ( ( resolve , reject ) => {
165- const spawnedProcess = spawn ( command , args ?? [ ] , {
166- // shell:true tells Windows to use shell command for spawning a child process
167- // https://stackoverflow.com/questions/60386867/node-spawn-child-process-not-working-in-windows
168- shell : true ,
169- windowsHide : true ,
170- ...options ,
171- } ) as ChildProcessByStdio < Writable , Readable , Readable > ;
172-
173- // eslint-disable-next-line functional/no-let
174- let stdout = '' ;
175- // eslint-disable-next-line functional/no-let
176- let stderr = '' ;
177- // eslint-disable-next-line functional/no-let
178- let output = '' ; // interleaved stdout and stderr
179-
180- spawnedProcess . stdout . on ( 'data' , ( data : unknown ) => {
181- const message = String ( data ) ;
182- stdout += message ;
183- output += message ;
184- onStdout ?.( message , spawnedProcess ) ;
185- } ) ;
186-
187- spawnedProcess . stderr . on ( 'data' , ( data : unknown ) => {
188- const message = String ( data ) ;
189- stderr += message ;
190- output += message ;
191- onStderr ?.( message , spawnedProcess ) ;
192- } ) ;
193-
194- spawnedProcess . on ( 'error' , error => {
195- reject ( error ) ;
196- } ) ;
162+ const worker = ( ) =>
163+ new Promise < ProcessResult > ( ( resolve , reject ) => {
164+ const spawnedProcess = spawn ( command , args ?? [ ] , {
165+ // shell:true tells Windows to use shell command for spawning a child process
166+ // https://stackoverflow.com/questions/60386867/node-spawn-child-process-not-working-in-windows
167+ shell : true ,
168+ windowsHide : true ,
169+ ...options ,
170+ } ) as ChildProcessByStdio < Writable , Readable , Readable > ;
171+
172+ // eslint-disable-next-line functional/no-let
173+ let stdout = '' ;
174+ // eslint-disable-next-line functional/no-let
175+ let stderr = '' ;
176+ // eslint-disable-next-line functional/no-let
177+ let output = '' ; // interleaved stdout and stderr
178+
179+ spawnedProcess . stdout . on ( 'data' , ( data : unknown ) => {
180+ const message = String ( data ) ;
181+ stdout += message ;
182+ output += message ;
183+ onStdout ?.( message , spawnedProcess ) ;
184+ } ) ;
185+
186+ spawnedProcess . stderr . on ( 'data' , ( data : unknown ) => {
187+ const message = String ( data ) ;
188+ stderr += message ;
189+ output += message ;
190+ onStderr ?.( message , spawnedProcess ) ;
191+ } ) ;
197192
198- spawnedProcess . on ( 'close' , ( code , signal ) => {
199- const result : ProcessResult = { bin, code, signal, stdout, stderr } ;
200- if ( code === 0 || ignoreExitCode ) {
193+ spawnedProcess . on ( 'error' , error => {
194+ reject ( error ) ;
195+ } ) ;
196+
197+ spawnedProcess . on ( 'close' , ( code , signal ) => {
198+ const result : ProcessResult = { bin, code, signal, stdout, stderr } ;
199+ if ( code === 0 || ignoreExitCode ) {
200+ if ( ! silent ) {
201201 logger . debug ( output ) ;
202- onComplete ?.( ) ;
203- resolve ( result ) ;
204- } else {
202+ }
203+ onComplete ?.( ) ;
204+ resolve ( result ) ;
205+ } else {
206+ if ( ! silent ) {
205207 // ensure stdout and stderr are logged to help debug failure
206208 logger . debug ( output , { force : true } ) ;
207- const error = new ProcessError ( result ) ;
208- onError ?.( error ) ;
209- reject ( error ) ;
210209 }
211- } ) ;
212- } ) ,
213- ) ;
210+ const error = new ProcessError ( result ) ;
211+ onError ?.( error ) ;
212+ reject ( error ) ;
213+ }
214+ } ) ;
215+ } ) ;
216+
217+ return silent ? worker ( ) : logger . command ( bin , worker ) ;
214218}
0 commit comments