@@ -10,6 +10,8 @@ import { findExecutableById } from '@httptoolkit/osx-find-executable';
1010import { Interceptor } from '..' ;
1111import { HtkConfig } from '../../config' ;
1212import { reportError , addBreadcrumb } from '../../error-tracking' ;
13+ import { spawnToResult } from '../../util' ;
14+
1315import { getTerminalEnvVars } from './terminal-env-overrides' ;
1416import { editShellStartupScripts , resetShellStartupScripts } from './terminal-scripts' ;
1517
@@ -27,28 +29,6 @@ interface SpawnArgs {
2729 skipStartupScripts ?: true ;
2830}
2931
30- // Spawn a command, and resolve with all stdout & stderr output as strings when it terminates
31- const spawnAndCollectOutput = ( command : string , args : string [ ] = [ ] ) : Promise < { stdout : string , stderr : string } > => {
32- return new Promise ( ( resolve , reject ) => {
33- const childProc = spawn ( command , args , { stdio : 'pipe' } ) ;
34- const { stdout, stderr } = childProc ;
35-
36- const stdoutData : Buffer [ ] = [ ] ;
37- stdout . on ( 'data' , ( d ) => stdoutData . push ( d ) ) ;
38- const stderrData : Buffer [ ] = [ ] ;
39- stderr . on ( 'data' , ( d ) => stderrData . push ( d ) ) ;
40-
41- childProc . once ( 'error' , reject ) ;
42- childProc . once ( 'close' , ( ) => {
43- // Note that we do _not_ check the error code
44- resolve ( {
45- stdout : Buffer . concat ( stdoutData ) . toString ( ) ,
46- stderr : Buffer . concat ( stderrData ) . toString ( )
47- } ) ;
48- } ) ;
49- } ) ;
50- } ;
51-
5232const getTerminalCommand = _ . memoize ( async ( ) : Promise < SpawnArgs | null > => {
5333 let result : Promise < SpawnArgs | null > ;
5434
@@ -138,7 +118,7 @@ const getXTerminalCommand = async (command = 'x-terminal-emulator'): Promise<Spa
138118 try {
139119 // Run the command with -h to get some output we can use to infer the terminal itself.
140120 // --version would be nice, but the debian wrapper ignores it. --help isn't supported by xterm.
141- const { stdout } = await spawnAndCollectOutput ( command , [ '-h' ] ) ;
121+ const { stdout } = await spawnToResult ( command , [ '-h' ] ) ;
142122 const helpOutput = stdout . toLowerCase ( ) . replace ( / [ ^ \w \d ] + / g, ' ' ) ;
143123
144124 if ( helpOutput . includes ( 'gnome terminal' ) && await commandExists ( 'gnome-terminal' ) ) {
@@ -164,7 +144,7 @@ const getXTerminalCommand = async (command = 'x-terminal-emulator'): Promise<Spa
164144const getKonsoleTerminalCommand = async ( command = 'konsole' ) : Promise < SpawnArgs > => {
165145 let extraArgs : string [ ] = [ ] ;
166146
167- const { stdout } = await spawnAndCollectOutput ( command , [ '--help' ] ) ;
147+ const { stdout } = await spawnToResult ( command , [ '--help' ] ) ;
168148
169149 // Forces Konsole to run in the foreground, with no separate process
170150 // Seems to be well supported for a long time, but check just in case
@@ -178,7 +158,7 @@ const getKonsoleTerminalCommand = async (command = 'konsole'): Promise<SpawnArgs
178158const getGnomeTerminalCommand = async ( command = 'gnome-terminal' ) : Promise < SpawnArgs > => {
179159 let extraArgs : string [ ] = [ ] ;
180160
181- const { stdout } = await spawnAndCollectOutput ( command , [ '--help-all' ] ) ;
161+ const { stdout } = await spawnToResult ( command , [ '--help-all' ] ) ;
182162
183163 // Officially supported option, but only supported in v3.28+
184164 if ( stdout . includes ( '--wait' ) ) {
@@ -200,7 +180,7 @@ const getGnomeTerminalCommand = async (command = 'gnome-terminal'): Promise<Spaw
200180const getXfceTerminalCommand = async ( command = 'xfce4-terminal' ) : Promise < SpawnArgs > => {
201181 let extraArgs : string [ ] = [ ] ;
202182
203- const { stdout } = await spawnAndCollectOutput ( command , [ '--help' ] ) ;
183+ const { stdout } = await spawnToResult ( command , [ '--help' ] ) ;
204184
205185 // Disables the XFCE terminal server for this terminal, so it runs in the foreground.
206186 // Seems to be well supported for a long time, but check just in case
0 commit comments